4 Commits
Author SHA1 Message Date
Scott GarrettandClaude Code 10dcee30a6 Round trip the chapter language between model and Matroska
ChapterDisplay/ChapterLanguage (0x437C) was read by the vendored parser
but never surfaced, and the remuxer's chapter builder never wrote it, so
any non-English chapter language silently degraded to the spec default
"eng" the first time Subler rewrote the Chapters element.

The importer now adopts the first ChapterDisplay language as the model
track's language (the model is one-language-per-track; MKV allows
per-atom but every common tool stamps one value). The remuxer stamps the
track language back into every atom as ISO-639-2/B, omitting it when nil
or eng so language-less files keep byte-identical rewrites. Unknown
extended tags map to "und" via MP42Languages and are written explicitly,
which is truthful rather than silently defaulting.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-26 18:17:22 -05:00
Scott GarrettandClaude Code 0cf79eca1c Rebuild Matroska chapters on remux and recompute Info Duration
MP42MkvRemuxer gains replacementChapters: when set, the Chapters element
is rebuilt from the model track instead of copied verbatim (chapterCount
0 removes the element and its SeekHead entry). Chapter timecodes are
written as absolute nanoseconds — libmatroska/mkvtoolnix/ffmpeg all
treat them as ns regardless of TimecodeScale, confirmed by hex-dumping
a mkvmerge-written file and reading it back with mkvinfo and ffprobe.
ChapterAtom is 0xB6 (0xB0 is PixelWidth), and mandatory EditionUID/
ChapterUID are derived deterministically from the content because
ffmpeg silently skips atoms without a ChapterUID. Starts are sorted
ascending with ties clamped to previous+1ms (mkvmerge rejects
non-ascending), empty titles get the importer's "Chapter N" fallback,
and the last atom's end rides the track duration only when known.

An existing Info Duration is recomputed as the greatest written block
start+1ms: the Info payload is rewritten with a fixed 8-byte double
slot patched after the clusters stream past, converted to TimecodeScale
units. Added blocks measure start+1 like source blocks so a second
no-op remux stays byte-identical (including BlockDuration broke
idempotence). Files without a source Duration keep having none.

The importer's chapter /1e6 needed no change (Start is already ns); a
comment now records that so it doesn't get "fixed" to scale-based math.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-26 12:54:43 -05:00
Scott GarrettandClaude Code 8ba3f25a2c Write added subtitle tracks during Matroska remux
MP42MkvRemuxer previously refused any track present in currentTracks
but not in sourceTracks; it now accepts added MP42SubtitleTracks whose
URL extension is srt/ssa/ass and muxes them into the rebuilt file as
S_TEXT/UTF8. The extension gate mirrors the app-side
DocumentWindowController.matroskaSubtitleExtensions list — the two
must stay in sync.

- Each added track gets an appended TrackEntry: TrackNumber = max of
  all entries + 1 (refuse past 0x3FFF, the 2-byte unmarked-vint
  limit), TrackType 17, FlagEnabled 1, FlagLacing 0, CodecID
  S_TEXT/UTF8, ISO 0x22B59C + IETF 0x22B59D language, Name only when
  it differs from the codec default.
- The model never holds subtitle text, so lines are re-parsed from
  track.URL at save time: SRT via LoadSRTFromURL + serializer drain,
  SSA/ASS via MP42SSAParser/MP42SSAConverter (styling stripped, same
  lossy path the tx3g muxer takes). Zero parsed lines refuses.
- Blocks are written as BlockGroup{Block(unmarked-vint track number,
  int16 BE relative timecode, UTF-8 text), BlockDuration(>=1 ms)}.
  The BlockGroup wrapper is mandatory: a bare Block as a direct
  Cluster child is silently ignored by libebml readers even though
  ffmpeg tolerates it.
- Lines are assigned to the last existing Cluster whose Timecode is
  <= the line's start time (inheriting missing cluster timecodes,
  negative relative timecodes are legal); lines whose relative
  timecode would exceed +32767 flush as synthesized Clusters right
  after that cluster. Original clusters are copied verbatim and
  never split.
- Added tracks join the existing per-track CueTrackPositions
  bookkeeping, and the drop-all-remaining guard still fires before
  additions, so drop-all-plus-add stays refused.

Adds TrackType/FlagLacing/CodecID/BlockDuration element ids to
MP42MatroskaTagMapping.h.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-26 11:53:17 -05:00
Scott GarrettandClaude Code 1040f47542 Add Matroska tagging and track-removal support
- MP42Ebml: EBML vint/ID primitives, flat Segment scanner, block track
  number decoding, and element builders shared by all Matroska writers.
- MP42MkvImporter / MP42FileImporter: tagsOnly mode that mirrors the
  file's own track flags instead of the MP4 conversion heuristics.
- MP42MatroskaTagMapping: read/write SimpleTag maps shared by importer
  and writer, including community names for MP4-only movie fields.
- MP42MkvTagWriter: mkvpropedit-style in-place rewrite of Tags, cover
  art, and per-track name/language/enabled/forced fields with a
  size+mtime staleness guard; an untouched save is a byte-for-byte
  no-op.
- MP42MkvRemuxer: stream-copy rebuild that drops the blocks of removed
  tracks (TrackNumbers keep their gaps; block bytes are never
  rewritten), regenerates SeekHead and Cues, carries metadata edits in
  the same pass, and writes via temp file + atomic rename.
- MP42MkvTagWriter+Internal.h exposes the tag writer's scanning and
  payload builders so the remuxer reuses them unchanged.

Verified with the external matrix harness: 241 assertions green across
layout fixtures (ffmpeg/mkvmerge/streamed), removal scenarios, cue
parity, and idempotency.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-25 19:48:09 -05:00