FFmpeg常用命令行讲解及实战一(一)https://developer.aliyun.com/article/1472390
②、举例
下面命令的功能是从输入视频文件的第 10 秒开始截取持续 10.345 秒的视频,并将截取后的视频保存为 FLV 格式的文件。视频和音频的编码格式将与输入文件相同,没有进行重新编码,只是进行了格式转换和截取操作。
ffmpeg -ss 10 -i SampleVideo_1280x720_20mb.mp4 -t 00:00:10.345 -vcodec copy -acodec copy -f flv -y SampleVideo-testflv.flv
回显信息:
PS D:\Work\test> ffmpeg -ss 10 -i SampleVideo_1280x720_20mb.mp4 -t 00:00:10.345 -vcodec copy -acodec copy -f flv -y SampleVideo-testflv.flv ffmpeg version 6.0-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers built with gcc 12.2.0 (Rev10, Built by MSYS2 project) configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband libavutil 58. 2.100 / 58. 2.100 libavcodec 60. 3.100 / 60. 3.100 libavformat 60. 3.100 / 60. 3.100 libavdevice 60. 1.100 / 60. 1.100 libavfilter 9. 3.100 / 9. 3.100 libswscale 7. 1.100 / 7. 1.100 libswresample 4. 10.100 / 4. 10.100 libpostproc 57. 1.100 / 57. 1.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'SampleVideo_1280x720_20mb.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 creation_time : 1970-01-01T00:00:00.000000Z encoder : Lavf53.24.2 Duration: 00:01:57.31, start: 0.000000, bitrate: 1436 kb/s Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1048 kb/s, 25 fps, 25 tbr, 12800 tbn (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : VideoHandler vendor_id : [0][0][0][0] Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 383 kb/s (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : SoundHandler vendor_id : [0][0][0][0] Output #0, flv, to 'SampleVideo-testflv.flv': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf60.3.100 Stream #0:0(und): Video: h264 (Main) ([7][0][0][0] / 0x0007), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 1048 kb/s, 25 fps, 25 tbr, 1k tbn (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : VideoHandler vendor_id : [0][0][0][0] Stream #0:1(und): Audio: aac (LC) ([10][0][0][0] / 0x000A), 48000 Hz, 5.1, fltp, 383 kb/s (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : SoundHandler vendor_id : [0][0][0][0] Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy) Press [q] to stop, [?] for help frame= 299 fps=0.0 q=-1.0 Lsize= 1618kB time=00:00:10.33 bitrate=1283.2kbits/s speed=1.2e+03x video:1039kB audio:563kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.975729% PS D:\Work\test>
使用 MediaInfo 查看生成文件相关信息,可以看到截取的视频长度为 11s 976 ms,且包含 1 路视频流及 1 路音频流,帧率为25 帧。
注:这里是 11s 多而不是 10.345s 的原因是截取时是以一个 GOP 进行截取的。
使用 VLC 播放确实如此:
2、视频选项
①、主要命令选项
-vframes number (output)
:设置输出文件的帧数,是 -frames:v 的别名。-b
:设定视频流量, 默认为 200Kbit/s,bitrate-r[:stream_specifier] fps (input/output,per-stream)
: 设置帧率(一种 Hz 值,缩写或者分数值)。-s[:stream_specifier] size (input/output,per-stream)
:设置帧的尺寸。-vn (output)
禁止输出视频。N:no-vcodec codec (output)
:设置视频编码器, 这是 -codec:v 的一个别名。aspect[:stream_specifier] aspect (output,per-stream)
:指定视频的纵横比(长宽显示比例)。aspect 是一 个浮点数字符串或者 num:den 格式字符串(其值就是 num/den) ,例如"4:3"
,"16:9"
,"1.3333"
以及"1.7777"
都是常用参数值。
②、举例
1)提取固定帧
下面命令的功能是将输入视频文件进行重新编码,并截取前 90 帧视频,然后将重新编码和截取后的视频保存为 FLV 格式的文件。视频编码器使用了 libx264,这意味着输出视频将使用 x264 编码器进行压缩,以提供高质量的视频编码。
ffmpeg -i SampleVideo_1280x720_20mb.mp4 -vcodec libx264 -vframes 75 -f flv -y SampleVideo-testflv.flv • 1
回显信息:
PS D:\Work\test> ffmpeg -i SampleVideo_1280x720_20mb.mp4 -vcodec libx264 -vframes 75 -f flv -y SampleVideo-testflv.flv ffmpeg version 6.0-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers built with gcc 12.2.0 (Rev10, Built by MSYS2 project) configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband libavutil 58. 2.100 / 58. 2.100 libavcodec 60. 3.100 / 60. 3.100 libavformat 60. 3.100 / 60. 3.100 libavdevice 60. 1.100 / 60. 1.100 libavfilter 9. 3.100 / 9. 3.100 libswscale 7. 1.100 / 7. 1.100 libswresample 4. 10.100 / 4. 10.100 libpostproc 57. 1.100 / 57. 1.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'SampleVideo_1280x720_20mb.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 creation_time : 1970-01-01T00:00:00.000000Z encoder : Lavf53.24.2 Duration: 00:01:57.31, start: 0.000000, bitrate: 1436 kb/s Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1048 kb/s, 25 fps, 25 tbr, 12800 tbn (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : VideoHandler vendor_id : [0][0][0][0] Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 383 kb/s (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : SoundHandler vendor_id : [0][0][0][0] Stream mapping: Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264)) Stream #0:1 -> #0:1 (aac (native) -> mp3 (libmp3lame)) Press [q] to stop, [?] for help [libx264 @ 00000211314d7340] using SAR=1/1 [libx264 @ 00000211314d7340] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 00000211314d7340] profile High, level 3.1, 4:2:0, 8-bit [libx264 @ 00000211314d7340] 264 - core 164 r3106 eaa68fa - H.264/MPEG-4 AVC codec - Copyleft 2003-2023 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=22 lookahead_threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 Output #0, flv, to 'SampleVideo-testflv.flv': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf60.3.100 Stream #0:0(und): Video: h264 ([7][0][0][0] / 0x0007), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 1k tbn (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : VideoHandler vendor_id : [0][0][0][0] encoder : Lavc60.3.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A Stream #0:1(und): Audio: mp3 ([2][0][0][0] / 0x0002), 48000 Hz, stereo, fltp (default) Metadata: creation_time : 1970-01-01T00:00:00.000000Z handler_name : SoundHandler vendor_id : [0][0][0][0] encoder : Lavc60.3.100 libmp3lame frame= 75 fps=0.0 q=-1.0 Lsize= 759kB time=00:00:02.97 bitrate=2088.0kbits/s speed=5.24x video:708kB audio:47kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.517854% [libx264 @ 00000211314d7340] frame I:1 Avg QP:20.95 size:110857 [libx264 @ 00000211314d7340] frame P:20 Avg QP:22.84 size: 20399 [libx264 @ 00000211314d7340] frame B:54 Avg QP:28.08 size: 3798 [libx264 @ 00000211314d7340] consecutive B-frames: 1.3% 8.0% 0.0% 90.7% [libx264 @ 00000211314d7340] mb I I16..4: 6.1% 25.2% 68.8% [libx264 @ 00000211314d7340] mb P I16..4: 1.7% 5.3% 0.8% P16..4: 44.9% 19.7% 10.2% 0.0% 0.0% skip:17.2% [libx264 @ 00000211314d7340] mb B I16..4: 0.2% 0.4% 0.1% B16..8: 50.8% 2.8% 0.3% direct: 0.6% skip:44.8% L0:54.1% L1:42.1% BI: 3.8% [libx264 @ 00000211314d7340] 8x8 transform intra:52.3% inter:70.8% [libx264 @ 00000211314d7340] coded y,uvDC,uvAC intra: 67.9% 80.2% 34.6% inter: 11.8% 15.8% 0.8% [libx264 @ 00000211314d7340] i16 v,h,dc,p: 19% 28% 10% 44% [libx264 @ 00000211314d7340] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 19% 14% 5% 9% 9% 8% 7% 8% [libx264 @ 00000211314d7340] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 13% 10% 9% 9% 10% 7% 14% 7% [libx264 @ 00000211314d7340] i8c dc,h,v,p: 46% 23% 20% 11% [libx264 @ 00000211314d7340] Weighted P-Frames: Y:0.0% UV:0.0% [libx264 @ 00000211314d7340] ref P L0: 71.2% 14.7% 13.0% 1.1% [libx264 @ 00000211314d7340] ref B L0: 96.7% 3.1% 0.2% [libx264 @ 00000211314d7340] ref B L1: 99.3% 0.7% [libx264 @ 00000211314d7340] kb/s:1930.46
使用 MediaInfo 查看生成文件相关信息,可以看到截取的视频长度为 3s 81ms,且包含 1 路视频流及 1 路音频流,帧率为25 帧。视频编码格式为 AVC,音频编码格式为 MP3。
2)禁止输出视频
下面命令的功能是将输入视频文件中的音频流提取出来,并保存为 FLV 格式的文件。通过使用 -vn 选项,该命令禁用了视频流的处理,只保留音频流。这可以用于提取音频轨道,或者将视频文件转换为只包含音频的文件。
ffmpeg -i SampleVideo_1280x720_20mb.mp4 -vn -f flv -y SampleVideo-testflv.flv
使用 MediaInfo 对其进行分析,可以看到只有一路音频流:
3)指定视频的纵横比
下面命令的功能是从输入视频文件中截取前 75 帧,并将截取后的视频调整为 640x480 的分辨率,同时设置显示比例为 4:3。然后将截取和调整后的视频保存为 FLV 格式的文件。
ffmpeg -i SampleVideo_1280x720_20mb.mp4 -vframes 75 -s 640*480 -aspect "4:3" -f flv -y SampleVideo-testflv.flv
使用 MediaInfo 对其进行分析,可以看到视频的横纵比为4:3
FFmpeg常用命令行讲解及实战一(三)https://developer.aliyun.com/article/1472393