diff --git a/pymediainfo/__init__.py b/pymediainfo/__init__.py index 694424d..b8ffce0 100644 --- a/pymediainfo/__init__.py +++ b/pymediainfo/__init__.py @@ -65,34 +65,38 @@ def __setstate__(self, state): # type: ignore def __init__(self, xml_dom_fragment: ET.Element): self.track_type = xml_dom_fragment.attrib["type"] + repeated_attributes = [] for elem in xml_dom_fragment: node_name = elem.tag.lower().strip().strip("_") if node_name == "id": node_name = "track_id" node_value = elem.text - other_node_name = "other_%s" % node_name if getattr(self, node_name) is None: setattr(self, node_name, node_value) else: + other_node_name = f"other_{node_name}" + repeated_attributes.append((node_name, other_node_name)) if getattr(self, other_node_name) is None: setattr(self, other_node_name, [node_value]) else: getattr(self, other_node_name).append(node_value) - for other in [d for d in self.__dict__.keys() if d.startswith("other_")]: + for primary_key, other_key in repeated_attributes: try: - primary = other.replace("other_", "") # Attempt to convert the main value to int - setattr(self, primary, int(getattr(self, primary))) + # Usually, if an attribute is repeated, one of its value + # is an int and others are human-readable formats + setattr(self, primary_key, int(getattr(self, primary_key))) except ValueError: - # If it fails, try to set the main value to an int taken from other values - for value in getattr(self, other): + # If it fails, try to find a secondary value + # that is an int and use it as main value + for value in getattr(self, other_key): try: - current = getattr(self, primary) + current = getattr(self, primary_key) # Set the main value to an int - setattr(self, primary, int(value)) + setattr(self, primary_key, int(value)) # Append its previous value to other values - getattr(self, other).append(current) + getattr(self, other_key).append(current) break except ValueError: pass diff --git a/tests/data/issue100.xml b/tests/data/issue100.xml new file mode 100644 index 0000000..6ec8500 --- /dev/null +++ b/tests/data/issue100.xml @@ -0,0 +1,25 @@ + + + + + 331 + 1 + General + General + 0 + 1 + 1 + 2 + AVC + AVC + AVC + AAC LC + AAC LC + AAC LC + RTP / RTP + RTP / RTP + RTP / RTP + English / English + + + diff --git a/tests/test_pymediainfo.py b/tests/test_pymediainfo.py index 354157e..13c5d94 100644 --- a/tests/test_pymediainfo.py +++ b/tests/test_pymediainfo.py @@ -56,11 +56,21 @@ def test_track_integer_attributes(self): break def test_track_other_attributes(self): - for track in self.media_info.tracks: - if track.track_type == "General": - self.assertEqual(5, len(track.other_file_size)) - self.assertEqual(4, len(track.other_duration)) - break + general_tracks = [ + track for track in self.media_info.tracks if track.track_type == "General" + ] + general_track = general_tracks[0] + self.assertEqual(5, len(general_track.other_file_size)) + self.assertEqual( + ["1mn 1s", "1mn 1s 394ms", "1mn 1s", "00:01:01.394"], general_track.other_duration + ) + + def test_track_existing_other_attributes(self): + with open(os.path.join(data_dir, "issue100.xml")) as f: + media_info = MediaInfo(f.read()) + general_tracks = [track for track in media_info.tracks if track.track_type == "General"] + general_track = general_tracks[0] + self.assertEqual(general_track.other_format_list, "RTP / RTP") def test_load_mediainfo_from_string(self): self.assertEqual(4, len(self.media_info.tracks))