FFMPEG - Make a watermark in random position

I'm trying to implement the following loop:

  1. Place logo.png in a random (x ,y) position of the video.
  2. Make the logo fade out after 2 secods.
  3. Wait 30 seconds and repeat.

After research in google I adapted different codes and got this this:

ffmpeg -i input.mp4 -loop 1 -i logo.png -filter_complex "[1]trim=0:30,fade=in:st=0:d=1:alpha=1,fade=out:st=9:d=1:alpha=1,loop=999:750:0,setpts=N/25/TB[w];[0][w]overlay=shortest=1:x=if(eq(mod(n\,200)\,0)\,sin(random(1))*w\,x):y=if(eq(mod(n\,200)\,0)\,sin(random(1))*h\,y)" output.mp4

But it's not fully functional for my needs (and maybe there are commands I can remove). It only appear in a small random area on the top-left area of the video and not always fade out after 2 seconds.

What I should change to make it work? Also, is it possible to reduce opacity of the logo.png or use a text instead?


Solution 1:

I managed to write a command that works, so I will post here in case someone needs.

ffmpeg.exe -i input.mp4 -vf drawtext="fontsize=10:fontfile=/Windows/Fonts/arial.ttf:text='Text Here':x=if(eq(mod(n\,1200)\,0)\,rand(0\,(w-text_w))\,x):y=if(eq(mod(n\,1200)\,0)\,rand(0\,(h-text_h))\,y):enable=lt(mod(n\,1200)\,200)" -c:v libx264 -crf 17 -c:a copy output.mp4

The parameter t\ was not working, that was my first problem using examples from ffmpeg documentation and from the answers in the community Stack.

Took me some time to noticed this issue. Maybe is something in Windows. So I just used n\ instead that is number of frames.

So now (x,y) change every 1200 frames

x=if(eq(mod(n\,1200)\,0)\,rand(0\,(w-text_w))\,x):y=if(eq(mod(n\,1200)\,0)\,rand(0\,(h-text_h))\,y)

Show text for 200 frames every 1200 frames

enable=lt(mod(n\,1200)\,200)

Thanks for all help here!

Solution 2:

Another formulation which might work better comes from the Stack Overflow article
ffmpeg - Dynamic letters and random position watermark to video?.

ffmpeg -i input.mp4 \
-vf \
"drawtext=fontfile=font.ttf:fontsize=80:[email protected]:text='studentname': \
 x=if(eq(mod(t\,30)\,0)\,rand(0\,(W-tw))\,x): \
 y=if(eq(mod(t\,30)\,0)\,rand(0\,(H-th))\,y)" \
-c:v libx264 -crf 23 -c:a copy output.mp4

This will randomize the position every 30 seconds with no repeats.