FFmpeg setpts Filter Guide to Adjust Frame Timing

This article explains how to use the FFmpeg setpts video filter to manipulate Presentation Time Stamps (PTS), allowing you to speed up, slow down, or precisely adjust the timing of video frames. You will learn the basic syntax, practical examples for fast and slow motion, and how to properly sync the accompanying audio.

Understanding PTS and the setpts Filter

In video files, each frame has a Presentation Time Stamp (PTS) that tells the player exactly when to display that frame. The setpts filter modifies these timestamps. By changing the PTS, you alter the playback speed and frame timing of the video without changing the actual number of frames (unless you also change the output frame rate).

The basic syntax for the filter is:

ffmpeg -i input.mp4 -vf "setpts=EXPRESSION" output.mp4

How to Speed Up Video (Fast Motion)

To speed up a video, you need to make the presentation timestamps occur sooner. This is done by multiplying the PTS by a fraction less than 1.

For example, to speed up a video to double speed (2x), multiply the PTS by 0.5:

ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" -an output.mp4

Note: The -an flag is used here to disable audio, as speeding up the video alone will desynchronize the audio.

How to Slow Down Video (Slow Motion)

To slow down a video, you must space out the timestamps. This is done by multiplying the PTS by a factor greater than 1.

To slow a video down to half speed (0.5x), multiply the PTS by 2.0:

ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" -an output.mp4

Resetting Timestamps to Zero

Sometimes, video clips extracted from live streams or security cameras have irregular starting timestamps. You can reset the container timestamps so the video starts exactly at zero using the STARTPTS variable:

ffmpeg -i input.mp4 -vf "setpts=PTS-STARTPTS" output.mp4

Adjusting Video and Audio Speed Together

If you change the video speed using setpts, the audio will quickly go out of sync. To change both video and audio speed simultaneously, combine the setpts video filter with the atempo audio filter.

Example: Speeding up Video and Audio by 2x

ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" -af "atempo=2.0" output.mp4

Example: Slowing down Video and Audio to Half Speed (0.5x)

ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" -af "atempo=0.5" output.mp4

Note: The atempo filter in FFmpeg only supports values between 0.5 and 100. If you need to slow down a video more than 0.5x, you must chain multiple atempo filters together (e.g., -af "atempo=0.5,atempo=0.5" for 0.25x speed).