10

Ok, so using the overlay filter, one can change the x/y location of a video over time by using the "t" variable. For example, overlay=x='if(gte(t,2), -w+(t-2)*20, NAN)':y=0 can be used to start sliding an overlayed element from the top left to top right, after 2 seconds has passed.

What I want to do is to use a similar principle for scaling elements. I would like to be able to change the scale of a video over time by using an expression (like, scale=w=iw*t, or something along that line).

Is this possible?

when I run that expression through -filter_complex, I get

Error when evaluating the expression 'iw*t'.

Maybe the expression for out_w:'iw*t' or for out_h:'-1' is self-referencing.

I'm feeling like there's a fair chance I'm going to need to patch FFMpeg if I want to get this functionality, but I thought I would ask first, to see if anyone would be in the know about this kind of thing!

Does anyone know how such a task might be accomplished?

Thanks for the help!

16
  • Sounds more like a syntax error right now but I haven't used that feature before so unfortunately I can't really help here. Commented Jul 10, 2014 at 19:51
  • 1
    t is not a defined thing when it comes to overlay filter. Commented Jul 17, 2014 at 16:01
  • 1
    You could just write a filter. wiki.multimedia.cx/index.php?title=FFmpeg_filter_howto Commented Aug 7, 2014 at 12:50
  • 1
    @alexspeller the scale filter does not support timeline editing, so not directly possible. I'll check if there's a workaround. Commented Feb 1, 2017 at 5:34
  • 1
    Technically possible with H.264/5 output. Don't know how players/editors will handle it. Commented Feb 1, 2017 at 9:25

2 Answers 2

2

From what I understand 't' is evaluated once. Have you read this: https://ffmpeg.org/ffmpeg-filters.html#zoompan ?

Ffmpeg Docs:

9.170.1 Examples

  • Zoom-in up to 1.5 and pan at same time to some spot near center of picture:

zoompan=z='min(zoom+0.0015,1.5)':d=700:x='if(gte(zoom,1.5),x,x+1/a)':y='if(gte(zoom,1.5),y,y+1)':s=640x360

  • Zoom-in up to 1.5 and pan always at center of picture:

zoompan=z='min(zoom+0.0015,1.5)':d=700:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'

  • Same as above but without pausing:

zoompan=z='min(max(zoom,pzoom)+0.0015,1.5)':d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'

0

Here is an example of an overlay image that scale from VALUE_1 to VALUE_2 from TIME_1 to TIME_2 using an ease-in-out animation

ffmpeg -i background.mp4 -loop 1 -t 20 -i image_overlay.png -filter_complex "\ [1]scale=w=' if( gte(t,START_TIME), if(lte(t,END_TIME), SCALE_1 + (SCALE_2 - SCALE_1) * (1 - exp(-5 * (t - START_TIME) / (END_TIME - START_TIME))), SCALE_2), SCALE_1 ) ':h=-1:eval=frame[top]; \ [0][top]overlay=0:0[out]" \ -map "[out]" -c:v h264_nvenc -crf 18 output2.mp4 

Since the second input is an image, you can't create an animation based on time, because an image is just a single frame, so the idea is: you need to create a video from the image. To do that, all you need is to add -loop 1 -t 20 before your input like so:

-loop 1 -t 20 -i image_overlay.png 

Don't forget to change the value 20 to the length of your first input.

If you want to detect automatically the length of the first video without using -t option, here is a solution:

ffmpeg -i background.mp4 -loop 1 -i image_overlay.png -filter_complex "\ [1]scale=w=' if( gte(t,START_TIME), if(lte(t,END_TIME), SCALE_1 + (SCALE_2 - SCALE_1) * (1 - exp(-5 * (t - START_TIME) / (END_TIME - START_TIME))), SCALE_2), SCALE_1 ) ':h=-1:eval=frame[top]; \ [0][top]overlay=w=0:h=0:shortest=1[out]" \ -map "[out]" -c:v h264_nvenc -crf 18 output2.mp4 

If you prefer a linear animation instead of ease-in-out, here you go:

ffmpeg -i i1.mp4 -loop 1 -i i2.png -filter_complex "\ [1]scale=w='if(gte(t,START_TIME), if(lte(t,END_TIME), VALUE_1 + (VALUE_2 - VALUE_1) * (t - START_TIME) / (END_TIME - START_TIME), VALUE_2), VALUE_1)':h=-1:eval=frame[top]; \ [0][top]overlay=x=0:y=0:shortest=1[out]" \ -map "[out]" -c:v h264_nvenc -crf 18 output2.mp4 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.