Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Last active December 25, 2022 09:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joshschmelzle/fc96024dbb14f3f919893fdd14e7dffc to your computer and use it in GitHub Desktop.
Save joshschmelzle/fc96024dbb14f3f919893fdd14e7dffc to your computer and use it in GitHub Desktop.
Merging two video clips into one and placing them next to each other with ffmpeg

Taking two video clips, placing them side by side, combining 4 channels of audio into 2, and output.

ffmpeg -i video1.MP4 -i video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]; [0:a][1:a]amerge[a]" -map "[v]" -map "[a]" -ac 2 output.mp4

This was done with two filters and the audio from both inputs.

  • hstack places each video side-by-side.
  • amerge combines the audio from both inputs into a single, multi-channel audio stream, and -ac 2 will make it stereo (without this option the audio stream may end up as 4 channels if both inputs are stereo.)
@ucalyptus2
Copy link

@joshschmelzle thanks for this, but how to combine videos with different sizes?
I get the error

[Parsed_amerge_2 @ 0x55dd77800820] No channel layout for input 1
[Parsed_amerge_2 @ 0x55dd77800820] Input channel layouts overlap: output layout will be determined by the number of distinct input channels
[Parsed_hstack_0 @ 0x55dd77801860] Input 1 height 1080 does not match input 0 height 270.
[Parsed_hstack_0 @ 0x55dd77801860] Failed to configure output pad on Parsed_hstack_0
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:1
Conversion failed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment