简体   繁体   English

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

[英]Use ffmpeg to watermark and scale an image on video

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. 我试图找出一个通用的ffmpeg命令来实现它,所以我不必根据我必须处理的视频调整命令。 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. 我已经看到avfilter有一个缩放选项,但我还没弄清楚是否可以根据输入视频的尺寸调整图像徽标的大小,这样我就可以说将徽标缩放到1/3例如视频长度,并保持图像比例。

Any idea? 任何的想法? doesn't need to be done in a single command, could even be a script. 不需要在单个命令中完成,甚至可以是脚本。 thanks in advance. 提前致谢。

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. 我认为“-sameq”会保持相同的视频质量,但最终的视频尺寸会更小。 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 谢谢

Thanks for idea, Ae.! 谢谢你的想法,Ae。!

Same thing using powershell: 使用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. 这里可以不使用'mogrify'工具,只有ffmpeg发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM