How to Use FFmpeg setpts for Slow Motion
This article explains how to use the setpts video filter
in FFmpeg to create a high-quality slow-motion effect. You will learn
the basic command-line syntax, how the presentation timestamp (PTS) math
works to slow down video footage, and how to adjust the accompanying
audio track so it remains perfectly in sync with your slowed-down
video.
Understanding the setpts Filter
The setpts filter works by modifying the Presentation
Timestamp (PTS) of each video frame. The PTS decides exactly when a
specific frame is displayed during playback.
To create a slow-motion effect, you must increase
the time interval between frames. You do this by multiplying the PTS by
a scaling factor greater than 1.0. For example: * Multiplying by
2.0 makes the video run at 0.5x speed
(half speed). * Multiplying by 4.0 makes the video run at
0.25x speed (quarter speed).
Basic Slow Motion Command (Video Only)
If you only want to slow down the video track and do not need the audio, use the following command. This example slows the video down to half speed (0.5x):
ffmpeg -i input.mp4 -filter:v "setpts=2.0*PTS" -an output.mp4-i input.mp4: Specifies your source video file.-filter:v "setpts=2.0*PTS": Applies the video filter. Multiplying the PTS by 2.0 doubles the duration of the video, creating the 0.5x slow-motion effect.-an: Disables the audio stream, preventing out-of-sync audio issues.output.mp4: The resulting slow-motion video file.
Slowing Down Video and Audio Together
When you slow down a video, the audio does not automatically slow
down with it. To slow down both elements and keep them synchronized, you
must use the atempo audio filter alongside
setpts.
Because atempo accepts a speed factor (where
0.5 is half speed), its value is the inverse of the
multiplier used in setpts.
Here is the command to slow both video and audio to half 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.mp4-filter_complex: Allows you to apply filters to both the video and audio streams simultaneously.[0:v]setpts=2.0*PTS[v]: Slows down the first input’s video stream by a factor of 2 and labels the output stream as[v].[0:a]atempo=0.5[a]: Slows down the first input’s audio stream to 0.5x speed and labels the output stream as[a].-map "[v]" -map "[a]": Tells FFmpeg to use the newly filtered video and audio streams in the final output file.
Note: The atempo filter only supports values between
0.5 and 2.0. If you want to slow your video down even further (e.g., to
0.25x speed), you must chain multiple atempo filters
together, like this: atempo=0.5,atempo=0.5.