convert-video

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Video Convert

视频转换

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调用中链接所有滤镜——避免生成中间文件。

Process

处理流程

  1. Identify the operation(s) from the user's request.
  2. Probe codec when converting formats; probe dimensions when resizing. Speed, rotation, flip, and frame extraction do not require probing.
  3. Construct the command using the appropriate pattern.
  4. Confirm with the user before running.
  5. Run and report output file path and size.
  1. 从用户请求中识别操作类型。
  2. 转换格式时探测编解码器;调整尺寸时探测分辨率。调速、旋转、翻转和帧提取无需探测。
  3. 使用合适的模板构造命令。
  4. 运行前与用户确认。
  5. 运行后报告输出文件路径和大小。

Operation Patterns

操作模板

Format Conversion

格式转换

Probe codec first — determines whether
-c copy
is safe:
bash
ffprobe -v quiet -show_streams "$INPUT" | grep codec_name
bash
undefined
先探测编解码器——以此判断
-c copy
是否安全:
bash
ffprobe -v quiet -show_streams "$INPUT" | grep codec_name
bash
undefined

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
undefined
ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
undefined

Trimming

修剪

bash
undefined
bash
undefined

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`。仅当用户反馈同步问题或必须在非关键帧边界裁剪时,才改用重新编码。

Speed Change

调速

bash
undefined
bash
undefined

2x speed:

2倍速:

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`。

Resize / Scale

调整尺寸/缩放

Probe dimensions first:
ffprobe -v quiet -show_streams "$INPUT" | grep -E "width|height"
bash
undefined
先探测分辨率:
ffprobe -v quiet -show_streams "$INPUT" | grep -E "width|height"
bash
undefined

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"
undefined
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"
undefined

Rotation / Flip

旋转/翻转

bash
undefined
bash
undefined

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"

undefined
undefined

Frame Extraction

帧提取

bash
undefined
bash
undefined

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`可获得最高质量设置。

Key Decisions

关键决策

  • Never upscale — add
    force_original_aspect_ratio=decrease
    when fitting to a bounding box.
  • For multi-filter operations, chain with commas in a single
    -vf
    ; but
    -filter:v
    and
    -filter:a
    must remain separate flags.
  • Use
    -c:a copy
    in all re-encode operations unless the audio format itself needs to change.
  • If ffprobe reveals a
    rotate
    metadata tag on the stream, use
    -vf "transpose=..."
    to bake rotation into pixels —
    -c copy
    preserves the metadata flag without rotating the actual frame data, which confuses many players.
  • 绝不放大——适配边界框时添加
    force_original_aspect_ratio=decrease
  • 对于多滤镜操作,在单个
    -vf
    中用逗号链接;但
    -filter:v
    -filter:a
    必须保持为独立参数。
  • 除非音频格式本身需要更改,否则在所有重新编码操作中使用
    -c:a copy
  • 如果ffprobe检测到流中存在
    rotate
    元数据标签,使用
    -vf "transpose=..."
    将旋转效果嵌入像素——
    -c copy
    会保留元数据标签但不会实际旋转帧数据,这会导致许多播放器出现异常。