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;
