Identify the operation type first, then apply the matching pattern below. For multi-operation requests (e.g., trim + resize + convert), chain all filters in a single ffmpeg invocation — avoid intermediate files.
首先识别操作类型,然后应用下方匹配的模板。对于多操作请求(如修剪+调整尺寸+转换),在单个ffmpeg调用中链接所有滤镜——避免生成中间文件。
- Identify the operation(s) from the user's request.
- Probe codec when converting formats; probe dimensions when resizing. Speed, rotation, flip, and frame extraction do not require probing.
- Construct the command using the appropriate pattern.
- Confirm with the user before running.
- Run and report output file path and size.
- 从用户请求中识别操作类型。
- 转换格式时探测编解码器;调整尺寸时探测分辨率。调速、旋转、翻转和帧提取无需探测。
- 使用合适的模板构造命令。
- 运行前与用户确认。
- 运行后报告输出文件路径和大小。
Probe codec first — determines whether
is safe:
bash
ffprobe -v quiet -show_streams "$INPUT" | grep codec_name
bash
ffprobe -v quiet -show_streams "$INPUT" | grep codec_name
Remux: same codec, different container (e.g., H.264 MKV → MP4) — instant, lossless:
Remux:编解码器相同,容器不同(如H.264 MKV → MP4)——即时、无损:
ffmpeg -i input.mkv -c copy output.mp4
ffmpeg -i input.mkv -c copy output.mp4
Re-encode: different codec required:
重新编码:需要转换编解码器:
ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
Fast trim without re-encoding (-ss before -i = container-level seek):
不重新编码的快速修剪(-ss放在-i之前=容器级定位):
ffmpeg -ss 00:01:30 -to 00:03:45 -i "$INPUT" -c copy "$OUTPUT"
ffmpeg -ss 00:01:30 -to 00:03:45 -i "$INPUT" -c copy "$OUTPUT"
Re-encoding trim (use when -c copy causes A/V sync issues near keyframes):
重新编码修剪(当使用-c copy导致关键帧附近音视频不同步时使用):
ffmpeg -ss 00:01:30 -to 00:03:45 -i "$INPUT" -c:v libx264 -crf 23 -c:a copy "$OUTPUT"
Always try `-c copy` first. Fall back to re-encoding only if the user reports sync issues or the cut must land on a non-keyframe boundary.
ffmpeg -ss 00:01:30 -to 00:03:45 -i "$INPUT" -c:v libx264 -crf 23 -c:a copy "$OUTPUT"
始终优先尝试`-c copy`。仅当用户反馈同步问题或必须在非关键帧边界裁剪时,才改用重新编码。
ffmpeg -i "$INPUT" -filter:v "setpts=0.5*PTS" -filter:a "atempo=2.0" "$OUTPUT"
ffmpeg -i "$INPUT" -filter:v "setpts=0.5*PTS" -filter:a "atempo=2.0" "$OUTPUT"
0.5x slow-motion:
0.5倍慢动作:
ffmpeg -i "$INPUT" -filter:v "setpts=2.0*PTS" -filter:a "atempo=0.5" "$OUTPUT"
`setpts` factor = `1 / speed_multiplier`. `atempo` range is 0.5–2.0 — chain for higher multiples:
`atempo=2.0,atempo=2.0` achieves 4x. For silent video, omit `-filter:a` entirely.
ffmpeg -i "$INPUT" -filter:v "setpts=2.0*PTS" -filter:a "atempo=0.5" "$OUTPUT"
`setpts`系数 = `1 / 速度倍数`。`atempo`范围是0.5–2.0——可链式调用实现更高倍数:`atempo=2.0,atempo=2.0`可实现4倍速。对于无声视频,完全省略`-filter:a`。
Probe dimensions first:
ffprobe -v quiet -show_streams "$INPUT" | grep -E "width|height"
先探测分辨率:
ffprobe -v quiet -show_streams "$INPUT" | grep -E "width|height"
Scale to width, auto height (-2 ensures H.264-compatible even height):
缩放到指定宽度,高度自动适配(-2确保高度为H.264兼容的偶数):
ffmpeg -i "$INPUT" -vf "scale=1280:-2" -c:a copy "$OUTPUT"
ffmpeg -i "$INPUT" -vf "scale=1280:-2" -c:a copy "$OUTPUT"
Fit within bounding box without upscaling:
在不放大的情况下适配边界框:
ffmpeg -i "$INPUT"
-vf "scale='min(1280,iw)':'min(720,ih)':force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2"
-c:a copy "$OUTPUT"
ffmpeg -i "$INPUT"
-vf "scale='min(1280,iw)':'min(720,ih)':force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2"
-c:a copy "$OUTPUT"
90° clockwise: ffmpeg -i "$INPUT" -vf "transpose=1" "$OUTPUT"
顺时针90°: ffmpeg -i "$INPUT" -vf "transpose=1" "$OUTPUT"
90° counter-clockwise: ffmpeg -i "$INPUT" -vf "transpose=2" "$OUTPUT"
逆时针90°: ffmpeg -i "$INPUT" -vf "transpose=2" "$OUTPUT"
180°: ffmpeg -i "$INPUT" -vf "transpose=1,transpose=1" "$OUTPUT"
180°: ffmpeg -i "$INPUT" -vf "transpose=1,transpose=1" "$OUTPUT"
Horizontal flip: ffmpeg -i "$INPUT" -vf "hflip" "$OUTPUT"
水平翻转: ffmpeg -i "$INPUT" -vf "hflip" "$OUTPUT"
Vertical flip: ffmpeg -i "$INPUT" -vf "vflip" "$OUTPUT"
垂直翻转: ffmpeg -i "$INPUT" -vf "vflip" "$OUTPUT"
Single frame at timestamp:
提取指定时间点的单帧:
ffmpeg -ss 00:00:10 -i "$INPUT" -frames:v 1 -q:v 2 output.jpg
ffmpeg -ss 00:00:10 -i "$INPUT" -frames:v 1 -q:v 2 output.jpg
One frame every N seconds:
每N秒提取一帧:
ffmpeg -i "$INPUT" -vf "fps=1/$N" -q:v 2 frames/frame_%04d.jpg
`-q:v 2` is near-maximum JPEG quality. Use `-q:v 1` for the highest quality setting.
ffmpeg -i "$INPUT" -vf "fps=1/$N" -q:v 2 frames/frame_%04d.jpg
`-q:v 2`接近最高JPEG质量。使用`-q:v 1`可获得最高质量设置。
- Never upscale — add
force_original_aspect_ratio=decrease
when fitting to a bounding box.
- For multi-filter operations, chain with commas in a single ; but and must remain separate flags.
- Use in all re-encode operations unless the audio format itself needs to change.
- If ffprobe reveals a metadata tag on the stream, use to bake rotation into pixels — preserves the metadata flag without rotating the actual frame data, which confuses many players.
- 绝不放大——适配边界框时添加
force_original_aspect_ratio=decrease
。
- 对于多滤镜操作,在单个中用逗号链接;但和必须保持为独立参数。
- 除非音频格式本身需要更改,否则在所有重新编码操作中使用。
- 如果ffprobe检测到流中存在元数据标签,使用将旋转效果嵌入像素——会保留元数据标签但不会实际旋转帧数据,这会导致许多播放器出现异常。