17

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

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
n2v2rda2
  • 327
  • 2
  • 5
  • 15

3 Answers3

40

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
llogan
  • 121,796
  • 28
  • 232
  • 243
  • 1
    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 ?? – Jitendar M Jun 04 '18 at 13:05
  • 2
    @JitendarM See scale example in [Vertically or horizontally stack several videos using ffmpeg?](https://stackoverflow.com/a/33764934/1109017) – llogan Jun 04 '18 at 19:46
  • thanks, will look into it. – Jitendar M Jun 05 '18 at 06:56
  • 2
    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 https://stackoverflow.com/questions/11552565/vertically-or-horizontally-stack-several-videos-using-ffmpeg – João Quintas Jun 11 '18 at 11:35
5

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}
Community
  • 1
  • 1
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
-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
Community
  • 1
  • 1
Álvaro
  • 2,872
  • 4
  • 20
  • 25