Repository navigation
feat(chapters): trim clips at the end of a source that decodes short - #83
Closed
ziv-lazarov-nagish wants to merge 1 commit into
Closed
ziv-lazarov-nagish wants to merge 1 commit into
ziv-lazarov-nagish wants to merge 1 commit into
Conversation
Headers can count frames that never decode. A clip past the last decoded frame made write_chapters fail with "zip() argument 2 is shorter than argument 1", so callers had to decode the whole source a second time to count its frames first. With trim_to_source=True a clip ends at the last frame that decodes, and a clip starting past it is dropped. Matroska writes chapters in the header, so a trimmed file is remuxed once (packets copied with their timestamps) with the chapter table and payloads of the clips written. The default still raises. write_chapters now returns the clips written. SIGN-1242
Contributor
|
Opened a competing implementation in #87. It keeps the same public behavior and the same overall approach: detect EOF during encoding, then packet-copy remux only when the chapter table needs correcting. The main differences are:
The potential improvement is clearer ownership of the EOF behavior and safer staging, rather than a different algorithm or a claimed speedup. The remux itself still seems justified. Local validation: lint and diff checks pass; the full suite passed with 392 tests, and all 116 chapter tests passed after the final assertion additions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some headers count frames that never decode. A
ChapterClipreaching past the last decoded frame makeswrite_chaptersfail withzip() argument 2 is shorter than argument 1. In signer-detection's export this is 9,938 videos of the 2026-10-25 failure report. Today the only way around it is tocount_framesfirst, which decodes the whole source a second time: 21 s on an 8-minute 1080p video, althoughwrite_chaptersdecodes every frame it needs anyway.Change
write_chapters(..., trim_to_source=True):write_chaptersreturns the clips written (withtrim_to_sourceoff, the clips given).The default is unchanged: a clip past the source's end still raises.
Relation to #82
Timelineandread_chaptersread trimmed files like any other.Notes
packet.size, as_count_video_packetsdoes. A first version checkeddts is None, but when chapter sizes change and change back (e.g. 48 → 16 → 48 px), Matroska demuxes every packet without a dts, and that version wrote an empty file. The new test uses that layout and fails with thedtscheck.write_chaptersgrew past ruff's complexity limit, so the clip loop moved into_write_clipsand the chapter table into_chapters.Checks
ruff check .passes.pytest: 361 passed. The 4 failures intest_regression.pyalso fail on main on this Mac: the local FFmpeg rejects-vsync.count_frameswent from 44.6 s to 23.5 s (warm, MPS), with frame-identical output.Linear: SIGN-1242
🤖 Generated with Claude Code
Note
Medium Risk
Touches core chapter mux/encode paths and changes
write_chaptersto return a value; remux only runs when trimming occurs but incorrect chapter metadata would break downstream timeline reads.Overview
Adds
trim_to_sourcetowrite_chaptersso chapter exports can finish when clip ranges exceed what the decoder actually yields (e.g. header frame counts vs. real stream length), without a separate full-source frame count pass.With
trim_to_source=True, clips that run past the last decoded frame are shortened, clips that start after that point are skipped, and the function returns theChapterCliplist that was written (with updatedend_framevalues). Default behavior is unchanged: out-of-range clips still raise.Because Matroska chapters are written in the header before muxed video, any trim triggers a packet-copy remux (
_remux_with_chapters) so chapter times and JSON payloads match the encoded spans; remux skips zero-size flush packets so variable chapter dimensions still copy correctly.Clip encoding is refactored into
_write_clips/_chapters;_write_clipgains a non-strict mode and returns the number of frames written.Reviewed by Cursor Bugbot for commit 24ce44a. Bugbot is set up for automated code reviews on this repo. Configure here.