FFmpeg -copyts and Stream Synchronization Explained
The -copyts (copy timestamps) flag in FFmpeg forces the
demuxer and muxer to preserve the original Presentation Timestamps (PTS)
and Decode Timestamps (DTS) from the input file rather than resetting
them to zero. By keeping the absolute timestamps intact,
-copyts directly impacts stream synchronization by
preserving the exact time offsets between audio, video, and subtitle
tracks, preventing audio/video (A/V) drift and misalignments that
frequently occur during seeking, stream extraction, or segmenting.
Default FFmpeg Behavior vs. -copyts
By default, FFmpeg adjusts timestamps so that the output stream
begins cleanly at t = 0. When processing a media
container:
- FFmpeg reads the earliest packet's timestamp.
- It subtracts that initial offset from all subsequent audio and video packets.
- This creates an output file where playback begins immediately at timestamp zero.
When -copyts is enabled, FFmpeg bypasses this
zero-offset recalculation. If an input video stream starts at
00:01:30.500 and its corresponding audio stream starts at
00:01:30.520, the output will reflect those exact
timestamps rather than mapping both back to zero.
Impact on Audio and Video (A/V) Synchronization
The primary benefit of -copyts is the preservation of
relative track timing, particularly when using seeking options:
- Preventing Independent Track Resets: In many
containers, audio and video packets do not start at the exact same
millisecond. If FFmpeg independently normalizes the video stream to zero
and the audio stream to zero during a seek or transcode, the slight
offset between them can collapse. This leads to subtle A/V
desynchronization (lip-sync errors). Using
-copytsmaintains the initial inter-stream gap. - Accurate Input Seeking (
-sswith-copyts): When performing an input seek (ffmpeg -ss ... -i ...), FFmpeg normally discards frames before the seek point and resets the remaining frames to start at zero. Adding-copytsensures that the retained audio and video packets retain their native timeline alignment relative to one another.
Use with
-start_at_zero
A common issue with using -copyts alone is that many
media players and hardware decoders cannot handle non-zero start times
properly, causing frozen frames, black screens, or playback failures
until the playhead reaches the initial timestamp.
To fix this while maintaining synchronization, FFmpeg provides the
-start_at_zero option. When used in tandem:
ffmpeg -ss 00:02:00 -copyts -start_at_zero -i input.mp4 -c copy output.mp4In this pipeline:
-copytspreserves the micro-offsets and synchronization between the separate streams.-start_at_zeroshifts the entire synchronized package backward uniformly so that the earliest track begins at00:00:00.000.
Behavior in Multi-Stream and Broadcast Workflows
In MPEG-TS, HLS, or multi-camera setups, streams rely on absolute Program Clock Reference (PCR) or wall-clock timestamps. Stripping these timestamps disrupts downstream multiplexers or chunk-slicing mechanisms.
- Chunked Streaming (HLS/DASH):
-copytsensures that contiguous segments share an uninterrupted timeline, preventing media players from stalling or losing audio sync across segment boundaries. - Concatenation and Splicing: When merging segmented recordings, copying original timestamps prevents audio packets from bleeding across cuts or overlapping adjacent video frames.
Potential Synchronization Pitfalls
While -copyts solves alignment issues, it also
replicates timing defects present in the source material:
- Timestamp Gaps: If the original file has missing
packets or time jumps,
-copytsreplicates that gap in the output timeline, which can cause decoders to pause video while waiting for audio to catch up. - Negative Timestamps: If an input uses negative
presentation timestamps (common with B-frame reordering),
-copytsretains them unless explicitly shifted with thesetptsorasetptsfilters, which can cause issues in older player environments.