diff options
| author | jvoisin | 2015-12-02 17:07:19 +0100 |
|---|---|---|
| committer | jvoisin | 2015-12-02 17:22:45 +0100 |
| commit | 80ece3001895ea13d50915a5215fd47e313bab4c (patch) | |
| tree | c5ede43867c5d7fe2af4178b34b0e6dc219f6aac /libmat/mutagenstripper.py | |
| parent | 3cf80e8b5d6faf410e9ad3aad77f23cf6418a587 (diff) | |
Remove hachoir from MAT.
This (huge) commit removes completely hachoir from MAT.
Audio files are now processed with mutagen, and images
with exiftool, since the main python imaging library (PIL)
isn't super-great to deal with metadata (and damaged/non-standard
files).
Package maintainer should change the dependencies to reflect this.
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 | ||
