Adjust Audio Timestamps with FFmpeg asetpts Filter
This article provides a quick and practical guide on how to use the
asetpts (Audio Set Presentation TimeStamp) filter in
FFmpeg. You will learn the basic syntax of this filter and discover how
to apply it to solve common audio synchronization issues, such as
resetting audio timelines to start at zero, introducing a fixed delay,
or rebuilding corrupted timestamps.
The asetpts filter in FFmpeg allows you to modify the
Presentation TimeStamp (PTS) of audio frames. This is essential when
syncing audio with video, fixing playback issues after trimming, or
concatenating files with discontinuous timelines. The filter is applied
using the audio filter flag (-af) followed by an expression
that defines how the new timestamps should be calculated.
Resetting Audio Timestamps to Start at Zero
When trimming audio files, the resulting output may retain the
original source timestamps, causing media players to lag before playback
starts. To reset the audio timeline so it starts exactly at zero, use
the PTS-STARTPTS expression:
ffmpeg -i input.mp3 -af "asetpts=PTS-STARTPTS" output.mp3Adding a Constant Delay to Audio
If your audio is playing too early relative to a video track, you can
delay the audio by adding a specific number of seconds to the timestamp.
To delay the audio by 2.5 seconds, use the following command, which
divides the delay time by the audio timebase (TB):
ffmpeg -i input.wav -af "asetpts=PTS+2.5/TB" output.wavTo shift the audio 2.5 seconds earlier instead, subtract the value:
"asetpts=PTS-2.5/TB".
Rebuilding Corrupted or Missing Timestamps
If an audio stream has broken, gapped, or missing timestamps, it can
cause stuttering during playback. You can generate a clean, continuous
timeline based entirely on the actual audio samples. This expression
uses the frame index (N), sample rate (SR),
and timebase (TB) to reconstruct the timeline:
ffmpeg -i input.aac -af "asetpts=N/(SR*TB)" output.aacAdjusting Audio Speed Timestamps
To change the speed of the audio timeline (often paired with video
speed adjustments like setpts), you can multiply the PTS by
a scaling factor. For example, to make the audio timestamps render twice
as fast (double speed):
ffmpeg -i input.mp3 -af "asetpts=0.5*PTS" output.mp3