From 6bf997097acfd3fce5ab6606b2259fa9beabaf26 Mon Sep 17 00:00:00 2001 From: Azsde Date: Wed, 26 Oct 2022 11:10:53 +0200 Subject: [PATCH] [BUGFIX #75] Fix successive track removal --- pymkv/MKVFile.py | 21 +++++++++++---------- pymkv/MKVTrack.py | 3 +++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pymkv/MKVFile.py b/pymkv/MKVFile.py index cd87088..de3a90b 100644 --- a/pymkv/MKVFile.py +++ b/pymkv/MKVFile.py @@ -42,7 +42,7 @@ import bitmath -from pymkv.MKVTrack import MKVTrack +from pymkv.MKVTrack import MKVTrack, MKVTrackIdNotFound from pymkv.MKVAttachment import MKVAttachment from pymkv.Timestamp import Timestamp from pymkv.ISO639_2 import is_ISO639_2 @@ -478,23 +478,24 @@ def replace_track(self, track_num, track): else: raise IndexError('track index out of range') - def remove_track(self, track_num): + def remove_track(self, track_id): """Remove a track from the :class:`~pymkv.MKVFile` object. Parameters ---------- - track_num : int - The track number of the track to remove. + track_id: int + The track id of the track to remove. Raises ------ - IndexError - Raised if `track_num` is is out of range of the track list. + MKVTrackIdNotFound + Raised if `track_id` is not found in the track list. """ - if 0 <= track_num < len(self.tracks): - del self.tracks[track_num] - else: - raise IndexError('track index out of range') + for track in self.tracks: + if (track.track_id == track_id): + self.tracks.remove(track) + return + raise MKVTrackIdNotFound('Track id not found in the track list') def split_none(self): """Remove all splitting options.""" diff --git a/pymkv/MKVTrack.py b/pymkv/MKVTrack.py index 245daad..800a4a8 100644 --- a/pymkv/MKVTrack.py +++ b/pymkv/MKVTrack.py @@ -258,3 +258,6 @@ def track_codec(self): def track_type(self): """str: The type of track such as video or audio.""" return self._track_type + +class MKVTrackIdNotFound(Exception): + pass \ No newline at end of file