This article explains how to merge multiple video sources into a single file and overlay a logo using FFmpeg. You will learn the specific filter chains required for complex input handling, ensuring precise synchronization and visual branding without format errors or unnecessary bloat.
Understanding the Filtergraph Structure
When combining inputs in FFmpeg, you must define a filtergraph that explicitly labels each stream. This allows the software to reference specific parts of your source files during processing. Without labeled streams, FFmpeg cannot distinguish between audio tracks from different videos or apply overlays correctly. The core concept relies on the
-filter_complex option, which handles multi-input operations that standard filters cannot manage alone.
Each input file is assigned an index starting from zero. For example, if you provide two video files, they are referenced as
[0:v] and
[1:v]. Audio streams follow the same pattern with
[0:a]. By assigning these labels, you create a map that the subsequent filter instructions can manipulate. This labeling system is critical when dealing with videos of different resolutions or frame rates, as it ensures that every operation targets the correct data source.
Merging Video Streams Side-by-Side
A common requirement is placing two videos next to each other, often called a split-screen view. To achieve this, you first need to ensure both videos share the same resolution. If one video is 1920x1080 and the other is 1280x720, you must scale them to a common height or width before merging. The
scale filter handles this resizing. For instance, you might scale both videos to a height of 1080 pixels while maintaining their aspect ratios.
Once scaled, you use the
hstack (horizontal stack) or
vstack (vertical stack) filter. The
hstack filter takes two inputs and outputs a single video frame where the left side is the first input and the right side is the second. The command structure looks like this:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=1920:1080[vid1]; [1:v]scale=1920:1080[vid2]; [vid1][vid2]hstack" output.mp4
In this example,
[vid1] and
[vid2] are temporary labels created by the scaling operations. These labels are then passed into the
hstack filter. It is important to note that if your videos have different durations, the output will be truncated to the length of the shortest video unless you pad the shorter one. Padding involves adding black frames to the end of the shorter clip so that both streams last equally long.
Adding a Logo Overlay
After merging your videos, you may want to add a static logo watermark. This requires loading a third input, typically an image file such as PNG or JPEG, which supports transparency. The alpha channel in the image allows the background to show through, making the logo appear as if it is floating on the video.
You load the logo using another input flag, for example
-i logo.png. In the filter graph, this becomes
[2:v]. To position the logo, you use the
overlay filter. This filter takes the main video stream and places the logo stream on top of it at specified coordinates. The coordinates are measured in pixels from the top-left corner of the video frame.
For a logo in the bottom-right corner with a 10-pixel margin, you would calculate the position based on the video width. If the video is 1920 pixels wide, the X coordinate would be
W-w-10, where
W is the main video width and
w is the logo width. The Y coordinate would be
H-h-10. The complete filter chain combines the stacked video with the overlay:
ffmpeg -i input1.mp4 -i input2.mp4 -i logo.png -filter_complex "[0:v]scale=1920:1080[vid1]; [1:v]scale=1920:1080[vid2]; [vid1][vid2]hstack[merged]; [merged][2:v]overlay=W-w-10:H-h-10" output.mp4
This command processes the inputs in order. First, it scales and stacks the two videos into
[merged]. Then, it overlays the logo onto the merged result. The final output contains both combined videos and the branded logo.
Handling Audio Synchronization
Video merging often overlooks audio tracks. If your source videos contain audio, you must decide whether to keep one, mix them, or discard them entirely. FFmpeg does not automatically merge audio streams in the same way it stacks video. You typically need to select one audio track or use the
amix filter to combine them.
If you choose to keep the audio from the first video only, you can map it explicitly. Using the
-map option ensures that the correct audio stream is included in the final file. For example,
-map 0:a selects the audio from the first input. If you want to mix audio from both videos, you must route both audio streams through the
amix filter in your complex filter graph. Failure to map audio correctly results in silent output or mismatched timing.
It is also advisable to check the duration of your audio tracks. If one video has a much longer audio track than the video content, the excess audio may cause playback issues in some players. Trimming the audio to match the video duration is a good practice for consistency.
Optimizing Output Quality
The quality of your final file depends heavily on the encoding settings you choose. FFmpeg uses default codecs that may not be optimal for all scenarios. For H.264 video, setting the preset to
fast or
medium balances speed and quality. You should also specify a constant rate factor (
-crf) to control visual fidelity. A CRF value between 18 and 23 is generally recommended for high-quality output.
Additionally, consider the pixel format. Older players may struggle with modern formats like
yuv420p10le. Using
-pix_fmt yuv420p ensures maximum compatibility across devices and browsers. This simple flag prevents color shifting or playback failures on older hardware.
For more details on resolution choices, see
Understanding the Difference Between 240p and 1080p Video Conversions. This resource helps you decide if upscaling or downscaling is necessary for your specific branding needs.
Frequently Asked Questions
What happens if my videos have different frame rates?
FFmpeg will attempt to convert the frame rates to match the output, but this can cause stuttering or dropped frames. It is best to resample all inputs to a common frame rate using the
fps filter before stacking or overlaying.
Can I animate the logo position?
Yes, you can use expressions in the
overlay filter coordinates. For example, using
x='if(gte(t,5),W-w-10,0)' moves the logo to the right after five seconds. This allows for dynamic branding effects without re-encoding the entire video separately.
Why is my audio out of sync after merging?
Audio desync usually occurs when video and audio streams have different start times or durations. Ensure you trim both video and audio to the exact same length before merging. Explicitly mapping audio streams with
-map also helps maintain synchronization.
Does the logo reduce video quality?
No, adding a logo is a lossless operation regarding the underlying video data. The logo is overlaid during the encoding process, meaning the original video pixels remain intact beneath the image. However, the overall file size may increase slightly due to the additional data.
How do I handle transparent backgrounds in logos?
You must use a format that supports alpha channels, such as PNG or WebP. JPEG images do not support transparency and will display a solid white background instead. Ensure your logo file is saved with transparency enabled before importing it into FFmpeg.