Monday, April 8, 2019

Ffmpeg Reverse Video - If I Could Turn Back Time


Staying on the time/ffmpeg topic, this blog will demonstrate how to reverse a video, essentially stepping backwards in time.

Since FFMpeg functionality is fluid with new features, and sometimes command-line arguments, changing along the way I've primarily stuck with a known version in my posts; namely FFMpeg v2.7.2.  This is relevant because in this particular post there are two ways to reverse a video; 1) deconstruction frame-by-frame followed by reassembly, 2) a video filter available in newer versions of Ffmpeg.

Source Video

Let's start by grabbing an interesting video to work from.  Calvin and Hobbes certainly will fit the bill.


$ youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' -o input.mp4 https://www.youtube.com/watch?v=KckYcuiKAN8



Snag a Clip

Let's snag the clip when Hobbes tackles Calvin, a solid segment that could benefit from a reversal.


$ ffmpeg -ss 20.0 -i input.mp4 -t 4 -strict -2 clip.mp4



Reverse the Clip

For newer versions of FFMpeg, this is simply a matter of applying the 'reverse' video filter, like so:


$ ffmpeg -i clip.mp4 -vf reverse reverse.mp4



Alas, with older versions of FFMpeg this can be also done, just a little more heavy lifting involved.  In general, we'll deconstruct the video into a series of frames, then re-encode by supplying the frames in reverse order.



$ ffmpeg -i clip.mp4 frame%06d.png

$ ls -t *.png | awk '{print "file \x27" $$1 "\x27"}' > fileList.txt

$ ffmpeg -f concat -r 30 -i fileList.txt reversed.mp4



Resulting in our reversed clip;

Gotta love that tiger!!

No comments:

Post a Comment