How to Use FFmpeg asetpts Filter to Adjust Audio Timing
This article explains how to use the FFmpeg asetpts
(Audio Set Presentation TimeStamp) filter to manipulate and adjust the
timing of audio streams. You will learn the basic syntax of the filter,
how to reset audio timestamps to start at zero, how to sync audio with
altered video speeds, and how to introduce precise time delays or
advances to resolve synchronization issues.
The asetpts filter works by modifying the Presentation
TimeStamp (PTS) of each audio frame. This determines exactly when each
packet of audio is played back. The basic syntax for applying the filter
in an FFmpeg command is -af "asetpts=EXPRESSION", where the
expression defines how the new timestamps are calculated.
Resetting Audio Timestamps to Zero
When extracting audio from a specific part of a container or
concatenating files, the audio may carry over offset timestamps, causing
playback delays. You can reset the timestamps so the audio starts
exactly at zero using the STARTPTS constant:
ffmpeg -i input.mp4 -af "asetpts=PTS-STARTPTS" output.mp4This expression subtracts the start presentation timestamp
(STARTPTS) from the current timestamp (PTS) of
each frame, effectively shifting the start of the audio stream to
zero.
Adding a Delay to Audio (Offsetting)
If your audio is playing too early relative to your video, you can
delay the audio by adding a specific number of seconds to the
timestamps. Since timestamps are measured in stream time base units, you
must divide the desired delay in seconds by the time base
(TB):
ffmpeg -i input.mp4 -af "asetpts=PTS+2.5/TB" output.mp4In this example, 2.5/TB adds a 2.5-second delay to the
audio stream. To shift the audio earlier instead, replace the plus sign
with a minus sign (e.g., PTS-1.0/TB to shift it 1 second
earlier).
Matching Audio Speed with Video Speed
When changing the playback speed of a video using the video
setpts filter, the audio timestamps must be scaled
proportionally using asetpts to prevent sync issues. For
example, to speed up both video and audio to double speed (which
requires halving the presentation timestamps):
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]asetpts=0.5*PTS[a]" -map "[v]" -map "[a]" output.mp4Note: Modifying audio timestamps using asetpts alone
to change speed will result in dropped or duplicated samples to match
the new timeline. To change audio speed smoothly without changing the
pitch, combine this with the atempo audio filter.