Saturday, August 10, 2019

Dynamic Zoom In / Out Video Effect


This is a follow-up on our previous post: FFMpeg Zoom which demonstrated performing a dynamic zoom capability on a video.

Interest from a YouTube viewer asked how to perform zooming in, then zooming back out. I figured I'd take a crack at it.

The previous post showed how to dynamically assigning the zoompan, incrementing each video frame.  Assigning the zoompan  based on the video frame number is the key to incrementing or decrementing the zoompan which gives the effect of zooming in or out.

Let's ignore the dynamic zooming for now and focus on how to assign the zoompan based on a conditional keyed off the frame number.

If we want to zoom to 5x from 15..20 sec (assuming 30fps) in the video the following command will do just that:
$ ffmpeg -y -i target.mp4 -vf "scale=iw:ih,zoompan=z='if(between(in,450,600),5.0,1.0)':d=1:x='320-(320/zoom)':y='240-(240/zoom)'" -an zoomInOut.mp4

Replacing the static zoom coefficients (5.0 and 1.0) with dynamic calculations will give us a smooth zoom in and out effect:
$ ffmpeg -y -i target.mp4 -vf "scale=iw*4.0:ih*4.0,zoompan=z='if(between(in,0,450),min(max(zoom,pzoom)+0.050,5.0),min(max(zoom,pzoom)-0.050,5.0))':d=1:x='320.0*4.0-(320.0*4.0/zoom)':y='240.0*4.0-(240.0*4.0/zoom)'" zoomInOut.mp4

Notice the assignments are identical with exception to the sign of the arithmetic, zooming in adds 0.050, where zooming out subtracts it.

The resulting video looks like this;






2 comments:

  1. how to zoom continuously until the end of the video

    ReplyDelete
  2. Dear Grant,

    Thank you for your example, which I find very usefull. In fact it is the only example I managed to find for video zoom in and out. I'd like to ask you if there is a formula for video zoom out only, without preliminary zooming in. I am not strong in mathematics, so my attempt to configure your formula as follows

    zoompan=z='max(min(zoom,pzoom)-0.05,5.0)':d=1:x='3366.0*4.0-(3366.0*4.0/zoom)':y='1340.0*4.0-(1340.0*4.0/zoom)'

    doesn't give any acceptable result.

    I would appreciate it if you find some time for an answer.

    ReplyDelete