ffmpeg-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

FFmpeg CLI Reference

FFmpeg CLI 参考手册

Contents

目录

Dependencies

依赖项

Verify ffmpeg is installed:
sh
ffmpeg -version
Install if missing:
sh
undefined
验证FFmpeg是否已安装:
sh
ffmpeg -version
如果未安装,执行以下命令安装:
sh
undefined

macOS

macOS

brew install ffmpeg
brew install ffmpeg

Ubuntu/Debian

Ubuntu/Debian

sudo apt install ffmpeg
sudo apt install ffmpeg

Windows (scoop)

Windows (scoop)

scoop install ffmpeg
undefined
scoop install ffmpeg
undefined

Glossary of flags and filters

参数与滤镜术语表

Flag/FilterPurpose
-vf
(also
-filter:v
)
Video filter
-af
(also
-filter:a
)
Audio filter
-filter_complex
Complex filter graph for multi-stream filtering
[0]
All streams from first input (0-based)
[0:v]
Video stream from first input
[1:a]
Audio stream from second input
0:v:0
First input, first video stream
0:a:1
First input, second audio stream
[name]
Named stream, used with
-filter_complex
-map [name]
Select stream for output
-y
Auto-overwrite output files without confirmation
Expression evaluations:
if
,
lte
,
gte
and more.
参数/滤镜用途
-vf
(也可写为
-filter:v
视频滤镜
-af
(也可写为
-filter:a
音频滤镜
-filter_complex
用于多流处理的复杂滤镜图
[0]
第一个输入文件的所有流(从0开始计数)
[0:v]
第一个输入文件的视频流
[1:a]
第二个输入文件的音频流
0:v:0
第一个输入文件的第一个视频流
0:a:1
第一个输入文件的第二个音频流
[name]
命名流,配合
-filter_complex
使用
-map [name]
选择输出流
-y
自动覆盖输出文件,无需确认
表达式运算:支持
if
lte
gte
等。

Converting formats

格式转换

Remux MP4 to MKV (no re-encoding):
sh
ffmpeg -i input.mp4 -c copy output.mkv
MKV and MP4 are both containers for H264/H265 video and AAC/MP3 audio. Video quality is determined by the codec, not the container. MKV supports multiple video streams; MP4 has wider device support.
Remux MP4 to MOV:
sh
ffmpeg -i input.mp4 -c copy output.mov
Encode MP4 to AVI (re-encodes):
sh
ffmpeg -i input.mp4 output.avi
将MP4格式重封装为MKV(无需重新编码):
sh
ffmpeg -i input.mp4 -c copy output.mkv
MKV和MP4均为H264/H265视频及AAC/MP3音频的容器格式。视频质量由编码格式决定,而非容器格式。MKV支持多视频流;MP4则拥有更广泛的设备兼容性。
将MP4格式重封装为MOV:
sh
ffmpeg -i input.mp4 -c copy output.mov
将MP4编码为AVI(需重新编码):
sh
ffmpeg -i input.mp4 output.avi

Resizing and padding

缩放与填充

Upscale to 1080x1920, preserve aspect ratio, black padding:
sh
ffmpeg -i input.mp4 -vf "scale=w=1080:h=1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1:1" output.mp4
scale options:
  • force_original_aspect_ratio=decrease
    - auto-decrease output dimensions to fit aspect ratio
  • force_original_aspect_ratio=increase
    - auto-increase dimensions
  • scale=w=1080:h=-1
    - let ffmpeg pick correct height for aspect ratio
  • scale=w=1080:h=-2
    - force dimensions divisible by 2
pad options:
  • pad=width:height:x:y:color
    - x:y is top-left corner position
  • (ow-iw)/2:(oh-ih)/2
    centres the video; negative values also centre
  • pad reference
setsar=1:1 ensures 1:1 pixel aspect ratio. Prevents ffmpeg from compensating for ratio changes. setsar reference
Two scaled outputs from one input (horizontal + vertical with logo overlay):
sh
ffmpeg -i input.mp4 -i logo.png \
  -filter_complex "[0:v]split=2[s0][s1]; \
    [s0]scale=w=1920:h=1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1:1[out1]; \
    [s1]scale=w=720:h=1280:force_original_aspect_ratio=decrease,pad=720:1280:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1:1[s2]; \
    [s2][1]overlay=(main_w-overlay_w)/2:(main_w-overlay_w)/5[out2]" \
  -map [out1] -map 0:a output_youtube.mp4 \
  -map [out2] -map 0:a output_shorts.mp4
将视频 upscale 至1080x1920,保持宽高比,添加黑边填充:
sh
ffmpeg -i input.mp4 -vf "scale=w=1080:h=1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1:1" output.mp4
scale参数选项
  • force_original_aspect_ratio=decrease
    - 自动缩小输出尺寸以适配原宽高比
  • force_original_aspect_ratio=increase
    - 自动放大输出尺寸
  • scale=w=1080:h=-1
    - 由FFmpeg自动计算符合宽高比的高度
  • scale=w=1080:h=-2
    - 强制尺寸为2的倍数
pad参数选项
  • pad=width:height:x:y:color
    - x:y为填充区域的左上角坐标
  • (ow-iw)/2:(oh-ih)/2
    - 将视频居中;负值同样可实现居中效果
  • pad参考文档
setsar=1:1用于确保1:1的像素宽高比,避免FFmpeg自动补偿宽高比变化。setsar参考文档
从单个输入生成两个缩放后的输出(横版+竖版,且添加logo水印):
sh
ffmpeg -i input.mp4 -i logo.png \
  -filter_complex "[0:v]split=2[s0][s1]; \
    [s0]scale=w=1920:h=1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1:1[out1]; \
    [s1]scale=w=720:h=1280:force_original_aspect_ratio=decrease,pad=720:1280:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1:1[s2]; \
    [s2][1]overlay=(main_w-overlay_w)/2:(main_w-overlay_w)/5[out2]" \
  -map [out1] -map 0:a output_youtube.mp4 \
  -map [out2] -map 0:a output_shorts.mp4

Trim by time

按时间剪辑

sh
ffmpeg -i input.mp4 -ss 00:00:10 -to 00:00:25 output.mp4
For faster but less accurate trimming, see the Input/output seeking section in
references/encoding-and-settings.md
. For the
-c copy
trade-offs when trimming, see
references/core-concepts.md
.
sh
ffmpeg -i input.mp4 -ss 00:00:10 -to 00:00:25 output.mp4
如需更快但精度稍低的剪辑方式,请查看
references/encoding-and-settings.md
中的输入/输出seek章节。关于剪辑时使用
-c copy
的利弊,请查看
references/core-concepts.md

Verification and inspection

验证与检查

List supported formats:
sh
ffmpeg -formats
List supported codecs:
sh
ffmpeg -codecs
列出支持的格式:
sh
ffmpeg -formats
列出支持的编码格式:
sh
ffmpeg -codecs

ffprobe

ffprobe

ffprobe provides structured metadata about media files.
Show detailed stream information:
sh
ffprobe -show_streams -i input.mp4
Verify moov atom position (faststart):
sh
ffprobe -v trace -i input.mp4
Look for
type:'moov'
near the beginning of output to confirm faststart is applied.
ffprobe用于提取媒体文件的结构化元数据。
显示详细的流信息:
sh
ffprobe -show_streams -i input.mp4
验证moov原子的位置(faststart):
sh
ffprobe -v trace -i input.mp4
在输出内容开头附近查找
type:'moov'
即可确认已应用faststart。

Reference files

参考文件

Load these on demand with the Read tool when the task requires deeper coverage.
FileTopics
references/core-concepts.md
-c copy
(remux vs transcode), input/output seeking, encoding quick ref (H264/H265/VP9 CRF ranges), GPU acceleration overview,
format=yuv420p
,
-movflags +faststart
references/audio-processing.md
Replace audio, extract audio, mix audio, combine MP3 tracks, crossfade, change audio format, merge and normalise
references/advanced-editing.md
Playback speed, FPS change, jump cuts, video cropping for social, drawtext overlay, subtitles (burn/embed/extract), combine media (overlay, logo, background, concat intro/main/outro, vstack)
references/asset-generation.md
Image to video, slideshow with fade, Ken Burns (zoompan), GIFs, video compilation with fades, thumbnails (single/multiple/scene), image thumbnails, storyboards (scene tile/keyframe/Nth frame)
references/encoding-and-settings.md
Optimised daily command, H264 (libx264) deep-dive, H265 (libx265) Apple compat, VP9 (libvpx-vp9) constant quality, 1-pass vs 2-pass,
-c copy
detailed, seeking detailed, GPU detailed (Nvidia/Intel/AMD)
当任务需要更深入的内容时,可使用读取工具按需加载以下文件。
文件主题
references/core-concepts.md
-c copy
(重封装 vs 转码)、输入/输出seek、编码快速参考(H264/H265/VP9的CRF范围)、GPU加速概述、
format=yuv420p
-movflags +faststart
references/audio-processing.md
替换音频、提取音频、混音、合并MP3轨道、交叉淡入淡出、转换音频格式、合并与归一化
references/advanced-editing.md
播放速度调整、帧率修改、跳剪、社交平台视频裁剪、drawtext水印、字幕(硬刻录/嵌入/提取)、媒体合成(水印、logo、背景、拼接片头/正片/片尾、垂直拼接)
references/asset-generation.md
图片转视频、带淡入淡出的幻灯片、Ken Burns效果(zoompan)、GIF生成、带淡入淡出的视频合集、缩略图(单张/多张/场景)、图片缩略图、故事板(场景分镜/关键帧/第N帧)
references/encoding-and-settings.md
日常优化命令、H264(libx264)深入解析、H265(libx265)苹果兼容性、VP9(libvpx-vp9)恒定质量、1-pass vs 2-pass编码、
-c copy
详细说明、seek详细说明、GPU加速详细说明(Nvidia/Intel/AMD)