Skip to content

MKVFile.add_track(str) uses stale existing_info from the source file, producing wrong track metadata #123

Description

@xMohnad

Describe the bug

When adding a track to an MKVFile by passing a file path as a str, MKVFile.add_track internally builds the new MKVTrack like this:

new_track = MKVTrack(
    track,
    mkvmerge_path=self.mkvmerge_path,
    existing_info=self._info_json,
)

self._info_json is the mkvmerge -J JSON output of the original file the MKVFile was constructed from, not the file being added. Since existing_info causes MKVTrack to read metadata from that JSON instead of running mkvmerge -J on the new file, and the new track defaults to track_id=0, the resulting MKVTrack ends up with metadata belonging to track 0 of the original file (typically the video track), completely unrelated to the file actually being added.

Creating an MKVTrack directly from the same path (without going through MKVFile.add_track) produces the correct metadata, confirming the bug is isolated to the existing_info=self._info_json reuse in MKVFile.add_track.

To Reproduce

from pymkv import MKVTrack, MKVFile

mkv = MKVFile("./source.mkv")

for track in mkv.tracks:
    print(track.track_name, track.track_codec)
# Japanese [Uncut]  HEVC/H.265/MPEG-H
# Japanese          AAC
# Arabic (Saudi Arabia)  SubStationAlpha

mkv.add_track("./subtitle.ass")

for track in mkv.tracks:
    print(track.track_name, track.track_codec)
# ... (original 3 tracks unchanged)
# None   HEVC/H.265/MPEG-H   <-- wrong: should be a subtitle track

# Compare with a direct MKVTrack construction:
track = MKVTrack("./subtitle.ass")
print(track.track_codec, track.track_type)
# SubStationAlpha  subtitles   <-- correct

Expected behavior

N/A

Screenshots

N/A

Software (please complete the following information):

  • OS: Arch Linux
  • MKVToolNix version mkvtoolnix-100.0-1

Additional context

N/A

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions