How to Use the setpts Filter in FFmpeg
This guide explains how to use the setpts (Presentation
TimeStamp) filter in FFmpeg to change the playback speed of your video
files. You will learn the basic syntax for speeding up and slowing down
video, how to avoid dropped frames, and how to synchronize the audio
using the atempo filter.
The setpts filter works by modifying the presentation
timestamps of each video frame. By changing these timestamps, you
instruct the video player to display the frames at a faster or slower
rate.
Speeding Up Video
To speed up a video, you need to decrease the presentation
timestamps. You do this by multiplying the PTS variable by
a fraction. For example, to double the speed of a video (making it play
in half the time), multiply the PTS by
0.5.
Use the following command to double the video speed:
ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4To speed up the video by 4x, you would use 0.25*PTS.
Slowing Down Video
To slow down a video, you need to increase the presentation
timestamps. You do this by multiplying the PTS by a number
greater than 1. For example, to slow a video down to half-speed (making
it take twice as long to play), multiply the PTS by
2.0.
Use the following command to half the video speed:
ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" output.mp4Adjusting Frame Rate (Smoothing Output)
When you speed up a video, some frames may be dropped to maintain the
default target container frame rate. When you slow down a video, frames
are repeated, which can result in a stuttering effect. To make
slow-motion video smoother, you can increase the output frame rate using
the -r flag or the fps filter:
ffmpeg -i input.mp4 -vf "setpts=2.0*PTS,fps=60" output.mp4Synchronizing Audio
The setpts filter only affects the video stream. If your
input file has audio, the audio will remain at its original speed,
resulting in out-of-sync playback. To change the audio speed to match
the video, you must use the atempo audio filter.
To double both video and audio speed (2x):
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mp4To halve both video and audio speed (0.5x):
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]" output.mp4Note that the atempo filter only accepts values between
0.5 and 100.0. If you need to slow down the
audio even further (e.g., to 0.25x speed), you must chain multiple
atempo filters together (such as
atempo=0.5,atempo=0.5).