Concatenate Videos with Different Pixel Formats in FFmpeg
Concatenating videos with different pixel formats in FFmpeg often leads to errors or playback issues because the output stream requires a unified format. This guide provides a straightforward FFmpeg command and explanation to reformat and merge your videos seamlessly using the complex filtergraph.
The Solution: FFmpeg Filter_Complex Command
When video files have different pixel formats (such as
yuv422p and yuv420p), you cannot use the fast
“concat demuxer” without re-encoding. Instead, you must use the
filter_complex option to convert the pixel formats to a
common standard (usually yuv420p for maximum compatibility)
before merging them.
Use the following command to concatenate two videos with different pixel formats:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex \
"[0:v]format=yuv420p[v0]; \
[1:v]format=yuv420p[v1]; \
[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mp4How the Command Works
-i video1.mp4 -i video2.mp4: Specifies the two input video files.[0:v]format=yuv420p[v0]: Takes the video stream of the first input (0:v), converts its pixel format toyuv420p, and labels the resulting stream as[v0].[1:v]format=yuv420p[v1]: Takes the video stream of the second input (1:v), converts its pixel format toyuv420p, and labels the resulting stream as[v1].concat=n=2:v=1:a=1: Concatenates the processed video streams and their original audio streams.n=2indicates the number of input segments (files).v=1tells FFmpeg to output one video stream.a=1tells FFmpeg to output one audio stream.
[outv][outa]: The labels assigned to the final concatenated video and audio streams.-map "[outv]" -map "[outa]": Instructs FFmpeg to write these specific merged streams into the finaloutput.mp4file.
Handling Different Resolutions (Optional)
If your videos also have different resolutions in addition to
different pixel formats, you must scale them to match. You can chain the
scale and format filters together inside the
same command:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex \
"[0:v]scale=1920:1080,format=yuv420p[v0]; \
[1:v]scale=1920:1080,format=yuv420p[v1]; \
[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mp4This ensures both videos are scaled to 1920x1080 and converted to the
yuv420p pixel format before concatenation, preventing any
rendering or playback errors.