FFmpeg -如何缩放视频然后应用水印?

时间:2021-06-27 18:35:36

Im trying to scale a video so that it is always 512 wide where the height changes in proportion to the original video. Once scaled, I then want to apply a watermark/overlay to the video, therefore the video will scale but the watermark wont.

我试着在一个视频上缩放,所以它总是在512宽的地方,高度与原始视频成比例。一旦缩放,我就想在视频中应用水印/叠加,因此视频将缩放,但水印不会。

I am able to achieve each of these separately using the following filters:

我可以分别使用以下过滤器实现每一个:

Scale

规模

-vf "scale=512:-1"

Watermark

水印

-vf "movie=watermark.png [watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]"

They work successfully on their own.

他们自己成功地工作。

However when trying to combine the two, Im having a bit of trouble.

然而,当我试图把两者结合起来的时候,我遇到了一点麻烦。

Having both as parameters of course does not work as one will override the other.

当然,作为一个参数,这两个参数并不会相互作用。

Ive tried:

我试过:

-vf "scale=512:-1,movie=watermark.png [watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]"

my thinking was that the scale would be applied first then the watermark but all I get is an error

我的想法是,水印首先会应用到水印上,但我得到的只是一个错误。

Too many inputs specified for the "movie" filter.

为“movie”过滤器指定的输入太多。

Error opening filters!

打开过滤器错误!

Then changing the , to a ; resulted in:

然后把a换成a;导致:

Simple filtergraph 'scale=512:-1; movie=watermark.png [watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]' does not have exactly one input and output.

简单filtergraph '规模= 512:1;电影=水印。png(水印);[in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]'并不只有一个输入和输出。

Error opening filters!

打开过滤器错误!

I presume I need to do something more with filterchains but Im struggling to figure it out.

我想我需要用过滤链做更多的事情,但我正在努力想办法解决。

Any ideas anyone?

有什么想法吗?

Many thanks in advance.

提前感谢。

3 个解决方案

#1


18  

You can use the -filter_complex option with the scale and overlay filters:

您可以使用-filter_complex选项与scale和overlay过滤器:

ffmpeg -i input.mp4 -i logo.png -filter_complex "[0:v]scale=512:-1[bg];[bg][1:v]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" output
  • See scale and overlay filter documentation for more info.
  • 有关更多信息,请参阅scale和overlay过滤器文档。
  • No need for the movie source filter as in the other examples.
  • 不需要像其他例子那样使用电影源过滤器。
  • You can add -c:a copy if you want to stream copy (re-mux) the original audio instead of re-encoding it. This is useful if your input and output container formats are the same.
  • 您可以添加-c:如果您想要流复制(remux)的原始音频而不是重新编码它。如果您的输入和输出容器格式相同,这将非常有用。
  • The example will place the logo in the center. For other placement options:
    • Upper left with 10 px padding: overlay=10:10
    • 左上角有10px填充:叠加=10:10。
    • Upper right with 10 px padding: overlay=W-w-10:10
    • 右上角有10px填充:叠加=W-w-10:10。
    • Lower right with 10 px padding: overlay=W-w-10:H-h-10
    • 右下角有10px填充:叠加=W-w-10:H-h-10。
    • Lower left with 10 px padding: overlay=H-h-10:10
    • 左下角有10px填充:叠加=H-h-10:10。
  • 这个例子将把logo放在中间。对于其他位置选项:左上角有10个px填充:覆盖=10:10右上角有10个px填充:叠加=W-w-10 -10的右下角,有10个px填充:叠加=W-w-10:H-h-10,左下角有10个px填充:叠加=H-h-10:10。

#2


14  

Thank you to both @DiJuMx and @LordNeckbeard, you both got me closer to my solution. Ive not tried the filter_complex option yet but it certainly looks simpler.

感谢@DiJuMx和@ lordbeard,你们都让我更接近我的解决方案。我还没有尝试过filter_complex选项,但它看起来确实更简单。

The solution I found to work is:

我找到的解决办法是:

-vf "movie=watermark.png [watermark]; [in]scale=512:trunc(ow/a/2)*2 [scale]; [scale][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]"

Note that Ive replaced the -1 in the scale as that had the potential to cause an uneven number of pixels in the height of the video when scaling which would then cause encoding errors.

注意,我替换了scale中的-1,因为它有可能导致在视频的高度出现不均匀的像素数量,然后缩放会导致编码错误。

#3


0  

From what I understand, this might work:

据我所知,这可能行得通:

-vf "movie=watermark.png [watermark]; [in] scale=512:-1,[watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]"

You apply the scale filter to the input "[in]".

您将scale filter应用于输入“[in]”。

Unfortunately I don't have much experience with the filters on ffmpeg so I can't help further. Sorry

不幸的是,我对ffmpeg的过滤器没有太多的经验,所以我不能再进一步了。对不起

#1


18  

You can use the -filter_complex option with the scale and overlay filters:

您可以使用-filter_complex选项与scale和overlay过滤器:

ffmpeg -i input.mp4 -i logo.png -filter_complex "[0:v]scale=512:-1[bg];[bg][1:v]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" output
  • See scale and overlay filter documentation for more info.
  • 有关更多信息,请参阅scale和overlay过滤器文档。
  • No need for the movie source filter as in the other examples.
  • 不需要像其他例子那样使用电影源过滤器。
  • You can add -c:a copy if you want to stream copy (re-mux) the original audio instead of re-encoding it. This is useful if your input and output container formats are the same.
  • 您可以添加-c:如果您想要流复制(remux)的原始音频而不是重新编码它。如果您的输入和输出容器格式相同,这将非常有用。
  • The example will place the logo in the center. For other placement options:
    • Upper left with 10 px padding: overlay=10:10
    • 左上角有10px填充:叠加=10:10。
    • Upper right with 10 px padding: overlay=W-w-10:10
    • 右上角有10px填充:叠加=W-w-10:10。
    • Lower right with 10 px padding: overlay=W-w-10:H-h-10
    • 右下角有10px填充:叠加=W-w-10:H-h-10。
    • Lower left with 10 px padding: overlay=H-h-10:10
    • 左下角有10px填充:叠加=H-h-10:10。
  • 这个例子将把logo放在中间。对于其他位置选项:左上角有10个px填充:覆盖=10:10右上角有10个px填充:叠加=W-w-10 -10的右下角,有10个px填充:叠加=W-w-10:H-h-10,左下角有10个px填充:叠加=H-h-10:10。

#2


14  

Thank you to both @DiJuMx and @LordNeckbeard, you both got me closer to my solution. Ive not tried the filter_complex option yet but it certainly looks simpler.

感谢@DiJuMx和@ lordbeard,你们都让我更接近我的解决方案。我还没有尝试过filter_complex选项,但它看起来确实更简单。

The solution I found to work is:

我找到的解决办法是:

-vf "movie=watermark.png [watermark]; [in]scale=512:trunc(ow/a/2)*2 [scale]; [scale][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]"

Note that Ive replaced the -1 in the scale as that had the potential to cause an uneven number of pixels in the height of the video when scaling which would then cause encoding errors.

注意,我替换了scale中的-1,因为它有可能导致在视频的高度出现不均匀的像素数量,然后缩放会导致编码错误。

#3


0  

From what I understand, this might work:

据我所知,这可能行得通:

-vf "movie=watermark.png [watermark]; [in] scale=512:-1,[watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]"

You apply the scale filter to the input "[in]".

您将scale filter应用于输入“[in]”。

Unfortunately I don't have much experience with the filters on ffmpeg so I can't help further. Sorry

不幸的是,我对ffmpeg的过滤器没有太多的经验,所以我不能再进一步了。对不起