]> git.0d.be Git - django-panik-nonstop.git/commitdiff
use mediainfo (and soxi if it fails) to get durations
authorFrédéric Péters <fpeters@0d.be>
Wed, 11 Jul 2018 17:27:17 +0000 (19:27 +0200)
committerFrédéric Péters <fpeters@0d.be>
Wed, 11 Jul 2018 17:27:17 +0000 (19:27 +0200)
nonstop/management/commands/compute_durations.py

index a505fecdf98ad75f55ed2334173dad5734a9f67a..7eab2abae3326aaf12f07829525a69878b822947 100644 (file)
@@ -1,10 +1,7 @@
 import datetime
 import os
-
-import mutagen
-import mutagen.aiff
-import mutagen.mp4
-import mutagen.asf
+import re
+import subprocess
 
 from django.core.management.base import BaseCommand, CommandError
 
@@ -12,19 +9,18 @@ from ...models import NonstopFile, Track
 
 
 def get_duration(filename):
-    klasses = {'.m4a': mutagen.mp4.MP4,
-               '.wma': mutagen.asf.ASF,
-               '.wav': None,
-               '.WAV': None,
-        }
-    klass = klasses.get(os.path.splitext(filename)[-1], mutagen.File)
-    if klass:
-        try:
-            return klass(filename).info.length
-        except (AttributeError, mutagen.aiff.InvalidChunk):
-            # fallback on soxi
-            pass
+    p = subprocess.Popen(['mediainfo', '--Inform=Audio;%Duration%', filename],
+                         close_fds=True,
+                         stdin=subprocess.PIPE,
+                         stdout=subprocess.PIPE,
+                         stderr=subprocess.PIPE)
+    stdout, stderr = p.communicate()
+    try:
+        return int(stdout) / 1000
+    except ValueError:
+        pass
 
+    # fallback on soxi
     p = subprocess.Popen(['soxi', filename], close_fds=True,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,