Try parsing track number from the filename (#1663)
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 14 Sep 2024 08:26:23 +0000 (10:26 +0200)
committerGitHub <noreply@github.com>
Sat, 14 Sep 2024 08:26:23 +0000 (10:26 +0200)
music_assistant/server/helpers/tags.py

index f0014b17362c44b6c1a4b896d3c9537939b23379..c60ea526de2ddda09f25329553a6de41b56802bf 100644 (file)
@@ -180,6 +180,16 @@ class AudioTags:
         """Return track tag if present."""
         if tag := self.tags.get("track"):
             return try_parse_int(tag.split("/")[0], None)
+        # fallback to parsing from filename (if present)
+        # this can be in the form of 01 - title.mp3
+        # or 01-title.mp3
+        # or 01.title.mp3
+        # or 01 title.mp3
+        # or 1. title.mp3
+        for splitpos in (4, 3, 2, 1):
+            firstpart = self.filename[:splitpos]
+            if firstpart.isnumeric():
+                return try_parse_int(firstpart, None)
         return None
 
     @property