From 4948dee4b9cdb4d5d0de549497927fe5ddf4f447 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 15 Dec 2014 22:46:00 +0100 Subject: [PATCH] audio: Make scanner return duration in milliseconds Also ensures that we normalize unknown duration to None instead of -1. --- mopidy/audio/scan.py | 14 +++++++++----- tests/audio/test_scan.py | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/mopidy/audio/scan.py b/mopidy/audio/scan.py index e79b038a..86c66f93 100644 --- a/mopidy/audio/scan.py +++ b/mopidy/audio/scan.py @@ -50,7 +50,7 @@ class Scanner(object): :type event: string :return: (tags, duration) pair. tags is a dictionary of lists for all the tags we found and duration is the length of the URI in - nanoseconds. No duration is indicated by -1 as in GStreamer. + milliseconds, or :class:`None` if the URI has no duration. """ try: self._setup(uri) @@ -61,7 +61,7 @@ class Scanner(object): if self._min_duration_ms is None: return tags, duration - elif duration >= self._min_duration_ms * gst.MSECOND: + elif duration >= self._min_duration_ms: return tags, duration raise exceptions.ScannerError('Rejecting file with less than %dms ' @@ -110,10 +110,15 @@ class Scanner(object): def _query_duration(self): try: - return self._pipe.query_duration(gst.FORMAT_TIME, None)[0] + duration = self._pipe.query_duration(gst.FORMAT_TIME, None)[0] except gst.QueryError: return None + if duration < 0: + return None + else: + return duration // gst.MSECOND + def _artists(tags, artist_name, artist_id=None): # Name missing, don't set artist @@ -165,8 +170,7 @@ def audio_data_to_track(data): track_kwargs['date'] = tags[gst.TAG_DATE][0].isoformat() track_kwargs['last_modified'] = int(data.get('mtime') or 0) - track_kwargs['length'] = max( - 0, (data.get(gst.TAG_DURATION) or 0)) // gst.MSECOND + track_kwargs['length'] = data.get('duration') # Clear out any empty values we found track_kwargs = {k: v for k, v in track_kwargs.items() if v} diff --git a/tests/audio/test_scan.py b/tests/audio/test_scan.py index c5cab9f0..ccecfbbb 100644 --- a/tests/audio/test_scan.py +++ b/tests/audio/test_scan.py @@ -20,7 +20,7 @@ class TranslatorTest(unittest.TestCase): def setUp(self): # noqa self.data = { 'uri': 'uri', - 'duration': 4531000000, + 'duration': 4531, 'mtime': 1234, 'tags': { 'album': ['album'], @@ -317,8 +317,8 @@ class ScannerTest(unittest.TestCase): def test_duration_is_set(self): self.scan(self.find('scanner/simple')) - self.check('scanner/simple/song1.mp3', 'duration', 4680000000) - self.check('scanner/simple/song1.ogg', 'duration', 4680000000) + self.check('scanner/simple/song1.mp3', 'duration', 4680) + self.check('scanner/simple/song1.ogg', 'duration', 4680) def test_artist_is_set(self): self.scan(self.find('scanner/simple'))