FFmpeg is a free and open-source video editing tool capable of trimming, cropping, concatenating, muxing, and transcoding almost any type of media file you throw at it.
It's also a very robust solution for implementing video automation, as we use it extensively in our own video editing API. For this tutorial we'll use FFmpeg 5.1.2, but any recent version will do.
The following command will speed up a video file by two times, keeping the frame rate the same. In other words, if your input video is 30 frames per second, your output video will also be 30 frames per second, but with half the frames dropped.
$ ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4
The -vf "setpts=0.5*PTS" argument sets the PTS (the presentation timestamp) half its original value, effectively doubling the video speed. To speed up the video by four times, use -vf "setpts=0.25*PTS".
There's also a way to speed up the video without losing frames. If you want to do this, you'll have to specify the frame rate you want to end up with. For example, you can double the speed of a 30-fps video to 60 fps by doing the following:
$ ffmpeg -i input.mp4 -r 60 -vf "setpts=0.5*PTS" output.mp4
The addition of the -r 60 argument instructs FFmpeg to force the output file to 60 fps, rather than using the frame rate of the input file.
You can slow down a video by setting a PTS value higher than 1. For example, here's how you slow down a video by 2 times:
$ ffmpeg -i input.mp4 -vf "setpts=2*PTS" output.mp4
Video with a low frame rate may become choppy if it is slowed down, as its frame rate may drop below the minimum recommended 25 frames per second. While there is no perfect solution, FFmpeg comes with a filter that can increase the frame rate by motion interpolation. Use the following command to enable this filter, which may take some time as it is CPU intensive:
$ ffmpeg -i input.mp4 -vf "setpts=2*PTS,minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=60'" output.mp4
Here, the setpts=2*PTS filter instructs FFmpeg to slow down the video by two times, which is combined with the minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=60' filter to ensure a smooth frame rate of 60. There are several options available to you with the minterpolate filter, which are explained on this page.