How FFmpeg -copyts Affects Stream Synchronization
This article explains how the -copyts (copy timestamps)
option in FFmpeg influences audio and video stream synchronization. By
default, FFmpeg resets input timestamps to start from zero, which can
sometimes disrupt the alignment between different media tracks. Enabling
-copyts preserves the original container timestamps,
ensuring that the relative timing between audio, video, and subtitle
streams remains intact, which is critical for maintaining
synchronization in complex editing workflows.
Default FFmpeg Behavior
vs. -copyts
When FFmpeg processes an input file, its default behavior is to demux the packets and shift their presentation timestamps (PTS) so that the output stream starts at or very close to zero. While this is helpful for standard playback compatibility, it strips away the absolute timing data of the source media.
By applying the -copyts option, you instruct FFmpeg to
bypass this shifting process. The tool copies the exact timestamps from
the input container directly to the output muxer.
Why
-copyts is Crucial for Stream Synchronization
Using -copyts directly impacts stream synchronization in
several key scenarios:
- Preserving Initial Offsets: In many media files, the audio and video streams do not start at the exact same millisecond. For example, the video might start 500 milliseconds after the audio. If FFmpeg shifts both streams to start at zero independently, it destroys this 500ms offset, resulting in out-of-sync audio. Preserving the original timestamps maintains this initial gap.
- Concatenation and Splicing: When merging or
concatenating multiple video segments, maintaining the original timeline
is essential. Without
-copyts, the timestamps of subsequent segments may overlap or gap incorrectly, causing players to freeze, drop frames, or lose audio-video sync. - Live Streams and HLS Processing: Live feeds (like RTMP or HLS) rely on absolute epoch timestamps to keep players in sync with the broadcast. Resetting these timestamps to zero breaks the continuity required for adaptive bitrate switching and live DVR controls.
Potential Issues and Complementary Options
While -copyts is vital for synchronization, using it
blindly can sometimes cause playback issues, as some media players
struggle to decode files that do not start near zero.
To resolve this, -copyts is frequently paired with the
-start_at_zero option. When used together: 1.
-copyts preserves the exact relative synchronization and
distance between the audio and video packets. 2.
-start_at_zero shifts the entire synchronized timeline back
so that the earliest stream starts at zero, satisfying player
requirements without causing synchronization drift.