Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions pymkv/MKVFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def __init__(
self.attachments: list[MKVAttachment] = []
self._number_file = 0
self._info_json: dict[str, Any] | None = None
self._global_tag_entries = 0

# exclusions
self.no_track_statistics_tags = False

if not verify_mkvmerge(mkvmerge_path=self.mkvmerge_path):
msg = "mkvmerge is not at the specified path, add it there or changed mkvmerge_path property"
Expand Down Expand Up @@ -141,13 +145,22 @@ def __init__(
if self.title is None and "title" in info_json["container"]["properties"]:
self.title = info_json["container"]["properties"]["title"]

self._global_tag_entries = sum(t["num_entries"] for t in info_json.get("global_tags", []))

# dictionary associating track_id to the number of tag entries:
track_tag_entries: dict[int, int] = {
t["track_id"]: t["num_entries"] for t in self._info_json.get("track_tags", [])
}

# add tracks with info
for track in info_json["tracks"]:
track_id = track["id"]
new_track = MKVTrack(
file_path,
track_id=track["id"],
track_id=track_id,
mkvmerge_path=self.mkvmerge_path,
existing_info=self._info_json,
tag_entries=track_tag_entries.get(track_id, 0),
)
if "track_name" in track["properties"]:
new_track.track_name = track["properties"]["track_name"]
Expand All @@ -167,6 +180,7 @@ def __init__(
new_track.flag_visual_impaired = track["properties"]["flag_visual_impaired"]
if "flag_original" in track["properties"]:
new_track.flag_original = track["properties"]["flag_original"]

self.add_track(new_track, new_file=False)

# split options
Expand Down Expand Up @@ -210,7 +224,17 @@ def chapter_language(self, language: str | None) -> None:
raise ValueError(msg)
self._chapter_language = language

def command(
@property
def global_tag_entries(self) -> int:
"""
Gets the number of global tag entries in the MKVFile object.

Returns:
int: The number of entries.
"""
return self._global_tag_entries

def command( # noqa: PLR0915
self,
output_path: str,
subprocess: bool = False,
Expand All @@ -235,6 +259,9 @@ def command(
command = [*self.mkvmerge_path, "-o", output_path]
if self.title is not None:
command.extend(["--title", self.title])
if self.no_track_statistics_tags:
# Do not write tags with track statistics.
command.append("--disable-track-statistics-tags")
track_order = []
for track in self.tracks:
# for track_order
Expand Down
14 changes: 14 additions & 0 deletions pymkv/MKVTrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class MKVTrack:
The path where pymkv looks for the mkvextract executable. pymkv relies on the mkvextract executable to extract
files. By default, it is assumed mkvextract is in your shell's $PATH variable. If it is not, you need to set
*mkvextract_path* to the executable location.
tag_entries : int, optional
The number of tag entries.
Attributes
----------
mkvmerge_path : list
Expand Down Expand Up @@ -131,6 +133,7 @@ def __init__( # noqa: PLR0913
mkvextract_path: str | os.PathLike | Iterable[str] = "mkvextract",
sync: int | None = None,
existing_info: dict[str, Any] | None = None,
tag_entries: int = 0,
) -> None:
from pymkv.TypeTrack import get_track_extension

Expand Down Expand Up @@ -163,6 +166,7 @@ def __init__( # noqa: PLR0913
self.flag_hearing_impaired = flag_hearing_impaired
self.flag_visual_impaired = flag_visual_impaired
self.flag_original = flag_original
self._tag_entries = tag_entries

# exclusions
self.no_chapters = False
Expand Down Expand Up @@ -433,6 +437,16 @@ def tags(self, file_path: str | os.PathLike | None) -> None:
raise FileNotFoundError(msg)
self._tags = str(file_path)

@property
def tag_entries(self) -> int:
"""
Gets the number of existing tag entries in the track.

Returns:
int: The number of entries.
"""
return self._tag_entries

@property
def track_codec(self) -> str | None:
"""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_open_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def test_open_file(get_path_test_file: Path) -> None:

assert mkv.title is None
assert len(mkv.tracks) == 2 # noqa: PLR2004
assert mkv.global_tag_entries == 4 # noqa: PLR2004
assert mkv.tracks[0].tag_entries == 3 # noqa: PLR2004
assert mkv.tracks[1].tag_entries == 4 # noqa: PLR2004


def test_mux_file(get_base_path: Path, get_path_test_file: Path) -> None:
Expand Down