Skip to content

Nudge non-monotonic source DTS instead of crashing the mux - #353

Open
ironicgitposting wants to merge 1 commit into
Kruk2:mainfrom
ironicgitposting:fix-non-monotonic-source-dts
Open

ironicgitposting wants to merge 1 commit into
Kruk2:mainfrom
ironicgitposting:fix-non-monotonic-source-dts

Conversation

@ironicgitposting

Copy link
Copy Markdown

Fixes #352.

Problem

_pump_source_streams muxes copied source packets verbatim. If the source audio (or subtitle) stream carries a duplicate/backwards DTS — common in web remuxes with 1/1000 audio time bases — the mp4 muxer hard-fails with non monotonically increasing dts to muxer and the whole job dies, potentially hours in. Deterministic: same packet, same crash, every run.

Fix

New _mux_source_packet helper tracks the last muxed DTS per output stream and nudges an offending packet forward by one tick (pts pulled up alongside if needed), then logs a warning. This mirrors what ffmpeg's CLI muxer does with such files, and what _clamp_pts_monotonic already does for the encoded video stream. Used by both the pump loop and _drain_source_streams' transcode flush.

Per-stream state lives in _setup_source_streams next to the rest of the pump state.

Testing

  • Added test_pump_nudges_non_monotonic_dts (duplicate DTS 370390 — the real-world value from the file I hit this on — comes out 370390, 370391 with pts matched).
  • tests/test_video_encoder_unit.py: 109 passed; the 3 failures (TestSharpening ×2, TestEncodeBuffer) also fail on a clean checkout in my env (CPU-only torch, no CUDA) and are unrelated.
  • Real-world validation of the diagnosis: ffprobe confirms the source file has two audio packets both at DTS 370390 ms, which is exactly the 17778720 (× 48, 48 kHz timescale) in the crash log.

🤖 Generated with Claude Code

Sloppy source files (e.g. web remuxes with 1/1000 audio time bases) can
carry duplicate or backwards DTS in their audio/subtitle streams. The
copy path in _pump_source_streams muxed those packets verbatim, and the
mp4 muxer hard-fails with 'non monotonically increasing dts', killing
the whole job partway through.

Track the last muxed DTS per output stream and nudge offending packets
forward by one tick, mirroring what ffmpeg's CLI muxer does and what
_clamp_pts_monotonic already does for the video stream.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encoder crashes with 'non monotonically increasing dts' when the source audio has duplicate DTS

1 participant