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

╄→尐↘猪︶ㄣ 提交于 2020-06-10 04:00:28

问题


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


回答1:


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



回答2:


Use the hstack filter:

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

If you want to vertically stack use vstack instead.




回答3:


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}


来源:https://stackoverflow.com/questions/24604689/how-to-join-two-images-into-one-with-ffmpeg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!