Fix FFmpeg Concat Audio Drop and Timebase Mismatch
Stream specifier ':a' in filtergraph description matches no streams | Non-monotonic DTS in output stream | concat demuxer output freezes or plays 2x–3x fast after clip 1 Stitching clips from mixed AI video generators—such as voiced Google Gemini Omni 1.2 Flash or Kling 4.0 shots alongside silent Runway Gen-4 / Gen-4.5 or local Wan 2.2 renders—either crashes FFmpeg immediately with a missing audio stream error, silently drops audio after the first cut, or freezes and speeds up video segments mid-playback.
FFmpeg concat demuxer (-c copy) blindly splices raw packet timestamps without re-encoding, so mismatched container timebases (1/12288 vs 1/15360 vs 1/24000 vs 1/90000), differing frame rates (16 fps vs 24 fps vs 30 fps), or mixed silent-and-voiced clips corrupt DTS/PTS math immediately across the edit boundary.
Copy-Paste Verified Fix
#!/usr/bin/env bash
set -euo pipefail
# Usage: ./safe-conform-concat.sh out_master.mp4 clip1.mp4 clip2.mp4 clip3.mp4
OUT_FILE="$1"
shift
TARGET_W="${TARGET_W:-1920}"
TARGET_H="${TARGET_H:-1080}"
TARGET_FPS="${TARGET_FPS:-24}"
TIMESCALE="${TIMESCALE:-24000}"
WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT
LIST_FILE="$WORKDIR/concat_list.txt"
: > "$LIST_FILE"
idx=0
for src in "$@"; do
idx=$((idx + 1))
norm="$(printf '%s/clip_%03d.mp4' "$WORKDIR" "$idx")"
has_audio="$(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$src" || true)"
vf_chain="scale=${TARGET_W}:${TARGET_H}:force_original_aspect_ratio=decrease,pad=${TARGET_W}:${TARGET_H}:(ow-iw)/2:(oh-ih)/2:black,setsar=1,fps=${TARGET_FPS}"
if [ -n "$has_audio" ]; then
ffmpeg -y -v error -i "$src" \
-map 0:v:0 -map 0:a:0 \
-vf "$vf_chain" -fps_mode cfr -video_track_timescale "$TIMESCALE" \
-c:v libx264 -profile:v high -level 4.1 -crf 17 -pix_fmt yuv420p \
-colorspace bt709 -color_primaries bt709 -color_trc bt709 -color_range tv \
-af "aresample=48000:async=1:first_pts=0,aformat=channel_layouts=stereo" \
-c:a aac -b:a 192k -ar 48000 "$norm"
else
ffmpeg -y -v error -i "$src" -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000 \
-map 0:v:0 -map 1:a:0 \
-vf "$vf_chain" -fps_mode cfr -video_track_timescale "$TIMESCALE" \
-c:v libx264 -profile:v high -level 4.1 -crf 17 -pix_fmt yuv420p \
-colorspace bt709 -color_primaries bt709 -color_trc bt709 -color_range tv \
-c:a aac -b:a 192k -ar 48000 -shortest "$norm"
fi
printf "file '%s'\n" "$norm" >> "$LIST_FILE"
done
ffmpeg -y -v error -f concat -safe 0 -i "$LIST_FILE" -c copy \
-colorspace bt709 -color_primaries bt709 -color_trc bt709 -color_range tv \
-movflags +faststart "$OUT_FILE"
echo "Conformed & concatenated ${idx} clips -> ${OUT_FILE}" Before vs. After (Broken Pattern vs. Verified Fix)
# list.txt mixes 24fps voiced Gemini Omni Flash clip (1/24000) + 16fps silent Wan clip (1/16384)
ffmpeg -f concat -safe 0 -i list.txt -c copy broken_edit.mp4
# Result: Non-monotonic DTS warnings, frozen frames after cut 1, and dropped audio # Conform each clip to 1920x1080 24fps, timescale 24000, 48kHz stereo (injecting anullsrc if silent):
ffmpeg -y -i silent_broll.mp4 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000 \
-map 0:v:0 -map 1:a:0 \
-vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=24" \
-video_track_timescale 24000 -c:v libx264 -crf 18 -pix_fmt yuv420p \
-c:a aac -b:a 192k -ar 48000 -shortest norm_02.mp4
# Now stream-copy concat succeeds with zero DTS drift:
ffmpeg -y -f concat -safe 0 -i conformed_list.txt -c copy -movflags +faststart master_concat.mp4 Why this works: Locking -video_track_timescale 24000, fps=24, setsar=1, and uniform 48 kHz stereo AAC on every intermediate guarantees identical headers and packet clocks across every splice point.
Why This Failure Happens in Real Pipelines
Every MP4 container stores frame timestamps as integer ticks multiplied by a stream timebase (time_base). One AI vendor exports 24 fps clips at time_base=1/12288, another uses 1/24000, a screen recorder writes 1/90000, and an open-weight Wan 2.2 export ships at 16 fps with 1/16384. When you join those files via ffmpeg -f concat -safe 0 -i list.txt -c copy, FFmpeg inherits clip 1’s timebase header and copies clip 2’s raw integer PTS ticks straight onto the wrong clock—making clip 2 play in slow motion, turbo-speed, or stall on a frozen frame while audio keeps advancing.
Audio stream presence creates a second failure boundary. The concat demuxer expects identical stream layouts across every listed file: if clip 1 contains 48 kHz stereo AAC and clip 2 has zero audio tracks (standard on silent B-roll or PNG end-cards), muxing drops audio for the remainder of the timeline or produces negative DTS drift. Similarly, the concat=n=3:v=1:a=1 filtergraph aborts before frame 1 with Stream specifier :a matches no streams unless every silent input receives synthetic silence.
Attempting to fix mixed clips with ad-hoc -fflags +genpts flags treats the symptom without harmonizing resolution, pixel aspect ratio (SAR), pixel format (yuv420p), or sample rate (44.1 kHz vs 48 kHz). The only deterministic studio fix is a two-stage conform-then-concat pipeline: inspect each source clip with ffprobe, normalize every clip to identical width/height, setsar=1, constant 24 or 30 fps, an explicit -video_track_timescale 24000, yuv420p, and 48 kHz stereo AAC (injecting anullsrc silence whenever audio is absent), and only then join the conformed intermediates with -f concat -c copy.
Step-by-Step Resolution & Verification
- 01
Audit every source clip for timebase, FPS, and missing audio streams
Run ffprobe across all input files checking r_frame_rate, time_base, width, height, and audio stream presence. Any divergence across clips makes raw -f concat -c copy unsafe.
- 02
Conform every clip to a single timescale and inject silent 48 kHz audio where missing
Use the safe-conform-concat.sh script so silent clips receive a matching 48 kHz stereo anullsrc track clipped with -shortest, while voiced clips resample via aresample=48000:async=1:first_pts=0 and every video track locks -video_track_timescale 24000.
- 03
Concatenate conformed intermediates and compare video vs audio duration
Join the normalized clips with -f concat -safe 0 -c copy -movflags +faststart, then confirm with ffprobe that the final video and audio durations match within one AAC frame (~21 ms).
ffprobe -v error -show_entries stream=codec_type,codec_name,width,height,r_frame_rate,time_base,sample_rate,duration -of compact=p=0 out_master.mp4 codec_name=h264|codec_type=video|width=1920|height=1080|r_frame_rate=24/1|time_base=1/24000|duration=12.000000
codec_name=aac|codec_type=audio|sample_rate=48000|r_frame_rate=0/0|time_base=1/48000|duration=12.021333 How to Prevent This Error From Recurring
Always lock -video_track_timescale on studio intermediates
Setting -video_track_timescale 24000 (for 24 fps) or 30000 (for 30 fps) eliminates random container timebases like 1/12288 or 1/15360 across mixed AI APIs.
Require an audio stream on every B-roll and PNG end-card clip
Generate silent 48 kHz stereo PCM/AAC tracks during ingest so downstream concat scripts, crossfade filters, and NLE timelines never hit missing-stream errors.
Use PCM WAV inside intermediate MOV/MKV when chaining multiple re-encodes
Because AAC adds ~21 ms encoder priming frames per encode pass, use lossless pcm_s16le audio in intermediate clips if you plan to run multi-stage concat plus loudnorm mastering.
Troubleshooting FAQs
- Why does ffmpeg -f concat -c copy work on some clips and randomly break on others?
- The concat demuxer copies compressed packets verbatim and assumes every file shares the exact same codec profile, resolution, SAR, pixel format, stream count, and MP4 timebase. Two clips from the same camera or API often match by luck, whereas mixing Gemini Omni 1.2 Flash, Kling 4.0, and PNG end-cards breaks those invariants.
- Why did my concatenated MP4 have tiny audio clicks at every cut boundary?
- AAC encodes audio in 1024-sample frames (~21.3 ms at 48 kHz) with priming padding. Splicing already-compressed AAC clips with -c copy can leave micro-gaps at cut points; switching intermediates to uncompressed pcm_s16le inside .mov or re-encoding the final joined audio track once removes every click.
- When should I use the concat filtergraph instead of the concat demuxer?
- Use the concat filtergraph (-filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]") when joining 2–5 short clips in a single pass with transitions like xfade/acrossfade. For 6+ clips or batch pipelines, pre-conforming intermediates and using the concat demuxer uses far less RAM and debugs much faster.
- What timescale should I choose for 30 fps or 29.97 fps projects?
- Use -video_track_timescale 30000 for 30 fps or 29.97 fps (30000/1001), -video_track_timescale 24000 for 24 fps or 23.976 fps, and -video_track_timescale 60000 for 60 fps timelines. Every frame lands on an exact integer tick with zero fractional rounding.
- Why does -safe 0 exist in the concat demuxer command?
- By default FFmpeg rejects absolute paths or parent-directory references inside concat list files for security. Passing -safe 0 immediately after -f concat lets your script reference normalized clips stored inside temporary working directories safely.