diff options
Diffstat (limited to 'libmat/mutagenstripper.py')
| -rw-r--r-- | libmat/mutagenstripper.py | 66 |
1 files changed, 64 insertions, 2 deletions
diff --git a/libmat/mutagenstripper.py b/libmat/mutagenstripper.py index 0f9520a..692c56f 100644 --- a/libmat/mutagenstripper.py +++ b/libmat/mutagenstripper.py | |||
| @@ -3,11 +3,15 @@ | |||
| 3 | 3 | ||
| 4 | import parser | 4 | import parser |
| 5 | 5 | ||
| 6 | from mutagen.flac import FLAC | ||
| 7 | from mutagen.oggvorbis import OggVorbis | ||
| 8 | from mutagen.mp3 import MP3 | ||
| 9 | |||
| 6 | 10 | ||
| 7 | class MutagenStripper(parser.GenericParser): | 11 | class MutagenStripper(parser.GenericParser): |
| 8 | """ Parser using the (awesome) mutagen library. """ | 12 | """ Parser using the (awesome) mutagen library. """ |
| 9 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): | 13 | def __init__(self, filename, mime, backup, is_writable, **kwargs): |
| 10 | super(MutagenStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs) | 14 | super(MutagenStripper, self).__init__(filename, mime, backup, is_writable, **kwargs) |
| 11 | self.mfile = None # This will be instanciated in self._create_mfile() | 15 | self.mfile = None # This will be instanciated in self._create_mfile() |
| 12 | self._create_mfile() | 16 | self._create_mfile() |
| 13 | 17 | ||
| @@ -36,3 +40,61 @@ class MutagenStripper(parser.GenericParser): | |||
| 36 | for key, value in self.mfile.tags: | 40 | for key, value in self.mfile.tags: |
| 37 | metadata[key] = value | 41 | metadata[key] = value |
| 38 | return metadata | 42 | return metadata |
| 43 | |||
| 44 | |||
| 45 | class MpegAudioStripper(MutagenStripper): | ||
| 46 | """ Represent a mp3 vorbis file | ||
| 47 | """ | ||
| 48 | def _create_mfile(self): | ||
| 49 | self.mfile = MP3(self.filename) | ||
| 50 | |||
| 51 | def get_meta(self): | ||
| 52 | """ | ||
| 53 | Return the content of the metadata block is present | ||
| 54 | """ | ||
| 55 | metadata = {} | ||
| 56 | if self.mfile.tags: | ||
| 57 | for key in self.mfile.tags.keys(): | ||
| 58 | meta = self.mfile.tags[key] | ||
| 59 | try: # Sometimes, the field has a human-redable description | ||
| 60 | desc = meta.desc | ||
| 61 | except AttributeError: | ||
| 62 | desc = key | ||
| 63 | text = meta.text[0] | ||
| 64 | metadata[desc] = text | ||
| 65 | return metadata | ||
| 66 | |||
| 67 | |||
| 68 | class OggStripper(MutagenStripper): | ||
| 69 | """ Represent an ogg vorbis file | ||
| 70 | """ | ||
| 71 | def _create_mfile(self): | ||
| 72 | self.mfile = OggVorbis(self.filename) | ||
| 73 | |||
| 74 | |||
| 75 | class FlacStripper(MutagenStripper): | ||
| 76 | """ Represent a Flac audio file | ||
| 77 | """ | ||
| 78 | def _create_mfile(self): | ||
| 79 | self.mfile = FLAC(self.filename) | ||
| 80 | |||
| 81 | def remove_all(self): | ||
| 82 | """ Remove the "metadata" block from the file | ||
| 83 | """ | ||
| 84 | super(FlacStripper, self).remove_all() | ||
| 85 | self.mfile.clear_pictures() | ||
| 86 | self.mfile.save() | ||
| 87 | return True | ||
| 88 | |||
| 89 | def is_clean(self): | ||
| 90 | """ Check if the "metadata" block is present in the file | ||
| 91 | """ | ||
| 92 | return super(FlacStripper, self).is_clean() and not self.mfile.pictures | ||
| 93 | |||
| 94 | def get_meta(self): | ||
| 95 | """ Return the content of the metadata block if present | ||
| 96 | """ | ||
| 97 | metadata = super(FlacStripper, self).get_meta() | ||
| 98 | if self.mfile.pictures: | ||
| 99 | metadata['picture:'] = 'yes' | ||
| 100 | return metadata | ||
