使用 ffmpeg 在静态背景图像上叠加具有透明度的动画图像? [英] Overlay animated images with transparency over a static background image using ffmpeg?

查看:213
本文介绍了使用 ffmpeg 在静态背景图像上叠加具有透明度的动画图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用一组 png 图像创建一个视频,这些 png 图像具有与静态背景合并的透明度.

经过大量挖掘后,我似乎可以通过使用过滤器库来实现.

我最初制作的不包括背景的视频是:

ffmpeg -y -qscale 1 -r 1 -b 9600 -loop -i bg.png -i frame_%d.png -s hd720 testvid.mp4

使用 -vf 我可以将背景应用为叠加:

ffmpeg -y -qscale 1 -r 1 -b 9600 -i frame_%d.png -vf "movie=bg.png [wm];[in][wm] overlay=0:0 [out]" -s hd720 testvid.mp4

然而,问题是它在输入上覆盖了背景.根据 分解为帧:

convert dancebanana.gif -define png:color-type=6 over.png

(设置 png:color-type=6 (RGB-Matte) 至关重要,因为 ffmpeg 不能正确处理索引透明度.)输入被命名为 over-0.pngover-1.pngover-2.png

我们的背景图片(缩放到香蕉):

使用 ffmpeg 版本 N-40511-g66337bf(git build 来自昨天),我们这样做:

ffmpeg -loop 1 -i bg.png -r 5 -i over-%d.png -filter_complex overlay -shortest out.avi

-loop 循环输入背景图像,这样我们就不会只有一帧,这很重要!
-r 稍微减慢跳舞的香蕉的速度,可选.
-filter_complex 是最近添加的 ffmpeg 功能,可以更轻松地处理多个输入.
-shortest 在最短输入结束时结束编码,这是必要的,因为循环背景意味着该输入永远不会结束.

使用稍微不那么前沿的构建,ffmpeg version 0.10.2.git-d3d5e84:

ffmpeg -loop 1 -r 5 -i back.png -vf 'movie=over-%d.png [over], [in][over] overlay' -frames:v 8 out.avi

movie 不允许设置速率,因此我们减慢背景速度,但效果相同.因为叠加的电影不是正确的输入,我们不能使用 -shortest 而是明确地将要输出的帧数设置为我们有多少叠加的输入帧.

最终结果(输出为用于嵌入的 gif):

I'm looking to create a video using a set of png images that have transparency merged with a static background.

After doing a lot of digging I seems like it's definitely possible by using the filters library.

My initial video making without including the background is:

ffmpeg -y -qscale 1 -r 1 -b 9600 -loop -i bg.png -i frame_%d.png -s hd720 testvid.mp4

Using -vf I can apply the background as overlay:

ffmpeg -y -qscale 1 -r 1 -b 9600 -i frame_%d.png -vf "movie=bg.png [wm];[in][wm] overlay=0:0 [out]" -s hd720 testvid.mp4

However the problem is it's overlaying the background over the input. According libacfilter I can split the input and play with it's content. I'm wondering if I can somehow change the overlay order?

Any help greatly appreciated!

UPDATE 1:
I'm trying to make the following filter work but I'm getting the movie without the background:

ffmpeg -y -qscale 1 -r 1 -b 9600 -i frame_%d.png -vf "movie=bg.png [bg]; [in] split [T1], fifo, [bg] overlay=0:0, [T2] overlay=0:0 [out]; [T1] fifo [T2]" -s hd720 testvid.mp4

UPDATE 2:
Got video making using -vf option. Just piped the input slit it applied image over it and overlayed the two split feeds! Probably not the most efficient way... but it worked!

ffmpeg -y -r 1 -b 9600 -i frame_%d.png -vf "movie=bg.png, scale=1280:720:0:0 [bg]; [in] format=rgb32, split [T1], fifo, [bg] overlay=0:0, [T2] overlay=0:0 [out]; [T1] fifo [T2]" -s hd720 testvid.mp4

解决方案

The overlay order is controlled by the order of the inputs, from the ffmpeg docs

[...] takes two inputs and one output, the first input is the "main" video on which the second input is overlayed.

You second command thus becomes:

ffmpeg -y -loop 1 -qscale 1 -r 1 -b 9600 -i frame_%d.png -vf "movie=bg.png [wm];[wm][in] overlay=0:0" -s hd720 testvid.mp4

With the latest versions of ffmpeg the new -filter_complex command makes the same process even simpler:

ffmpeg -loop 1 -i bg.png -i frame_%d.png -filter_complex overlay -shortest testvid.mp4


A complete working example:

The source of our transparent input images (apologies for dancing):

Exploded to frames with ImageMagick:

convert dancingbanana.gif -define png:color-type=6 over.png

(Setting png:color-type=6 (RGB-Matte) is crucial because ffmpeg doesn't handle indexed transparency correctly.) Inputs are named over-0.png, over-1.png, over-2.png, etc.

Our background image (scaled to banana):

Using ffmpeg version N-40511-g66337bf (a git build from yesterday), we do:

ffmpeg -loop 1 -i bg.png -r 5 -i over-%d.png -filter_complex overlay -shortest out.avi

-loop loops the background image input so that we don't just have one frame, crucial!
-r slows down the dancing banana a bit, optional.
-filter_complex is a very recently added ffmpeg feature making handling of multiple inputs easier.
-shortest ends encoding when the shortest input ends, which is necessary as looping the background means that that input will never end.

Using a slightly less cutting-edge build, ffmpeg version 0.10.2.git-d3d5e84:

ffmpeg -loop 1 -r 5 -i back.png -vf 'movie=over-%d.png [over], [in][over] overlay' -frames:v 8 out.avi

movie doesn't allow rate setting, so we slow down the background instead which gives the same effect. Because the overlaid movie isn't a proper input, we can't use -shortest and instead explicitly set the number of frames to output to how many overlaid input frames we have.

The final result (output as a gif for embedding):

这篇关于使用 ffmpeg 在静态背景图像上叠加具有透明度的动画图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆