audio: Make scanner return duration in milliseconds

Also ensures that we normalize unknown duration to None instead of -1.
This commit is contained in:
Thomas Adamcik 2014-12-15 22:46:00 +01:00
parent d9d501cd98
commit 4948dee4b9
2 changed files with 12 additions and 8 deletions

View File

@ -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}

View File

@ -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'))