FFmpeg Concatenate Videos with Different Aspect Ratios
This article provides a step-by-step guide and the exact FFmpeg command needed to concatenate two videos with different aspect ratios. You will learn how to use FFmpeg’s complex filter graph to scale, pad, and merge your videos into a single output file without stretching or distorting the original footage.
The Challenge with Different Aspect Ratios
When concatenating videos with different aspect ratios or resolutions using the basic FFmpeg demuxer, the resulting file will often suffer from severe distortion, green bars, or playback errors. To merge them successfully, both videos must be normalized to the same resolution and aspect ratio before they are joined.
The most effective way to achieve this is by using FFmpeg’s
-filter_complex flag to scale and pad the videos to a
uniform resolution (such as 1920x1080) and then concatenate them.
The FFmpeg Command
Run the following command in your terminal to concatenate two videos with different aspect ratios:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex \
"[0:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1[v0]; \
[1:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1[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
Here is a breakdown of the specific filters used in the command:
-i input1.mp4 -i input2.mp4: Specifies the two input video files.scale=1920:1080:force_original_aspect_ratio=decrease: Scales the video to fit within a 1920x1080 bounding box. Thedecreaseoption ensures the video is shrunk proportionally without stretching, preserving its original aspect ratio.pad=1920:1080:(ow-iw)/2:(oh-ih)/2: Adds black bars (letterboxing or pillarboxing) to fill the remaining space of the 1920x1080 canvas. The math(ow-iw)/2and(oh-ih)/2centers the video horizontally and vertically.setsar=1: Sets the Sample Aspect Ratio to 1:1, ensuring square pixels for consistent playback across devices.[v0]and[v1]: Temporary labels assigned to the processed video streams.concat=n=2:v=1:a=1: Concatenates the processed video streams ([v0],[v1]) and their respective audio streams ([0:a],[1:a]).n=2indicates the number of input segments,v=1outputs one video stream, anda=1outputs one audio stream.-map "[outv]" -map "[outa]": Maps the final concatenated video and audio outputs to the final container file,output.mp4.