Ae.
Ae.

Reputation: 204

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. 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.

Upvotes: 4

Views: 3923

Answers (2)

Alexus1024
Alexus1024

Reputation: 523

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.

Upvotes: 1

Ae.
Ae.

Reputation: 204

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

Upvotes: 2

Related Questions