Java教程

ffmpeg使用说明(3):ffmpeg音视频格式转换,裁剪,合并

本文主要是介绍ffmpeg使用说明(3):ffmpeg音视频格式转换,裁剪,合并,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 格式转换
    • 封装格式
    • 编码格式
    • 帧率
    • 码率: -b:v 视频码率, -b:a 音频码率
    • 视频分辨率
    • 音频采样率
  • 裁剪
  • 合并

格式转换

封装格式

  1. 保持音频编码ffmpeg -i cctv1.mp4 -codec copy cctv1.ts
$ ffmpeg -i cctv1.mp4 -codec copy cctv1.ts

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Output #0, mpegts, to 'cctv1.ts':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, q=2-31, 2088 kb/s, 25 fps, 25 tbr, 90k tbn, 12800 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)

编码格式

  1. 改变音频编码ffmpeg -i cctv1.flv -acodec aac cctv1.mp4
$ ffmpeg -i cctv1.flv -acodec aac cctv1.mp4

Input #0, flv, from 'cctv1.flv':
  Metadata:
    encoder         : Lavf58.20.100
  Duration: 00:00:23.69, start: 0.000000, bitrate: 1793 kb/s
    Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 128 kb/s
    Stream #0:1: Video: flv1, yuv420p, 1920x1080, 200 kb/s, 25 fps, 25 tbr, 1k tbn

Stream mapping:
  Stream #0:1 -> #0:0 (flv1 (flv) -> h264 (libx264))
  Stream #0:0 -> #0:1 (mp3 (mp3float) -> aac (native))

Output #0, mp4, to 'cctv1.mp4':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080, q=-1--1, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.54.100 aac

  1. 改变视频格式ffmpeg -i cctv1.flv -vcodec h264 cctv1.mp4
$ ffmpeg -i cctv1.flv -vcodec h264 cctv1.mp4

Input #0, flv, from 'cctv1.flv':
  Metadata:
    encoder         : Lavf58.20.100
  Duration: 00:00:23.69, start: 0.000000, bitrate: 1793 kb/s
    Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 128 kb/s
    Stream #0:1: Video: flv1, yuv420p, 1920x1080, 200 kb/s, 25 fps, 25 tbr, 1k tbn

Stream mapping:
  Stream #0:1 -> #0:0 (flv1 (flv) -> h264 (libx264))
  Stream #0:0 -> #0:1 (mp3 (mp3float) -> aac (native))

Output #0, mp4, to 'cctv1.mp4':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080, q=-1--1, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.54.100 aac

    

  1. 音频编码和视频编码全部变更ffmpeg -i cctv1.flv -acodec aac -vcodec h264 cctv1.mp4
$ ffmpeg -i cctv1.flv -acodec aac -vcodec h264 cctv1.mp4

Input #0, flv, from 'cctv1.flv':
  Metadata:
    encoder         : Lavf58.20.100
  Duration: 00:00:23.69, start: 0.000000, bitrate: 1793 kb/s
    Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 128 kb/s
    Stream #0:1: Video: flv1, yuv420p, 1920x1080, 200 kb/s, 25 fps, 25 tbr, 1k tbn

Stream mapping:
  Stream #0:1 -> #0:0 (flv1 (flv) -> h264 (libx264))
  Stream #0:0 -> #0:1 (mp3 (mp3float) -> aac (native))

Output #0, mp4, to 'cctv1.mp4':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080, q=-1--1, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.54.100 aac

帧率

ffmpeg -i cctv1.mp4 -r 15 cctv1_15fps.mp4

$ ffmpeg -i cctv1.mp4 -r 15 cctv1_15fps.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))

Output #0, mp4, to 'cctv1_15fps.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080, q=-1--1, 15 fps, 15360 tbn, 15 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.54.100 aac

码率: -b:v 视频码率, -b:a 音频码率

ffmpeg -i cctv1.mp4 -b:v 400k -b:a 100k cctv1_15fps.mp4

$ ffmpeg -i cctv1.mp4 -b:v 400k -b:a 100k cctv1_15fps.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))

Output #0, mp4, to 'cctv1_15fps.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080, q=-1--1, 400 kb/s, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/400000 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 100 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.54.100 aac

视频分辨率

ffmpeg -i cctv1.mp4 -s 1280x720 cctv1_1280x720.mp4

$ ffmpeg -i cctv1.mp4 -s 1280x720 cctv1_1280x720.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))

Output #0, mp4, to 'cctv1_1280x720.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1280x720, q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.54.100 aac

音频采样率

$ ffmpeg -i cctv1.mp4 -ar 44100 cctv1_44100.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))

Output #0, mp4, to 'cctv1_44100.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080, q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.54.100 aac

裁剪

ffmpeg -i cctv1.mp4 -ss 5 -t 3 -codec copy 1.mp4

  • -ss 裁剪开始时刻
  • -t 裁剪时长(s)
$ ffmpeg -i cctv1.mp4 -ss 5 -t 3  -codec copy 1.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Output #0, mp4, to '1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, q=2-31, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
frame=    0 fps=0.0 q=-1.0 Lsize=      48kB time=00:00:02.97 bitrate= 132.5kbits/s speed=1.08e+03x  

合并

  1. 编写concat文件

    file '1.mp4'
    file '2.mp4'
    
  2. ffmpeg -f concat -i filelist.txt -codec copy output.mp4

$ ffmpeg -f concat -i filelist.txt -codec copy output.mp4

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x559349f6e500] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'filelist.txt':
  Duration: N/A, start: -0.021333, bitrate: 3796 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 3668 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s
    Metadata:
      handler_name    : SoundHandler
Output #0, mp4, to 'output.mp4':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, q=2-31, 3668 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x559349f7f080] Auto-inserting h264_mp4toannexb bitstream filter
frame=  150 fps=0.0 q=-1.0 Lsize=    2646kB time=00:00:06.04 bitrate=3588.7kbits/s speed= 267x    
video:2548kB audio:93kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.191427%


Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:03.02, start: 0.000000, bitrate: 3778 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 3668 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:03.00, start: 0.000000, bitrate: 3422 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 3287 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      
      
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:06.06, start: 0.000000, bitrate: 3575 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 3465 kb/s, 24.91 fps, 50 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 126 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

这篇关于ffmpeg使用说明(3):ffmpeg音视频格式转换,裁剪,合并的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!