使用 ffmpeg 在视频上添加水印和缩放图像

发布于 2024-12-17 13:39:42 字数 530 浏览 4 评论 0原文

我希望能够使用包含网站网址的徽标图像为视频添加水印。 视频可以具有不同的格式和尺寸。 我正在尝试找出一个通用的 ffmpeg 命令来实现它,这样我就不必根据我必须处理的视频来调整命令。 到目前为止,我得到:

ffmpeg -i sample.mov -sameq -acodec copy -vf 'movie=logo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]' sample2.mov

通过这种方式,尽管徽标在不同尺寸的视频中看起来太大或太小。 我看到 avfilter 有一个 scale 选项,但我不知道它是否是可以根据输入视频的尺寸调整图像徽标的大小,例如,我可以将徽标缩放到视频长度的 1/3,并保持图像比例。

有什么想法吗?不需要在单个命令中完成,甚至可以是脚本。 提前致谢。

I want to be able to watermark videos with a logo image, which contains a website url.
The videos can be of different formats and dimension.
I'm trying to figure out a generic ffmpeg command to achieve it, so that i don't have to tweak the command depending on the video i have to process.
So far i got:

ffmpeg -i sample.mov -sameq -acodec copy -vf 'movie=logo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]' sample2.mov

In this way though the logo will look too big or too small with video of different size.
I've seen there is a scale option for avfilter, but I haven't figure out whether it's possible to resize the image logo based on the dimension of the input video, so that I can say to scale the logo to 1/3 of the video length for example, and keep the image ratio.

Any idea? doesn't need to be done in a single command, could even be a script.
thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-12-24 13:39:42

与此同时,我想出了这个完成这项工作的脚本:

#!/bin/bash

VIDEO=$1
LOGO=$2
VIDEO_WATERMARKED=w_${VIDEO}

VIDEO_WIDTH=`ffprobe -show_streams $VIDEO 2>&1 | grep ^width | sed s/width=//`
echo The video width is $VIDEO_WIDTH

cp $LOGO logo.png
IMAGE_WIDTH=$((VIDEO_WIDTH/3))
echo The image width will be $IMAGE_WIDTH

mogrify -resize $IMAGE_WIDTH logo.png
echo logo.png resized

echo Starting watermarking
ffmpeg -i $VIDEO -sameq -acodec copy -vf 'movie=logo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]' $VIDEO_WATERMARKED
echo Video watermarked

我唯一不确定的是如何保持相同的视频质量。我认为“-sameq”会保持相同的视频质量,但生成的视频尺寸较小。
我注意到了这一点:

INPUT
Duration: 00:01:25.53, start: 0.000000, bitrate: 307 kb/s
    Stream #0:0(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), 
yuv420p, 640x480 [SAR 1:1 DAR 4:3], 261 kb/s, 10 fps, 10 tbr, 3k tbn, 25 tbc
OUTPUT
   encoder         : Lavf53.20.0
    Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), yuv420p, 640x480 [SAR 1:
1 DAR 4:3], q=-1--1, 10 tbn, 10 tbc

而音频信息是相同的。
关于如何保持原始视频质量有什么建议吗?
谢谢

In the meantime i came up with this script that does the job:

#!/bin/bash

VIDEO=$1
LOGO=$2
VIDEO_WATERMARKED=w_${VIDEO}

VIDEO_WIDTH=`ffprobe -show_streams $VIDEO 2>&1 | grep ^width | sed s/width=//`
echo The video width is $VIDEO_WIDTH

cp $LOGO logo.png
IMAGE_WIDTH=$((VIDEO_WIDTH/3))
echo The image width will be $IMAGE_WIDTH

mogrify -resize $IMAGE_WIDTH logo.png
echo logo.png resized

echo Starting watermarking
ffmpeg -i $VIDEO -sameq -acodec copy -vf 'movie=logo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]' $VIDEO_WATERMARKED
echo Video watermarked

The only thing i'm not certain about is how to keep the same video quality. I thought that "-sameq" would keep the same video quality, but the resulting video size is smaller.
I've noticed this:

INPUT
Duration: 00:01:25.53, start: 0.000000, bitrate: 307 kb/s
    Stream #0:0(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), 
yuv420p, 640x480 [SAR 1:1 DAR 4:3], 261 kb/s, 10 fps, 10 tbr, 3k tbn, 25 tbc
OUTPUT
   encoder         : Lavf53.20.0
    Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), yuv420p, 640x480 [SAR 1:
1 DAR 4:3], q=-1--1, 10 tbn, 10 tbc

whereas the audio information are identical.
Any advice on how to keep the original video quality?
thanks

三人与歌 2024-12-24 13:39:42

谢谢你的主意,艾伊!

使用 powershell 同样的事情:

$videoFilename = "..."
$logoFilename = "..."

$videoInfo = (& "$($ffmpeg)ffprobe.exe" -show_streams -of xml -loglevel quiet $videoFilename) | Out-String
$videoStreamInfo = Select-Xml -Content $videoInfo -XPath "/ffprobe/streams/stream[@codec_type='video' and @width and @height][1]"

$videoWidth = $videoStreamInfo.Node.width
$videoHeight = $videoStreamInfo.Node.height

# logo will be 10% orginal video width
$logoWidth = $videoWidth/10

# preparing arguments
$a = "-i", $videoFilename, "-i", $logoFilename, "-filter_complex", "[1]scale=$($logoWidth):$($logoWidth)/a [logo]; [0][logo]overlay=main_w-overlay_w-10:10", "-ss", "-y", "-loglevel", "error", $node.output
# logo actual height is cumputed by ffdshow`s scale filter at "$($logoWidth)/a". a - original video aspect ratio


# clear error stream for clear error handling
$error.Clear()
# execute ffmpeg
(& "$($ffmpeg)ffmpeg.exe" $a)

if($error.Count -gt 0){
    Write-Output "error! $error"
}

这里可以不使用“mogrify”工具,只使用 ffmpeg 发行版。

Thanks for idea, Ae.!

Same thing using powershell:

$videoFilename = "..."
$logoFilename = "..."

$videoInfo = (& "$($ffmpeg)ffprobe.exe" -show_streams -of xml -loglevel quiet $videoFilename) | Out-String
$videoStreamInfo = Select-Xml -Content $videoInfo -XPath "/ffprobe/streams/stream[@codec_type='video' and @width and @height][1]"

$videoWidth = $videoStreamInfo.Node.width
$videoHeight = $videoStreamInfo.Node.height

# logo will be 10% orginal video width
$logoWidth = $videoWidth/10

# preparing arguments
$a = "-i", $videoFilename, "-i", $logoFilename, "-filter_complex", "[1]scale=$($logoWidth):$($logoWidth)/a [logo]; [0][logo]overlay=main_w-overlay_w-10:10", "-ss", "-y", "-loglevel", "error", $node.output
# logo actual height is cumputed by ffdshow`s scale filter at "$($logoWidth)/a". a - original video aspect ratio


# clear error stream for clear error handling
$error.Clear()
# execute ffmpeg
(& "$($ffmpeg)ffmpeg.exe" $a)

if($error.Count -gt 0){
    Write-Output "error! $error"
}

here a can go without using 'mogrify' tool, only ffmpeg distribution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文