Catch invalid replaygain tag values (#2282)
authorOzGav <gavnosp@hotmail.com>
Mon, 21 Jul 2025 15:49:40 +0000 (01:49 +1000)
committerGitHub <noreply@github.com>
Mon, 21 Jul 2025 15:49:40 +0000 (17:49 +0200)
* Catch invalid replaygain tag values

* Update music_assistant/helpers/tags.py

Co-authored-by: Maxim Raznatovski <nda.mr43@gmail.com>
* Update music_assistant/helpers/tags.py

Co-authored-by: Maxim Raznatovski <nda.mr43@gmail.com>
* Update music_assistant/helpers/tags.py

Co-authored-by: Maxim Raznatovski <nda.mr43@gmail.com>
* Update music_assistant/helpers/tags.py

Co-authored-by: Maxim Raznatovski <nda.mr43@gmail.com>
---------

Co-authored-by: Maxim Raznatovski <nda.mr43@gmail.com>
music_assistant/helpers/tags.py

index 647d90f5e56ffd71751c68d03561db7ab7a8f85b..2e8d2bbbaf5210b2178eec6fb1fb53d9280b49f0 100644 (file)
@@ -383,20 +383,40 @@ class AudioTags:
 
     @property
     def track_loudness(self) -> float | None:
-        """Try to read/calculate the integrated loudness from the tags."""
-        if (tag := self.tags.get("r128trackgain")) is not None:
-            return -23 - float(int(tag.split(" ")[0]) / 256)
-        if (tag := self.tags.get("replaygaintrackgain")) is not None:
-            return -18 - float(tag.split(" ")[0])
+        """Try to read/calculate the integrated loudness from the tags (track level)."""
+        if tag := self.tags.get("r128trackgain"):
+            try:
+                gain_adjustment = int(tag.split(" ")[0]) / 256
+                return -23 - gain_adjustment
+            except (ValueError, IndexError) as e:
+                LOGGER.warning(f"Invalid r128trackgain tag value: {tag!r} — {e}")
+
+        if tag := self.tags.get("replaygaintrackgain"):
+            try:
+                gain_adjustment = float(tag.split(" ")[0])
+                return -18 - gain_adjustment
+            except (ValueError, IndexError) as e:
+                LOGGER.warning(f"Invalid replaygaintrackgain tag value: {tag!r} — {e}")
+
         return None
 
     @property
     def track_album_loudness(self) -> float | None:
         """Try to read/calculate the integrated loudness from the tags (album level)."""
         if tag := self.tags.get("r128albumgain"):
-            return -23 - float(int(tag.split(" ")[0]) / 256)
-        if (tag := self.tags.get("replaygainalbumgain")) is not None:
-            return -18 - float(tag.split(" ")[0])
+            try:
+                gain_adjustment = int(tag.split(" ")[0]) / 256
+                return -23 - gain_adjustment
+            except (ValueError, IndexError) as e:
+                LOGGER.warning(f"Invalid r128albumgain tag value: {tag!r} — {e}")
+
+        if tag := self.tags.get("replaygainalbumgain"):
+            try:
+                gain_adjustment = float(tag.split(" ")[0])
+                return -18 - gain_adjustment
+            except (ValueError, IndexError) as e:
+                LOGGER.warning(f"Invalid replaygainalbumgain tag value: {tag!r} — {e}")
+
         return None
 
     @classmethod