-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_file_scanner.py
53 lines (42 loc) · 1.61 KB
/
audio_file_scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
iterates over files returning structured audio metadata
Provides a class for iterating over the specified files, whether given
as individual files or directories. For each file, attempts to open
them and observe their metadata; skips files that fail to open as
audio files.
Copyright © 2014 Christopher R. Maden.
This code may be freely copied, distributed, and reused without
restriction. It would be nice if you gave me credit. There is no
warranty; it might destroy all your data. In fact, it probably will.
"""
__author__ = u"Christopher R. Maden <[email protected]>"
__date__ = u"11 March 2014"
__version__ = 0.1
from file_scanner import FileScanner
import mutagen
class AudioFileScanner( FileScanner ):
"""
Given a list of filenames, acts as an iterator over all specified
files. If specified filenames include directories, recursively
descends into directories.
Attempts to open each file using the mutagen library. Skips
unsuccessful files.
Returns files as instances of mutagen.FileType or some subtype
thereof.
Raises IOError if filenames do not exist.
"""
def next( self ):
"""
Let FileScanner identify the next filename to process.
Attempt to open it with mutagen; return the resulting object
if successful.
"""
audio_filename = self.get_next_filename()
audio_file = mutagen.File( audio_filename )
# mutagen will return None if it doesn’t know what to do with
# it.
if audio_file is None:
return self.next()
return audio_file