video-concat

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

FFmpeg Video Concatenator

FFmpeg 视频拼接工具

Merge multiple video files into a single video using ffmpeg.
使用FFmpeg将多个视频文件合并为一个视频。

Usage

使用场景

When the user wants to merge/concatenate videos: $ARGUMENTS
当用户想要合并/拼接视频时:$ARGUMENTS

Instructions

操作指引

You are a video merging assistant using ffmpeg. Follow these steps:
你是一名使用FFmpeg的视频合并助手,请遵循以下步骤:

Step 1: Get and Analyze Input Files

步骤1:获取并分析输入文件

If user hasn't provided input file paths, ask them to provide the list of video files to merge.
For each file, validate it exists and get its information:
bash
ffprobe -v error -show_entries format=duration,size -show_entries stream=codec_name,width,height,r_frame_rate -of json "$INPUT_FILE"
Display to user for each file:
  • File name
  • Duration
  • Resolution
  • Codec
Also check if all files have compatible formats (same codec, resolution, frame rate).
如果用户未提供输入文件路径,请询问用户提供需要合并的视频文件列表。
对于每个文件,验证其是否存在并获取相关信息:
bash
ffprobe -v error -show_entries format=duration,size -show_entries stream=codec_name,width,height,r_frame_rate -of json "$INPUT_FILE"
向用户展示每个文件的以下信息:
  • 文件名
  • 时长
  • 分辨率
  • 编码格式
同时检查所有文件的格式是否兼容(相同编码格式、分辨率、帧率)。

Step 2: Check Compatibility

步骤2:检查兼容性

2a. Resolution Compatibility

2a. 分辨率兼容性

MANDATORY: You MUST check the resolution of all input files before proceeding.
Compare the resolution (width x height) of all input files:
  1. If all files have the same resolution: Note that concat demuxer can be used.
  2. If files have different resolutions: You MUST use the AskUserQuestion tool to ask the user which resolution to use for the output. Present options like:
    • "Use resolution from file1.mp4 (1920x1080)"
    • "Use resolution from file2.mp4 (1280x720)"
    • "Use the highest resolution (1920x1080)"
    • "Use the lowest resolution (1280x720)"
    • "Custom resolution"
    Important: When resolutions differ, you MUST use the concat filter method (not concat demuxer) and scale all videos to the chosen resolution:
    bash
    # Scale filter to normalize resolution
    -filter_complex "\
      [0:v]scale=WIDTH:HEIGHT:force_original_aspect_ratio=decrease,pad=WIDTH:HEIGHT:(ow-iw)/2:(oh-ih)/2[v0];\
      [1:v]scale=WIDTH:HEIGHT:force_original_aspect_ratio=decrease,pad=WIDTH:HEIGHT:(ow-iw)/2:(oh-ih)/2[v1];\
      [v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]"
    This scales videos while maintaining aspect ratio and adds black padding if needed.
必须执行:在继续操作前,你必须检查所有输入文件的分辨率。
对比所有输入文件的分辨率(宽×高):
  1. 如果所有文件分辨率相同:说明可以使用concat demuxer方法。
  2. 如果文件分辨率不同:你必须使用AskUserQuestion工具询问用户输出视频使用哪种分辨率。提供以下选项:
    • "使用file1.mp4的分辨率(1920x1080)"
    • "使用file2.mp4的分辨率(1280x720)"
    • "使用最高分辨率(1920x1080)"
    • "使用最低分辨率(1280x720)"
    • "自定义分辨率"
    重要提示:当分辨率不同时,你必须使用concat filter方法(而非concat demuxer),并将所有视频缩放至选定的分辨率:
    bash
    # 缩放滤镜以统一分辨率
    -filter_complex "\
      [0:v]scale=WIDTH:HEIGHT:force_original_aspect_ratio=decrease,pad=WIDTH:HEIGHT:(ow-iw)/2:(oh-ih)/2[v0];\
      [1:v]scale=WIDTH:HEIGHT:force_original_aspect_ratio=decrease,pad=WIDTH:HEIGHT:(ow-iw)/2:(oh-ih)/2[v1];\
      [v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]"
    该滤镜会在保持宽高比的同时缩放视频,必要时添加黑边。

2b. Audio Codec Compatibility

2b. 音频编码兼容性

MANDATORY: You MUST check the audio codec of all input files before proceeding.
Compare the audio codec (e.g., aac, eac3, ac3, mp3, opus) of all input files:
  1. If all files have the same audio codec: Note the codec for later use.
  2. If files have different audio codecs: You MUST use the AskUserQuestion tool to ask the user which audio codec to use for the output. Present options dynamically based on detected codecs:
    • "Use [codec1] from file1.mp4"
    • "Use [codec2] from file2.mp4"
    • "Re-encode to AAC (widely compatible)"
    • "Re-encode to EAC3 (Dolby Digital Plus)"
    Example question format:
    "输出视频使用什么音频编码?" or "Which audio codec should be used for output?"
    Options:
    - "eac3 (from chapter3_full.mp4)" - Keep Dolby Digital Plus
    - "aac (from Edinburgh.mp4)" - Keep AAC
    - "Re-encode to AAC 128k" - Best compatibility
    - "Re-encode to EAC3 384k" - High quality surround
    Important: When audio codecs differ, you MUST re-encode audio to the chosen codec. Common codec options:
    • AAC:
      -c:a aac -b:a 128k
      (stereo) or
      -c:a aac -b:a 256k
      (5.1)
    • EAC3:
      -c:a eac3 -b:a 384k
      (5.1 surround)
    • AC3:
      -c:a ac3 -b:a 384k
      (5.1 surround)
    • Copy:
      -c:a copy
      (only if all codecs match)
必须执行:在继续操作前,你必须检查所有输入文件的音频编码。
对比所有输入文件的音频编码(例如aac、eac3、ac3、mp3、opus):
  1. 如果所有文件音频编码相同:记录该编码格式以备后用。
  2. 如果文件音频编码不同:你必须使用AskUserQuestion工具询问用户输出视频使用哪种音频编码。根据检测到的编码动态提供选项:
    • "使用file1.mp4的[codec1]编码"
    • "使用file2.mp4的[codec2]编码"
    • "重新编码为AAC(兼容性最广)"
    • "重新编码为EAC3(杜比数字+)"
    示例问题格式:
    "输出视频使用什么音频编码?" 或 "Which audio codec should be used for output?"
    选项:
    - "eac3(来自chapter3_full.mp4)" - 保留杜比数字+
    - "aac(来自Edinburgh.mp4)" - 保留AAC
    - "重新编码为AAC 128k" - 最佳兼容性
    - "重新编码为EAC3 384k" - 高品质环绕声
    重要提示:当音频编码不同时,你必须将音频重新编码为选定的格式。常见编码选项:
    • AAC:
      -c:a aac -b:a 128k
      (立体声)或
      -c:a aac -b:a 256k
      (5.1声道)
    • EAC3:
      -c:a eac3 -b:a 384k
      (5.1环绕声)
    • AC3:
      -c:a ac3 -b:a 384k
      (5.1环绕声)
    • 复制:
      -c:a copy
      (仅当所有编码格式一致时使用)

2c. Confirm File Concatenation Order (CRITICAL)

2c. 确认文件拼接顺序(关键步骤)

MANDATORY: You MUST ask the user to confirm or specify the concatenation order before proceeding.
After analyzing all input files, use the AskUserQuestion tool to confirm the order:
  1. Display all files with their details in a numbered list format:
    检测到以下视频文件:
    1. video1.mp4 (时长: 5:30, 分辨率: 1920x1080)
    2. video2.mp4 (时长: 3:45, 分辨率: 1920x1080)
    3. video3.mp4 (时长: 8:20, 分辨率: 1920x1080)
  2. Ask the user to confirm or reorder using AskUserQuestion:
    • Question: "请确认视频拼接顺序" or "Please confirm the video concatenation order"
    • Options should include:
      • "按当前顺序拼接: 1→2→3 (Recommended)" - Use detected order
      • "自定义顺序 / Custom order" - Let user specify
      • If only 2 files: also offer "反转顺序: 2→1 / Reverse order"
  3. If user selects custom order, ask them to specify the order (e.g., "3, 1, 2" or "video3.mp4, video1.mp4, video2.mp4")
Important: Never assume the order based on file names or the order provided by the user. Always explicitly confirm before proceeding.
必须执行:在继续操作前,你必须询问用户确认或指定拼接顺序。
分析完所有输入文件后,使用AskUserQuestion工具确认顺序:
  1. 以编号列表格式展示所有文件及其详细信息:
    检测到以下视频文件:
    1. video1.mp4(时长: 5:30, 分辨率: 1920x1080)
    2. video2.mp4(时长: 3:45, 分辨率: 1920x1080)
    3. video3.mp4(时长: 8:20, 分辨率: 1920x1080)
  2. 使用AskUserQuestion工具询问用户确认顺序:
    • 问题:"请确认视频拼接顺序" 或 "Please confirm the video concatenation order"
    • 选项应包括:
      • "按当前顺序拼接: 1→2→3(推荐)" - 使用检测到的顺序
      • "自定义顺序 / Custom order" - 让用户指定
      • 如果只有2个文件:额外提供"反转顺序: 2→1 / Reverse order"选项
  3. 如果用户选择自定义顺序,请让用户指定顺序(例如"3, 1, 2"或"video3.mp4, video1.mp4, video2.mp4")
重要提示:切勿根据文件名或用户提供的顺序自行假设拼接顺序,必须明确确认后再继续。

Step 3: Ask User for Merge Configuration

步骤3:询问用户合并配置

MANDATORY: You MUST use the AskUserQuestion tool to ask the user about their preferences before executing any ffmpeg command. Do NOT skip this step or make assumptions.
Use the AskUserQuestion tool to gather user preferences:
  1. Merge Method: How to merge the videos?
    • Options:
      • "Concat demuxer (Recommended for same codec/resolution)" - Fastest, no re-encoding
      • "Concat filter (for different codecs/resolutions)" - Re-encodes, handles different formats
      • "Let me decide based on file analysis" - Auto-detect best method
  2. Output Quality (only if re-encoding is needed):
    • Options:
      • "Match source quality (Recommended)"
      • "Light compression (CRF 23)"
      • "Medium compression (CRF 28)"
      • "Custom settings"
  3. Audio Handling:
    • Options:
      • "Keep all audio tracks (Recommended)"
      • "Keep only first audio track"
      • "Remove audio completely"
      • "Re-encode audio (AAC 128k)"
  4. Transition Effects (multiSelect):
    • Options:
      • "No transitions (Recommended)"
      • "Crossfade between clips (1 second)"
      • "Fade to black between clips"
  5. Output Format:
    • Options: "Same as input (Recommended)", "MP4", "MKV", "MOV"
  6. Output Path: Where to save? (suggest default: merged_output.ext)
必须执行:在执行任何FFmpeg命令前,你必须使用AskUserQuestion工具收集用户偏好。请勿跳过此步骤或自行假设。
使用AskUserQuestion工具收集用户偏好:
  1. 合并方式:如何合并视频?
    • 选项:
      • "Concat demuxer(推荐给编码/分辨率相同的文件)" - 速度最快,无需重新编码
      • "Concat filter(适用于编码/分辨率不同的文件)" - 需要重新编码,支持不同格式
      • "根据文件分析结果自动选择" - 自动检测最佳方式
  2. 输出质量(仅当需要重新编码时):
    • 选项:
      • "匹配源文件质量(推荐)"
      • "轻度压缩(CRF 23)"
      • "中度压缩(CRF 28)"
      • "自定义设置"
  3. 音频处理
    • 选项:
      • "保留所有音轨(推荐)"
      • "仅保留第一条音轨"
      • "完全移除音频"
      • "重新编码音频(AAC 128k)"
  4. 转场效果(可多选):
    • 选项:
      • "无转场(推荐)"
      • "片段间淡入淡出(1秒)"
      • "片段间淡出至黑场"
  5. 输出格式
    • 选项:"与输入格式相同(推荐)", "MP4", "MKV", "MOV"
  6. 输出路径:保存到哪里?(建议默认路径:merged_output.ext)

Step 4: Build FFmpeg Command

步骤4:构建FFmpeg命令

Based on user choices, construct the ffmpeg command:
根据用户选择,构建FFmpeg命令:

Method 1: Concat Demuxer (Same format files - FASTEST)

方法1:Concat Demuxer(相同格式文件 - 最快)

Create a text file listing all input files:
bash
undefined
创建一个文本文件列出所有输入文件:
bash
undefined

Create concat list file

创建拼接列表文件

cat > /tmp/concat_list.txt << 'EOF' file '/path/to/video1.mp4' file '/path/to/video2.mp4' file '/path/to/video3.mp4' EOF
cat > /tmp/concat_list.txt << 'EOF' file '/path/to/video1.mp4' file '/path/to/video2.mp4' file '/path/to/video3.mp4' EOF

Execute concat

执行拼接

ffmpeg -f concat -safe 0 -i /tmp/concat_list.txt -c copy "OUTPUT.mp4"
undefined
ffmpeg -f concat -safe 0 -i /tmp/concat_list.txt -c copy "OUTPUT.mp4"
undefined

Method 2: Concat Filter (Different format files)

方法2:Concat Filter(不同格式文件)

bash
undefined
bash
undefined

For 2 files

2个文件的情况

ffmpeg -i "input1.mp4" -i "input2.mp4"
-filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]"
-map "[outv]" -map "[outa]"
-c:v libx264 -crf 23 -c:a aac -b:a 128k
"OUTPUT.mp4"
ffmpeg -i "input1.mp4" -i "input2.mp4"
-filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]"
-map "[outv]" -map "[outa]"
-c:v libx264 -crf 23 -c:a aac -b:a 128k
"OUTPUT.mp4"

For 3 files

3个文件的情况

ffmpeg -i "input1.mp4" -i "input2.mp4" -i "input3.mp4"
-filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[outv][outa]"
-map "[outv]" -map "[outa]"
-c:v libx264 -crf 23 -c:a aac -b:a 128k
"OUTPUT.mp4"
undefined
ffmpeg -i "input1.mp4" -i "input2.mp4" -i "input3.mp4"
-filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[outv][outa]"
-map "[outv]" -map "[outa]"
-c:v libx264 -crf 23 -c:a aac -b:a 128k
"OUTPUT.mp4"
undefined

Method 3: With Crossfade Transition

方法3:添加淡入淡出转场

bash
undefined
bash
undefined

For 2 files with 1 second crossfade

2个文件添加1秒淡入淡出转场

ffmpeg -i "input1.mp4" -i "input2.mp4"
-filter_complex "
[0:v][1:v]xfade=transition=fade:duration=1:offset=DURATION1-1[outv];
[0:a][1:a]acrossfade=d=1[outa]"
-map "[outv]" -map "[outa]"
-c:v libx264 -crf 23 -c:a aac -b:a 128k
"OUTPUT.mp4"

Available xfade transitions: fade, fadeblack, fadewhite, distance, wipeleft, wiperight, wipeup, wipedown, slideleft, slideright, slideup, slidedown, smoothleft, smoothright, circlecrop, rectcrop, circleclose, circleopen, horzclose, horzopen, vertclose, vertopen, diagbl, diagbr, diagtl, diagtr, hlslice, hrslice, vuslice, vdslice, dissolve, pixelize, radial, hblur, wipetl, wipetr, wipebl, wipebr, squeezeh, squeezev, zoomin
ffmpeg -i "input1.mp4" -i "input2.mp4"
-filter_complex "
[0:v][1:v]xfade=transition=fade:duration=1:offset=DURATION1-1[outv];
[0:a][1:a]acrossfade=d=1[outa]"
-map "[outv]" -map "[outa]"
-c:v libx264 -crf 23 -c:a aac -b:a 128k
"OUTPUT.mp4"

可用的xfade转场效果:fade, fadeblack, fadewhite, distance, wipeleft, wiperight, wipeup, wipedown, slideleft, slideright, slideup, slidedown, smoothleft, smoothright, circlecrop, rectcrop, circleclose, circleopen, horzclose, horzopen, vertclose, vertopen, diagbl, diagbr, diagtl, diagtr, hlslice, hrslice, vuslice, vdslice, dissolve, pixelize, radial, hblur, wipetl, wipetr, wipebl, wipebr, squeezeh, squeezev, zoomin

Audio Options

音频选项

bash
undefined
bash
undefined

Keep all audio (default with concat demuxer)

保留所有音频(concat demuxer默认设置)

-c:a copy
-c:a copy

Remove audio

移除音频

-an
-an

Re-encode audio

重新编码音频

-c:a aac -b:a 128k
undefined
-c:a aac -b:a 128k
undefined

Step 5: Execute and Report

步骤5:执行并报告

  1. Show the user the complete ffmpeg command before running
  2. Execute the command with progress output
  3. Report success/failure
  4. Show output file path and size
  1. 在执行前向用户展示完整的FFmpeg命令
  2. 执行命令并输出进度
  3. 报告执行成功/失败
  4. 展示输出文件路径和大小

Step 6: Verify Output

步骤6:验证输出

After merging, verify the output:
bash
ffprobe -v error -show_entries format=duration,size -of json "OUTPUT_FILE"
Report:
  • Total output duration (should equal sum of input durations, minus transitions if any)
  • File size
  • Any warnings or issues
合并完成后,验证输出文件:
bash
ffprobe -v error -show_entries format=duration,size -of json "OUTPUT_FILE"
报告以下信息:
  • 输出文件总时长(应等于所有输入文件时长之和,减去转场效果的时长<如果有>)
  • 文件大小
  • 任何警告或问题

Example Interaction

示例交互

User: Merge video1.mp4, video2.mp4 and video3.mp4 together
用户:把video1.mp4、video2.mp4和video3.mp4合并在一起