How to join two images into one with FFmpeg? [duplicate]
Asked Answered
I

3

19

There are two images: a.jpg and b.jpg.

I just want to know how to join them into one image using ffmpeg.

How should I finish the ffmpeg -i a.jpg -i b.jpg command to get a c.jpg output image?

This is an example of what I am trying to achieve:

  1. a.jpg

    a.jpg

  2. b.jpg

    b.jpg

  3. c.jpg

    a.jpg and b.jpg side-by-side on one image

Impenetrability answered 7/7, 2014 at 7:1 Comment(1)
You might consider changing the accepted answer since the current one is invalid.Adversity
L
-5

I think that tile is your ffmpeg command.

You can find more info on Superuser.

Try:

ffmpeg -i a.jpg -i b.jpg -filter_complex scale=120:-1,tile=2x1 output.jpg
Leopoldeen answered 9/7, 2014 at 9:34 Comment(2)
I get a black box instead of the b.jpg in the output.jpg when I use your command.Adversity
This method is invalid for joining images. Here's why: superuser.com/a/1091070/442991Adversity
P
43

Use the hstack filter.

2 images:

ffmpeg -i a.jpg -i b.jpg -filter_complex hstack output.jpg

3 images:

ffmpeg -i a.jpg -i b.jpg -i c.jpg -filter_complex "[0][1][2]hstack=inputs=3" output.jpg
Phalansterian answered 19/6, 2016 at 3:13 Comment(4)
This works, if we have two files with same widthXheight, but what if we have two images with different widthXheight, how do we scale it and join them ??Pasargadae
@JitendarM See scale example in Vertically or horizontally stack several videos using ffmpeg?Phalansterian
thanks, will look into it.Pasargadae
I tried using this method for merging three images, but apparently, we need to make it in groups of two. It was enough for me, but do you know if there is a way to merge more than two images at once? EDIT: just found out the answer to this in the thread that is mentioned at the beginning of this post #11553065Irresponsible
A
6

Watch out! Dirty hacks ahead!

Pay attention to the LordNeckbeard's answer since it is much better than the hacks of mine.


Álvaro's answer didn't work for me so I did more research to solve the issue. This is what I've learnt:

I am going to use the following variables.

A_HEIGHT=458
A_WIDTH=370
B_HEIGHT=600
B_WIDTH=750
B_CROP_X=112
B_CROP_Y=0
A_IMAGE=a.jpg
B_IMAGE=b.jpg
B_IMAGE_SCALED=b-scaled.png
B_IMAGE_CROPPED=b-cropped.png
C_IMAGE_WITHOUT_A=c-without-a.png
C_IMAGE=c.png

Scale and crop

In case your images are not of the same width and height.

This script scales B to be of the size as A.

# Scale.
ffmpeg -y -i ${B_IMAGE} -vf \
  scale=${A_HEIGHT}*${B_WIDTH}/${B_HEIGHT}:${A_HEIGHT} ${B_IMAGE_SCALED}

# Crop.
ffmpeg -y -i ${B_IMAGE_SCALED} -vf \
  "crop=${A_WIDTH}:${A_HEIGHT}:${B_CROP_X}:${B_CROP_Y}" ${B_IMAGE_CROPPED}

Merge / join two images

ffmpeg -y -i ${B_IMAGE_CROPPED} -filter_complex tile=2x1 ${C_IMAGE_WITHOUT_A}

Now the C_IMAGE_WITHOUT_A lacks A; there is a black background instead. (You'll should get something similar to the picture below.)

Example

I have found a way to join those two images nevertheless:

ffmpeg -y -i ${C_IMAGE_WITHOUT_A} -i ${A_IMAGE} \
  -filter_complex overlay=${A_WIDTH}:0 ${C_IMAGE}
Adversity answered 18/6, 2016 at 20:50 Comment(0)
L
-5

I think that tile is your ffmpeg command.

You can find more info on Superuser.

Try:

ffmpeg -i a.jpg -i b.jpg -filter_complex scale=120:-1,tile=2x1 output.jpg
Leopoldeen answered 9/7, 2014 at 9:34 Comment(2)
I get a black box instead of the b.jpg in the output.jpg when I use your command.Adversity
This method is invalid for joining images. Here's why: superuser.com/a/1091070/442991Adversity

© 2022 - 2024 — McMap. All rights reserved.