From eb95252a95f69168bf87e9cfc292e344ca7899f8 Mon Sep 17 00:00:00 2001 From: Robert Colfin Date: Wed, 20 Nov 2024 08:14:47 -0500 Subject: [PATCH 1/2] Exposing number of global and track tag entries. --- pymkv/MKVFile.py | 23 ++++++++++++++++++++++- pymkv/MKVTrack.py | 14 ++++++++++++++ tests/test_open_files.py | 3 +++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/pymkv/MKVFile.py b/pymkv/MKVFile.py index 40ac0aa..efb66c8 100644 --- a/pymkv/MKVFile.py +++ b/pymkv/MKVFile.py @@ -113,6 +113,7 @@ def __init__( self.attachments: list[MKVAttachment] = [] self._number_file = 0 self._info_json: dict[str, Any] | None = None + self._global_tag_entries = 0 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" @@ -141,13 +142,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"] @@ -167,6 +177,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 @@ -210,6 +221,16 @@ def chapter_language(self, language: str | None) -> None: raise ValueError(msg) self._chapter_language = language + @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( self, output_path: str, diff --git a/pymkv/MKVTrack.py b/pymkv/MKVTrack.py index 80f6e6d..8f36fd4 100644 --- a/pymkv/MKVTrack.py +++ b/pymkv/MKVTrack.py @@ -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 @@ -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 @@ -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 @@ -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: """ diff --git a/tests/test_open_files.py b/tests/test_open_files.py index 7475dbc..34faea1 100644 --- a/tests/test_open_files.py +++ b/tests/test_open_files.py @@ -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: From 09106210e87833a2028cf289154a72bf0951d643 Mon Sep 17 00:00:00 2001 From: Robert Colfin Date: Thu, 21 Nov 2024 13:36:37 -0500 Subject: [PATCH 2/2] adding option to disable-track-statistics-tags when calling mkvmerge. --- pymkv/MKVFile.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pymkv/MKVFile.py b/pymkv/MKVFile.py index efb66c8..3154564 100644 --- a/pymkv/MKVFile.py +++ b/pymkv/MKVFile.py @@ -115,6 +115,9 @@ def __init__( 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" raise FileNotFoundError(msg) @@ -231,7 +234,7 @@ def global_tag_entries(self) -> int: """ return self._global_tag_entries - def command( + def command( # noqa: PLR0915 self, output_path: str, subprocess: bool = False, @@ -256,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