Use ffmpeg for the watermark and scale the image on the video

I want to be able to watermark with a logo that contains the website URL. Videos can be of different formats and sizes. I am trying to find a common ffmpeg command to achieve it, so I don’t need to configure the command depending on the video that I have to process. So far I have received:

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 

Thus, although the logo will look too large or too small with videos of different sizes. I saw that there is a scale option for avfilter, but I don’t know if it is possible to change the image logo size based on the size of the input video, so I can say to scale the logo to 1/3 of the video length, for example, and maintain the image ratio.

Any idea? no need to do in one command, maybe even a script. thanks in advance.

+4
source share
2 answers

At the same time, 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 sure about is how to maintain the same video quality. I thought that "-sameq" would maintain the same video quality, but as a result, the video size is smaller. I 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 is identical. Any tips on how to maintain the original video quality? thanks

+2
source

Thanks for the idea, Ae.!

The same with 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" } 

can work here without using the "mogrify" tool, only for ffmpeg distribution.

+1
source

Source: https://habr.com/ru/post/1382382/


All Articles