ffmpeg: smooth zoompan with no jiggle

6

3

I'm trying to create a basic 5 second zoompan to the center of an image (from the example on the ffmpeg.org website). The command below works, but jitters more than my hands after 5 cups of coffee:

ffmpeg -framerate 25 -loop 1 -i island.jpg -filter_complex "[0:v]scale=-2:480,zoompan=z='min(zoom+0.0015,1.5)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d=125,trim=duration=5[v]" -map "[v]" -y out.mp4

Input jpg. Output mp4.

I'm aware of the ffmpeg bug #4298. The posted suggested workaround is to use the scale filter prior to zoompan. But as shown in my example, this still seems to have no effect.

It seems any arbitrary x or y values cause the jiggle/jerky/shaky effect.

Can anyone offer any kind of effective workaround? Thanks!

Version info:

ffmpeg version 3.1.2-static http://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 5.4.0 (Debian 5.4.0-6) 20160609

Timbo White

Posted 2016-08-11T22:59:39.297

Reputation: 175

Answers

13

Avoid downscaling beforehand. Either apply a trunc function to the x and y expressions. Or upscale it before. Preferably the latter. This gets rid of most of the jitter for me.

ffmpeg -framerate 25 -loop 1 -i island.jpg -filter_complex "[0:v]scale=8000x4000,zoompan=z='min(zoom+0.0015,1.5)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d=125,trim=duration=5[v]" -map "[v]" -y out.mp4

Basically, the filter is rounding the values from the x and y expressions, which may be either rounded up or down. That's creating an uneven motion due to changes in direction of pan. Increasing the resolution beforehand allows the rounding to be smaller.

Gyan

Posted 2016-08-11T22:59:39.297

Reputation: 21 016

1Awesome, thank you! I was also able to the downscale after the zoompan filter to get the desired 480p and the zoom remained smooth: ffmpeg -framerate 25 -loop 1 -i island.jpg -filter_complex "[0:v]scale=8000x4000,zoompan=z='min(zoom+0.0015,1.5)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d=125,trim=duration=5[v1];[v1]scale=-2:480[v]" -map "[v]" -y out.mp4 – Timbo White – 2016-08-12T08:24:38.733

You can scale the output from within zoompan e..g :d=125:s=hd480 or d=125:s=640x360 – Gyan – 2016-08-12T10:24:51.320

OK so s is zoompan's output image size. But it doesn't seem like there's a way to get zoompan's scale to preserve the aspect ratio of the original image automatically. So in order to get the result to a height of 480 with aspect ratio preserved for this 4000x2000 example, you'd have manually set zoompan to s=960x480 (can't use -2x480 trick). Otherwise zoompan uses s=hd720 (1280x720) by default, which skews the aspect ratio. – Timbo White – 2016-08-12T10:57:29.060

@Mulvya, can you expand on "apply a trunc function to the x and y expressions" upscaling can cause a decrease in performance, maybe there is an alternative work around. – Eric – 2018-01-26T04:08:32.250

After the filter evaluates the x & y value for a given frame,trunc will remove the fractional part of the result. Apply it like this x=trunc(iw/2-(iw/zoom/2)). This may not be as smooth as the upscale route. – Gyan – 2018-01-26T04:33:03.773

It's been almost two years since the answer but the problem is still there, ffmpeg team did nothing in this part. If you want to do zoompan over 4k video and use this upscaling then you get a huge memory overhead and performance downgrade about 16x times on my machine. – Northern Captain – 2018-04-04T21:13:19.107

I'll take a look at the code. – Gyan – 2018-04-05T05:39:22.193

The issue is being tracked here (reported by @slhck), but it hasn't received much love actually... the scaling workaround isn't always a solution as it uses a lot of memory

– mcont – 2018-04-25T19:38:32.080