How to Fix Negative Start Timestamp in FFmpeg
When processing video files with FFmpeg, you may encounter an issue where the output video has a negative start timestamp, causing playback delays, audio synchronization issues, or failures when uploading to media platforms. This article provides a quick guide to understanding why this happens and outlines the exact FFmpeg commands and flags you need to reset your video start time to zero.
Why Does FFmpeg Produce Negative Timestamps?
Negative start timestamps usually occur during stream copying
(-c copy) or when remuxing container formats (like MP4,
MKV, or TS). It happens because: * The source video contains B-frames
(bi-directional frames) which require a reordered presentation timestamp
(PTS) that can fall below zero relative to the audio. * The input file
has an offset or edit list designed to keep audio and video
synchronized, which FFmpeg preserves by default.
To fix this, you must instruct FFmpeg to shift or recalculate the timestamps during the muxing or encoding process.
Solution
1: Use the -avoid_negative_ts Flag (Recommended for Stream
Copying)
If you are copying video and audio streams without re-encoding, the
most efficient solution is the -avoid_negative_ts flag.
This shifts all timestamps so that the earliest packet starts exactly at
zero.
Run the following command:
ffmpeg -i input.mp4 -c copy -avoid_negative_ts make_zero output.mp4-avoid_negative_ts make_zero: Shifts all timestamps by a relative amount so that the first timestamp becomes exactly0. This fixes the negative start time without desynchronizing the audio and video.
Solution
2: Reset Timestamps with setpts and asetpts
(For Re-encoding)
If you are re-encoding your video (not using -c copy),
you can use video and audio filters to force the presentation timestamps
to start at zero.
Run the following command:
ffmpeg -i input.mp4 -vf "setpts=PTS-STARTPTS" -af "asetpts=PTS-STARTPTS" -c:v libx264 -c:a aac output.mp4-vf "setpts=PTS-STARTPTS": Subtracts the initial video timestamp from all subsequent video frames, resetting the start to zero.-af "asetpts=PTS-STARTPTS": Applies the same logic to the audio frames to maintain perfect synchronization.
Solution 3:
Force Start at Zero with -start_at_zero
If you are using the -copyts flag to preserve timestamps
but want to eliminate the negative start delay, you can pair it with the
-start_at_zero flag.
Run the following command:
ffmpeg -copyts -start_at_zero -i input.mp4 -c copy output.mp4-copyts: Tells FFmpeg to copy the input timestamps.-start_at_zero: Forces the output stream to shift its start time to zero, correcting the negative offset while retaining the original timestamp intervals.