Shift Video Timestamps with FFmpeg setpts Filter
This article provides a straightforward guide on how to shift all
timestamps in a video stream by a constant offset using FFmpeg’s
setpts filter. You will learn the exact commands and
expressions needed to either delay or advance your video frames by a
specific duration in seconds, allowing you to easily fix synchronization
issues.
The setpts (Set Presentation Timestamp) filter in FFmpeg
modifies the timestamps of video frames. To shift these timestamps by a
constant offset, you must add or subtract your desired offset (in
seconds) divided by the stream’s time base (TB).
The Mathematical Formula
In FFmpeg, timestamps are evaluated in terms of the stream’s time base. To apply a constant offset in seconds, use the following expression:
PTS + OFFSET / TB
- PTS: The original Presentation Timestamp of the frame.
- OFFSET: The amount of time in seconds you want to
shift the video (e.g.,
2for two seconds). - TB: The Time Base variable, which FFmpeg automatically calculates.
How to Delay a Video Stream (Shift Forward)
If you want to delay the video stream by a specific number of seconds (moving the timestamps forward), use the addition operator.
To delay the video by 3.5 seconds, run the following command:
ffmpeg -i input.mp4 -vf "setpts=PTS+3.5/TB" -c:a copy output.mp4In this command: * -vf "setpts=PTS+3.5/TB" applies the
video filter to shift all video frames forward by 3.5 seconds. *
-c:a copy copies the audio stream without re-encoding it
(note that this will desynchronize the audio from the shifted
video).
How to Advance a Video Stream (Shift Backward)
If you want the video to play earlier (moving the timestamps backward), use the subtraction operator.
To advance the video by 2 seconds, run the following command:
ffmpeg -i input.mp4 -vf "setpts=PTS-2/TB" -c:a copy output.mp4Keeping Audio in Sync
If you shift the video stream, the audio stream will remain at its
original timestamp unless you shift it as well. To shift both the video
and audio streams by the exact same offset, use the setpts
filter for video and the asetpts filter for audio:
ffmpeg -i input.mp4 -vf "setpts=PTS+2/TB" -af "asetpts=PTS+2/TB" output.mp4This ensures both streams are shifted by 2 seconds, maintaining perfect audio-to-video synchronization.