fix: add_track(str) no longer reuses the source file's mkvmerge info - #129
Merged
Conversation
MKVFile.add_track passed self._info_json - the mkvmerge -J output of the
file the MKVFile was built from - as existing_info for a track created
from an unrelated path. MKVTrack then read metadata out of that JSON
instead of probing the file, and since track_id defaults to 0 the new
track reported track 0 of the original file:
mkv = MKVFile("file.mkv")
mkv.add_track("subtitle.srt")
mkv.tracks[-1].track_type # 'video' instead of 'subtitles'
Same line also suppressed verification: MKVTrack.file_path skips
verify_supported when _info_json is set, so any path was accepted.
Fixes #123
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #129 +/- ##
==========================================
- Coverage 99.11% 98.92% -0.20%
==========================================
Files 21 21
Lines 1477 1487 +10
==========================================
+ Hits 1464 1471 +7
- Misses 13 16 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
MKVTrack ran mkvmerge -J twice per construction. The file_path setter called verify_supported(), which fetches the info and returns only a bool, and the track_id setter then fetched the same JSON again because _info_json was still empty. The cause is that _info_json served two roles at once: cache of the probe, and flag for "caller supplied the info, skip verification". That same conflation produced #123. Split them with _info_path, the path the cached info describes: - the file_path setter probes once and keeps the output, so track_id has nothing left to fetch - pointing a track at a different file drops the cache, so the track always describes the file it currently points at - existing_info is trusted for the first assignment and stops being trusted once the path changes - verify_supported is replaced by info.container.supported, which is exactly what that function returns. checking_file_path already validated the path, so only the mkvmerge lookup remains, and it is cached This also fixes stale metadata on reassignment. Before, pointing a track at another file kept the previous file's type and codec, and skipped verification of the new path: t = MKVTrack("sub.srt") # subtitles t.file_path = "video.mkv" t.track_type # 'subtitles' — wrong 20 iterations of MKVFile + add_track + command: 1.56s -> 1.10s, with add_track itself halved from 1.01s to 0.53s. Tests dropped 20 patches of verify_supported in the MKVTrack namespace; they targeted a function that is no longer called and would have passed even with verification broken. test_track_file_path_setter_verification_failure now uses a real empty file, which mkvmerge reports as unsupported.
This was referenced Aug 2, 2026
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.
Fixes #123.
The issue reports wrong metadata. It is worse than that — the added track is silently dropped from the muxed output.
generated command, trimmed to the added file:
The track is built with
existing_info=self._info_json, which is themkvmerge -Joutput of the fileMKVFilewas constructed from, not ofs.srt.MKVTrackreads metadata from that JSON instead of probing the file, andtrack_iddefaults to 0, so the SRT reports itself as track 0 of the source — video.TrackOptionsthen asks the SRT for a video track and explicitly suppresses subtitles. An SRT has no video track, so nothing is taken from it.The same line also suppressed verification:
MKVTrack.file_pathskipsverify_supportedwhen_info_jsonis set, so any path was accepted through this branch.Nothing is lost by dropping it.
MKVFile.__init__builds its tracks withexisting_info=info_structdirectly and passesMKVTrackobjects toadd_track, so thestrbranch is never used while loading a file — the reuse only ever applied to caller-supplied paths, where it was wrong by construction.Behaviour change worth noting:
add_track(str)on anMKVFilebuilt from a file now raisesValueErrorfor an unsupported path instead of accepting it silently. Code that relied on the old silence was already producing a broken mux, but it will now fail loudly.Cost:
add_track(str)now runsmkvmerge -Jon the added file, about 33 ms here.MKVTrack(path)already pays exactly this, soadd_track(str)just costs the same as the equivalent explicit construction.Both new tests fail on master and pass with the fix — the second one covers the verification bypass, which is
DID NOT RAISEbefore the change.Second commit: probe each track file once instead of twice.
Profiling the fix showed
MKVTrackrunningmkvmerge -Jtwice per construction —verify_supported()in thefile_pathsetter fetches the info and returns only a bool, then thetrack_idsetter fetches the same JSON again. Same root cause as above:_info_jsondoubles as both the probe cache and the "caller vouched, skip verification" flag.Split by
_info_path, the path the cached info describes. The setter now probes once and keeps the result,existing_infois trusted for the first assignment only, and pointing the track elsewhere drops the cache.This also fixes stale metadata on reassignment, which was a second latent bug of the same family:
Verification of the new path was skipped too, for the same reason.
20 iterations of
MKVFile+add_track+command: 1.56s → 1.10s,add_trackitself halved from 1.01s to 0.53s.verify_supportedis replaced byinfo.container.supported, which is literally what that function returns;checking_file_pathhad already validated the path, so only the mkvmerge lookup remains and that one is cached.Test churn worth flagging: 20 patches of
verify_supportedin theMKVTracknamespace are gone. They targeted a function that is no longer called, so leaving them would mean tests passing even with verification broken.test_track_file_path_setter_verification_failurenow uses a real empty file, which mkvmerge reports as unsupported, instead of mocking the check away.Both new reassignment tests fail without this commit.