FFMPEG:在另一个视频中叠加一个视频,并使黑色像素透明

问题描述:

我正在尝试使用 FFMPEG 创建一个视频,其中一个视频覆盖在另一个视频上。

I'm trying to use FFMPEG to create a video with one video overlayed on top another.

我有2个MP4。我需要在覆盖层视频透明中使所有 BLACK像素,以便我可以看到其下的主要视频。

I have 2 MP4s. I need to make all BLACK pixels in the overlay video transparent so that I can see the main video underneath it.

我发现两种方法可以在另一种视频中叠加一个视频:

I found two ways to overlay one video on another:

首先,以下将叠加层置于中心,因此隐藏主要影片的下方部分:

First, the following positions the overlay in the center, and therefore, hides that portion of the main video beneath it:

    ffmpeg -i 1.mp4 -vf "movie=2.mp4 [a]; [in][a] overlay=352:0 [b]" combined.mp4 -y

而且,这个,将重叠式视频放在左边,但是它的不透明度设置为50%,所以至少其他下面的视频是可见的:

And, this one, places the overlay video on the left, but it's opacity is set to 50% so at least other one beneath it is visible:

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex "[0:v]setpts=PTS-STARTPTS[top]; [1:v]setpts=PTS-STARTPTS, format=yuva420p,colorchannelmixer=aa=0.5[bottom]; [top][bottom]overlay=shortest=0" -acodec libvo_aacenc -vcodec libx264 out.mp4 -y

我的目标只是使覆盖中的所有黑色像素(2 .mp4)完全透明。这样做可以做到这一点。

My goal is simply to make all black pixels in the overlay (2.mp4) completely transparent. How can this be done.

这种做法的方法是将黑色和黑色重叠,正如@MoDJ所说,这可能不会产生令人满意的结果。下面我建议的方法也不一样,但值得一试。

The notional way to do this is to chroma-key the black out and then overlay, But as @MoDJ said, this likely won't produce satisfactory results. Neither will the method I suggest below, but it's worth a try.

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex
"[1]split[m][a];
 [a]geq='if(gt(lum(X,Y),16),255,0)',hue=s=0[al];
 [m][al]alphamerge[ovr];
 [0][ovr]overlay"
output.mp4

以上,我复制了重叠视频流,然后使用 geq 过滤器来操纵亮度值,使得亮度大于16的任何像素(即不是纯黑色)的亮度设置为白色,否则为零。由于我没有为两个颜色通道提供表达式,所以 geq 会回到luma表达式。我们不想要,所以我使用 hue 过滤器来取消这些渠道。然后,我使用 alphamerge 过滤器将其作为Alpha通道与叠加视频的第一个副本进行合并。然后,覆盖。就像我说的,这可能不会产生令人满意的结果。您可以调整geq过滤器中的值 16 以更改黑色阈值。限制范围(Y:16-235)视频文件的建议范围为16-24。

Above, I duplicate the overlay video stream, then use the geq filter to manipulate the luma values so that any pixel with luma greater than 16 (i.e. not pure black) has its luma set to white, else zero. Since I haven't provided expressions for the two color channels, geq falls back on the luma expression. We don't want that, so I use the hue filter to nullify those channels. Then I use the alphamerge filter to merge this as an alpha channel with the first copy of the overlay video. Then, the overlay. Like I said, this may not produce satisfactory results. You can tweak the value 16 in the geq filter to change the black threshold. Suggested range is 16-24 for limited-range (Y: 16-235) video files.