In this article, we explain how to speed up or slow down a video using FFmpeg. Whether you’re a video editor, a developer dealing with media files, or an enthusiast curious about video manipulation, you’ll find value in this guide 🙂
We’ll begin with setting up FFmpeg on your system, delve into understanding some essential commands, and then move towards our main focus – slowing down and speeding up videos using FFmpeg.
Let’s dive right in.
Table of Contents
Setting up FFmpeg
Before we learn to speed up or slow down videos using FFmpeg, we first have to install it on our computers. Setting up FFmpeg on your computer, whether it’s Windows, macOS, or Linux-based, is a straightforward process. However, the steps vary slightly depending on the platform. Here are the steps to install it –
For Windows users:
- Download the latest FFmpeg static build from the official website. Ensure you select the correct architecture (32-bit or 64-bit) for your system.
- Extract the downloaded ZIP file. You’ll find a folder named ‘ffmpeg-<version>-essentials_build’.
- Add the ‘bin’ directory within this folder to your system’s PATH. This step allows you to run FFmpeg from the command line, irrespective of the directory you’re in.
For macOS users:
- Open Terminal.
- If you have Homebrew installed, simply type in
brew install ffmpeg. If you don’t, consider installing Homebrew first. It simplifies the software installation process on macOS.
For Linux users:
The FFmpeg package is generally included in the standard repository of most Linux distributions. Use your distribution’s package manager to install FFmpeg. For example, on Ubuntu, use
sudo apt-get install ffmpeg.
After installation, verify it by typing
ffmpeg -version in the command line. The output should look something like this –
(base) krishna@debian:~/research$ ffmpeg -version -hide_banner ffmpeg version N-111859-g3c397a1d46-20230830 Copyright (c) 2000-2023 the FFmpeg developers built with gcc 13.1.0 (crosstool-NG 22.214.171.124_227d99d) libavutil 58. 17.100 / 58. 17.100 libavcodec 60. 23.100 / 60. 23.100 libavformat 60. 10.101 / 60. 10.101 libavdevice 60. 2.101 / 60. 2.101 libavfilter 9. 11.100 / 9. 11.100 libswscale 7. 3.100 / 7. 3.100 libswresample 4. 11.100 / 4. 11.100 libpostproc 57. 2.100 / 57. 2.100
If you see details of the installed FFmpeg version, you’ve successfully set it up. If you are interested in FFmpeg, go here to see more options on installing FFmpeg.
Now, let’s learn to slow down a video using FFmpeg. After that, we’ll learn how to speed up a video.
How to Slow Down Video with FFmpeg
We’ll now explore the process of slowing down a video using the
setpts video filter in FFmpeg. ‘setpts’ stands for “set presentation timestamp” and it adjusts the frame timestamps, which can effectively slow down or speed up your video playback.
Here’s the basic command to slow down a video using FFmpeg and the
ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" output.mp4
In this command,
-vfoption tells FFmpeg that we are going to apply a video filter.
- The “setpts=2.0*PTS” portion is our filter of choice.
PTSstands for Presentation TimeStamp in the video stream, and by multiplying it by 2.0, we are effectively doubling the length of the video, thus slowing it down by half.
setpts filter can take any positive floating-point number as an argument. If you want to slow the video down even more, simply increase the value. For instance, using
setpts=4.0*PTS would make the video play at quarter speed.
How to Speed Up Video with FFmpeg
Speeding up a video involves reducing its overall playback duration. So, if you have a 10 min video (600 seconds) and you speed it up by a factor of 10, then the output is going to be 1 min long (60 seconds). We can easily speed up a video using the setpts filter in FFmpeg as follows.
As you might recall, the ‘setpts’ filter adjusts the frame timestamps, which affects the playback speed. To speed up the video, we use a value less than 1.0. Here’s how to do it:
ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4
This command reduces the timestamps by half, effectively doubling the video speed. You can adjust the multiplier according to your needs. A smaller value will speed up the video more.
In the example below, I speed up the original video by a factor of 4 using
Before we end this article, I’d like to briefly touch upon presentation timestamps so that you know what they are when you are changing their values.
SetPts will Re-encode your videos
Just as a cautionary note, when you use setpts in FFmpeg, it will drop frames to achieve the requested speed-up. And, this will force FFmpeg to re-encode your content. Always remember that you can set the video quality you need using CBR, Capped VBR, or CRF techniques while speeding up or slowing down your videos.
Appendix: What is PTS or Presentation Time Stamp?
The Presentation Time Stamp, or PTS, is part of the data in video and audio streams that indicates when a frame (video) or packet (audio) should be presented to the viewer or listener.
Essentially, it’s a timestamp that tells the media player, “This is the exact moment when this particular frame or packet should be displayed or played.”
To better understand, consider watching a movie. Each frame of the movie has a specific time when it should appear on your screen. This timing ensures that all the frames are shown in the correct sequence and at the right speed, giving you a smooth viewing experience. The timing of these frames is dictated by their PTS values.
Now, the PTS values are expressed in “timebase units,” which are fractions of a second. The timebase is determined by the video or audio stream’s frame or sample rate. For instance, if a video has a frame rate of 30 frames per second (fps), each frame will have a duration of 1/30 of a second, and the PTS will increment by this amount for each subsequent frame.
So, if we have a sequence of frames with PTS values like 0, 1/30, 2/30, 3/30, and so forth, the video player will present each frame precisely 1/30 of a second after the previous one, resulting in a smoothly playing video at the correct speed.
When we manipulate the PTS values, as we do when slowing down or speeding up a video using FFmpeg, we’re altering these timestamps. For instance, if we slow down a video by a factor of two (using
setpts=2.0*PTS), we’re effectively doubling the PTS values for each frame. This makes the video player display each frame for twice as long, halving the video’s playback speed. Conversely, if we speed up a video by a factor of two (using
setpts=0.5*PTS), we’re halving the PTS values, making the frames display twice as quickly and doubling the playback speed.
It’s important to note that manipulating PTS values doesn’t alter the actual media content (i.e., the images in the video frames or the audio samples), but rather the timing of when they are presented, which is how the speed changes are achieved.
It’s also worth noting that PTS values can be presented in different ways. They are generally represented in timebase units, as previously explained, but they can also be represented as real time, depending on the context. The
pkt_pts_time field shows the PTS in seconds, which is the real time representation.
By now, you’ve gained a solid understanding of how to speed up or slow down a video using FFmpeg using the setpts filter. Remember, that it involves re-encoding and you can always adjust the encoding parameters to achieve your desired output video quality.
To learn more about FFmpeg, head over our Recipes in FFmpeg section.
Until next time, happy streaming!
Krishna Rao Vijayanagar
Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.
With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.