Skip to content

Instantly share code, notes, and snippets.

@QidiLiu
Created December 8, 2020 19:11
Show Gist options
  • Save QidiLiu/5dd45ade31b2b3f037e775e604402ffb to your computer and use it in GitHub Desktop.
Save QidiLiu/5dd45ade31b2b3f037e775e604402ffb to your computer and use it in GitHub Desktop.
FFmpeg 指令集

FFmpeg 指令集

ffmpeg命令行工具的命令列表。翻译自FFmpeg cheat sheet

下载 FFmpeg: https://www.ffmpeg.org/download.html

完整文档: https://www.ffmpeg.org/ffmpeg.html

基本转码

ffmpeg -i in.mp4 out.avi

将MKV文件重混为MP4

ffmpeg -i in.mkv -c:v copy -c:a copy out.mp4

高质量编码

Use the crf (Constant Rate Factor) parameter to control the output quality. The lower crf, the higher the quality (range: 0-51). The default value is 23, and visually lossless compression corresponds to -crf 18. Use the preset parameter to control the speed of the compression process. Additional info: https://trac.ffmpeg.org/wiki/Encode/H.264

ffmpeg -i in.mp4 -preset slower -crf 18 out.mp4

视频截取片段

无重新编码:

ffmpeg -ss [start] -i in.mp4 -t [duration] -c copy out.mp4
  • -ss specifies the start time, e.g. 00:01:23.000 or 83 (in seconds)
  • -t specifies the duration of the clip (same format).
  • Recent ffmpeg also has a flag to supply the end time with -to.
  • -c copy copies the first video, audio, and subtitle bitstream from the input to the output file without re-encoding them. This won't harm the quality and make the command run within seconds.

带重新编码:

如果不使用-c复制选项,则ffmpeg将根据您选择的格式自动对输出的视频和音频重新编码。要获得高质量的视频和音频,请阅读 x264 Encoding GuideAAC Encoding Guide.

例如:

ffmpeg -ss [start] -i in.mp4 -t [duration] -c:v libx264 -c:a aac -strict experimental -b:a 128k out.mp4

混合视频和其他视频的音频

从in0.mp4复制视频并从in1.mp4复制音频:

ffmpeg -i in0.mp4 -i in1.mp4 -c copy -map 0:0 -map 1:1 -shortest out.mp4

Concat 解复用器

首先,制作一个文本文件

file 'in1.mp4'
file 'in2.mp4'
file 'in3.mp4'
file 'in4.mp4'

然后运行ffmpeg:

ffmpeg -f concat -i list.txt -c copy out.mp4

延迟音频/视频

延迟影片3.84秒:

ffmpeg -i in.mp4 -itsoffset 3.84 -i in.mp4 -map 1:v -map 0:a -vcodec copy -acodec copy out.mp4

延迟音频3.84秒:

ffmpeg -i in.mp4 -itsoffset 3.84 -i in.mp4 -map 0:v -map 1:a -vcodec copy -acodec copy out.mp4

刻录字幕

libass (确保您的ffmpeg安装在配置--enable-libass中具有该库).

首先将字幕转换为.ass格式:

ffmpeg -i sub.srt sub.ass

然后使用视频滤镜添加它们:

ffmpeg -i in.mp4 -vf ass=sub.ass out.mp4

从视频中提取帧

提取1到5秒之间以及11到15秒之间的所有帧:

ffmpeg -i in.mp4 -vf select='between(t,1,5)+between(t,11,15)' -vsync 0 out%d.png

仅每秒提取一帧:

ffmpeg -i in.mp4 -fps=1 -vsync 0 out%d.png

旋转视频

顺时针旋转90:

ffmpeg -i in.mov -vf "transpose=1" out.mov

对于transpose参数,您可以传递:

0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip

使用-vf“ transpose = 2,transpose = 2”180度.

下载“传输流”视频流

  1. 找到播放列表文件, e.g. using Chrome > F12 > Network > Filter: m3u8
  2. 下载并连接视频片段:
ffmpeg -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4

如果您收到“协议'https不在白名单'文件,密码'上!”错误,添加protocol_whitelist选项:

ffmpeg -protocol_whitelist "file,http,https,tcp,tls" -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4

使某些音频静音

用静音取代音频的前90秒:

ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='lte(t,90)':volume=0" out.mp4

用静音替换1'20“和1'30”之间的所有音频:

ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='between(t,80,90)':volume=0" out.mp4

隔行

使用“还有另一个反交错滤镜”进行反交错.

ffmpeg -i in.mp4 -vf yadif out.mp4

从图像创建视频幻灯片

参数:-r标记图像帧速率(每个图像的倒数时间); -vf fps = 25标记输出的真实帧率

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4

从视频中提取图像

  • 提取所有帧: ffmpeg -i input.mp4 thumb%04d.jpg -hide_banner
  • 每秒提取一帧: ffmpeg -i input.mp4 -vf fps=1 thumb%04d.jpg -hide_banner
  • 仅提取一帧: ffmpeg -i input.mp4 -ss 00:00:10.000 -vframes 1 thumb.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment