Optimizing Screen Recordings with ffmpeg: Removing Duplicate Frames and Increasing Playback Speed

| | 3 min read

I recently had to record a screencast for an article on website personalization. I faced a common problem that many people encounter when recording their screens - the resulting video was huge and had a lot of duplicate or similar frames. This was mainly due to the delay in loading the pages and giving the manual inputs. This made the video appear very slow as well.

Screen Recording in Ubuntu

I use Ubuntu 22.04, and it comes with a handy screen recording tool which can be activated by pressing Print Screen.

Ubuntu Screenshot Tool

 

 

FFMPEG for Rescue

Creating a screen recording video is a tough job, not by using the tool, but you need to act as per the script, and there is a possibility of delays and pauses between each activity within the screen recording. In my case, the video I recorded turned up more than 2 minutes. To make it more manageable for the viewers, I needed to remove the duplicate frames and increase the video speed, and that's when I discovered FFmpeg. This powerful command-line tool can remove duplicate frames and increase the speed of a video without losing its essence.

MPDECIMATE and SETPTS

I used the mpdecimate filter in ffmpeg to remove duplicate frames. This filter compares each frame to the previous frame and if the difference between them is below a certain threshold, it removes the duplicate frame. This significantly reduced the file size of the video without sacrificing too much visual quality.

To increase the speed of the video, I used the setpts filter in ffmpeg. This filter adjusts the video frames' presentation timestamps (PTS), which controls the playback speed of the video. I used the expression "PTS/6", which increased the playback speed by 6.

The Command

I used the following command:

ffmpeg -i input.webm -vf mpdecimate,setpts=PTS/6 out.webm

The input video is input.webm, and the output video is out.webm. The filter mpdecimate is used to remove duplicate frames, and the filter setpts with the expression "PTS/6" is used to increase the speed of the video.

The Result

The result was excellent; the original 18.6 MB 2.44minute video was now a 1.8 MB 27-sec video, and the essence of the video was not lost.

Screen Recording Optimized with FFmpeg

It's worth noting that the expression used in the setpts filter can be adjusted to suit your needs. For example, if you want to increase the speed of the video by a factor of 2, you can use the expression "PTS/2". Also, there are a lot of other options available. You can get the details from the FFmpeg documentation page.

NOTE: The result may depend on the specific characteristics of the video, such as its framerate and the timebase of the stream, and it's not always guaranteed that the video will look good after applying such a big factor.

This way, I could remove duplicate frames and increase the video speed using ffmpeg. This can be especially useful for screen recordings, where the duration of the video can be reduced significantly without losing its essence. If you also have a similar problem with your screen recordings, I recommend giving ffmpeg a try.