How to Adjust Audio Timestamps Using asetpts in FFmpeg

This guide explains how to manipulate audio presentation timestamps (PTS) using FFmpeg's asetpts audio filter. Adjusting these timestamps allows you to resolve audio-video synchronization problems, introduce positive or negative delays, and reset container timing after trimming or concatenating media files.

Understanding the asetpts Filter

The asetpts filter modifies the presentation timestamp of incoming audio frames using custom mathematical expressions. In FFmpeg, presentation timestamps determine precisely when a specific packet of audio should be played relative to the stream's start.

The basic syntax for applying the filter via the audio filter argument (-af) is:

ffmpeg -i input.wav -af "asetpts=EXPRESSION" output.wav

Common Variables in asetpts Expressions

  • PTS: The presentation timestamp of the current input frame.
  • STARTPTS: The presentation timestamp of the first frame.
  • TB: The timebase of the input audio stream (used to convert seconds into timestamp units).
  • N: The sequential count of the current frame (starting at 0).
  • SR: The sample rate of the audio.

Key Use Cases and Commands

1. Resetting Timestamps to Zero

When cutting or splicing audio from a larger file, streams often retain their original presentation timestamps, which can cause synchronization errors or player playback failures. You can normalize the audio to start exactly at zero using PTS-STARTPTS:

ffmpeg -i input.mp3 -af "asetpts=PTS-STARTPTS" output.mp3

2. Adding an Audio Delay (Offset)

To shift the entire audio stream forward by a set duration, add the desired number of seconds divided by the stream timebase (TB).

To delay audio by 3 seconds:

ffmpeg -i input.wav -af "asetpts=PTS+3/TB" output.wav

To shift the audio backward by 1.5 seconds:

ffmpeg -i input.wav -af "asetpts=PTS-1.5/TB" output.wav

3. Adjusting Audio Playback Speed via Timestamps

You can alter the pacing of audio timestamps by multiplying the PTS value.

To double the playback speed (timestamps arrive twice as fast):

ffmpeg -i input.mp3 -af "asetpts=0.5*PTS" output.mp3

To slow the playback to half speed:

ffmpeg -i input.mp3 -af "asetpts=2.0*PTS" output.mp3

Note: Modifying timestamps alone changes frame pacing rather than audio sample rates directly. For pitch-preserving playback speed changes, use the atempo filter instead.

4. Synchronizing Audio and Video Streams

When an audio stream drifts out of sync with a video stream, you can apply timestamp offsets alongside video adjustments.

To delay the audio track by 500 milliseconds (0.5 seconds) to align with a video:

ffmpeg -i input.mp4 -vf "setpts=PTS-STARTPTS" -af "asetpts=PTS-STARTPTS+0.5/TB" -c:v libx264 -c:a aac output.mp4

This ensures the video starts cleanly at time zero while shifting the audio track by half a second to fix synchronization lag.