ffmpeg使用说明(2):ffmpeg提取video,audio,yuv/rgb,PCM

目录

提取视频

保留封装格式

ffmpeg -i cctv1.flv -an -vcodec copy cctv1_copy.flv

$ ffprobe cctv1.flv 
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

$ ffmpeg -i cctv1.flv -an -vcodec copy cctv1_copy.flv

$ ffprobe cctv1_copy.flv
Input #0, flv, from 'cctv1_copy.flv':
  Metadata:
    encoder         : Lavf58.29.100
  Duration: 00:00:23.40, start: 0.103000, bitrate: 1733 kb/s
    Stream #0:0: Video: flv1, yuv420p, 1920x1080, 200 kb/s, 25 fps, 25 tbr, 1k tbn

转换封装格式

ffmpeg -i cctv1.flv -vcodec libx264 -an cctv1.h264

$ ffprobe cctv1.flv 
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
    
$ ffmpeg -i cctv1.flv -vcodec libx264 -an cctv1.h264

$ ffprobe cctv1.h264 
Input #0, h264, from 'cctv1.h264':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1080, 25 fps, 25 tbr, 1200k tbn, 50 tbc

是否需要转码的差异在于-vcodec copy 还是-vcodec codec_name

提取音频

保留封装格式

ffmpeg -i cctv1.flv -vn -acodec libmp3lame test.mp3

ffmpeg -i cctv1.flv -vn cctv1.mp3

$ ffprobe cctv1.flv 
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
    
$ ffmpeg -i cctv1.flv -vn cctv1.mp3

$ ffprobe cctv1.mp3
Input #0, mp3, from 'cctv1.mp3':
  Metadata:
    encoder         : Lavf58.29.100
  Duration: 00:00:14.54, start: 0.023021, bitrate: 128 kb/s
    Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.54
 

转换封装格式

$ ffmpeg -i cctv1.flv -vn -acodec aac cctv1.aac

$ ffprobe cctv1.flv 
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

$ ffmpeg -i cctv1.flv -vn -acodec aac cctv1.aac

$ ffprobe cctv1.aac 
Input #0, aac, from 'cctv1.aac':
  Duration: 00:00:14.10, bitrate: 135 kb/s
    Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp, 135 kb/s

是否需要转码的差异在于-acodec copy 还是-acodec codec_name

提取yuv

支持的yuv格式汇总

IO... yuv420p                3            12
IO... yuv422p                3            16
IO... yuv444p                3            24
IO... yuv410p                3             9
IO... yuv411p                3            12
IO... yuvj420p               3            12
IO... yuvj422p               3            16
IO... yuvj444p               3            24
IO... yuv440p                3            16
IO... yuvj440p               3            16
IO... yuva420p               4            20
IO... yuv420p16le            3            24
IO... yuv420p16be            3            24
IO... yuv422p16le            3            32
IO... yuv422p16be            3            32
IO... yuv444p16le            3            48
IO... yuv444p16be            3            48
IO... yuv420p9be             3            13
IO... yuv420p9le             3            13
IO... yuv420p10be            3            15
IO... yuv420p10le            3            15
IO... yuv422p10be            3            20
IO... yuv422p10le            3            20
IO... yuv444p9be             3            27
IO... yuv444p9le             3            27
IO... yuv444p10be            3            30
IO... yuv444p10le            3            30
IO... yuv422p9be             3            18
IO... yuv422p9le             3            18
IO... yuva422p               4            24
IO... yuva444p               4            32
IO... yuva420p9be            4            22
IO... yuva420p9le            4            22
IO... yuva422p9be            4            27
IO... yuva422p9le            4            27
IO... yuva444p9be            4            36
IO... yuva444p9le            4            36
IO... yuva420p10be           4            25
IO... yuva420p10le           4            25
IO... yuva422p10be           4            30
IO... yuva422p10le           4            30
IO... yuva444p10be           4            40
IO... yuva444p10le           4            40
IO... yuva420p16be           4            40
IO... yuva420p16le           4            40
IO... yuva422p16be           4            48
IO... yuva422p16le           4            48
IO... yuva444p16be           4            64
IO... yuva444p16le           4            64
IO... yuv420p12be            3            18
IO... yuv420p12le            3            18
IO... yuv420p14be            3            21
IO... yuv420p14le            3            21
IO... yuv422p12be            3            24
IO... yuv422p12le            3            24
IO... yuv422p14be            3            28
IO... yuv422p14le            3            28
IO... yuv444p12be            3            36
IO... yuv444p12le            3            36
IO... yuv444p14be            3            42
IO... yuv444p14le            3            42
IO... yuvj411p               3            12
IO... yuv440p10le            3            20
IO... yuv440p10be            3            20
IO... yuv440p12le            3            24
IO... yuv440p12be            3            24
IO... yuva422p12be           4            36
IO... yuva422p12le           4            36
IO... yuva444p12be           4            48
IO... yuva444p12le           4            48

不改变参数

ffmpeg -i cctv1.flv -pix_fmt yuv420p -t 40ms 1920x1080_yuv420p_cctv1.yuv

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
File '1920x1080_yuv420p_cctv1.yuv' already exists. Overwrite ? [y/N] y
Stream mapping:
  Stream #0:1 -> #0:0 (flv1 (flv) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to '1920x1080_yuv420p_cctv1.yuv':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1920x1080, q=2-31, 622080 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 rawvideo
frame=    1 fps=0.0 q=-0.0 Lsize=    3038kB time=00:00:00.04 bitrate=622080.0kbits/s dup=3 drop=0 speed=1.93x    
video:3038kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

改变参数

ffmpeg -i cctv1.flv -pix_fmt yuv420p -t 3 -s 320x240 320x240_yuv420p.yuv

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) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to '320x240_yuv420p.yuv':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, q=2-31, 23040 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 rawvideo
frame=   75 fps=0.0 q=-0.0 Lsize=    8438kB time=00:00:03.00 bitrate=23040.0kbits/s dup=3 drop=0 speed=9.82x    
video:8438kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

依据ffmpeg的使用说明变更指定参数即可

提取rgb

判断支持rgb的像素格式

IO... rgb24                  3            24
IO... rgb8                   3             8
.O..B rgb4                   3             4
IO... rgb4_byte              3             4
IO... rgba                   4            32
IO... rgb48be                3            48
IO... rgb48le                3            48
IO... rgb565be               3            16
IO... rgb565le               3            16
IO... rgb555be               3            15
IO... rgb555le               3            15
IO... rgb444le               3            12
IO... rgb444be               3            12
IO... rgba64be               4            64
IO... rgba64le               4            64
IO... rgb0                   3            24
$ ffmpeg -i cctv1.flv -pix_fmt rgb24 -s 1080x720 -t 40ms 1080x720_rgb24.rgb
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
File '1080x720_rgb24.rgb' already exists. Overwrite ? [y/N] y
Stream mapping:
  Stream #0:1 -> #0:0 (flv1 (flv) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to '1080x720_rgb24.rgb':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1080x720, q=2-31, 466560 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 rawvideo
frame=    1 fps=0.0 q=-0.0 Lsize=    2278kB time=00:00:00.04 bitrate=466560.0kbits/s dup=3 drop=0 speed= 1.8x    
video:2278kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

yuv与rgb互转

$ ffmpeg -video_size 1280x720 -pix_fmt yuv420p -i 1280x720_yuv420p.yuv -pix_fmt rgb24 -t 40ms  1280x720_rgb24.rgb

Input #0, rawvideo, from '1280x720_yuv420p.yuv':
  Duration: 00:00:06.76, start: 0.000000, bitrate: 276071 kb/s
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1280x720, 276480 kb/s, 25 tbr, 25 tbn, 25 tbc
File '1280x720_rgb24.rgb' already exists. Overwrite ? [y/N] y
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to '1280x720_rgb24.rgb':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1280x720, q=2-31, 552960 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 rawvideo
frame=    1 fps=0.0 q=-0.0 Lsize=    2700kB time=00:00:00.04 bitrate=552960.0kbits/s speed=1.23x    
video:2700kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

$ ffmpeg -video_size 1280x720 -pix_fmt rgb24 -i 1280x720_rgb24.rgb -pix_fmt yuv420p -t 40ms -s 320x240 320x240_yuv420p.yuv
Input #0, rawvideo, from '1280x720_rgb24.rgb':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 552960 kb/s
    Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1280x720, 552960 kb/s, 25 tbr, 25 tbn, 25 tbc
File '320x240_yuv420p.yuv' already exists. Overwrite ? [y/N] y
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to '320x240_yuv420p.yuv':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, q=2-31, 23040 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 rawvideo
frame=    1 fps=0.0 q=-0.0 Lsize=     112kB time=00:00:00.04 bitrate=23040.0kbits/s speed= 2.2x    
video:112kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

提取pcm

$ ffmpeg -i cctv1.aac -ar 48000 -ac 2 -f s16le 48000_2_s16le.pcm

Input #0, aac, from 'cctv1.aac':
  Duration: 00:00:14.10, bitrate: 135 kb/s
    Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp, 135 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (aac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, s16le, to '48000_2_s16le.pcm':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
    Metadata:
      encoder         : Lavc58.54.100 pcm_s16le
size=    2728kB time=00:00:14.54 bitrate=1536.0kbits/s speed= 524x    
video:0kB audio:2728kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

上一篇:ffmpeg使用说明(0):ffmpeg/ffplay/ffprobe


下一篇:javacv相关学习:使用javacv调用ffmpeg命令推流