diff options
| author | jvoisin | 2011-07-27 16:37:59 +0200 |
|---|---|---|
| committer | jvoisin | 2011-07-27 16:37:59 +0200 |
| commit | 37ba35d1b2361b742f5aeab1ed7a202a54052a43 (patch) | |
| tree | fe164f9b1879660d59873503259724993cd044ed /lib | |
| parent | 52803d469598b5ab99f8e1cd6c2e125a45ae0fd7 (diff) | |
Add ogg support
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/audio.py | 84 | ||||
| -rw-r--r-- | lib/mat.py | 19 |
2 files changed, 98 insertions, 5 deletions
diff --git a/lib/audio.py b/lib/audio.py index 55562cc..0c8859d 100644 --- a/lib/audio.py +++ b/lib/audio.py | |||
| @@ -2,10 +2,15 @@ | |||
| 2 | Care about audio fileformat | 2 | Care about audio fileformat |
| 3 | ''' | 3 | ''' |
| 4 | from mutagen.flac import FLAC | 4 | from mutagen.flac import FLAC |
| 5 | from mutagen.apev2 import APEv2 | ||
| 6 | from mutagen.oggvorbis import OggVorbis | ||
| 7 | |||
| 8 | |||
| 5 | 9 | ||
| 6 | import parser | 10 | import parser |
| 7 | import shutil | 11 | import shutil |
| 8 | 12 | ||
| 13 | |||
| 9 | class MpegAudioStripper(parser.GenericParser): | 14 | class MpegAudioStripper(parser.GenericParser): |
| 10 | ''' | 15 | ''' |
| 11 | Represent mpeg audio file (mp3, ...) | 16 | Represent mpeg audio file (mp3, ...) |
| @@ -16,6 +21,80 @@ class MpegAudioStripper(parser.GenericParser): | |||
| 16 | else: | 21 | else: |
| 17 | return False | 22 | return False |
| 18 | 23 | ||
| 24 | |||
| 25 | class OggStripper(parser.GenericParser): | ||
| 26 | ''' | ||
| 27 | Represent an ogg vorbis file | ||
| 28 | ''' | ||
| 29 | def remove_all(self): | ||
| 30 | if self.backup is True: | ||
| 31 | shutil.copy2(self.filename, self.output) | ||
| 32 | self.filename = self.output | ||
| 33 | |||
| 34 | mfile = OggVorbis(self.filename) | ||
| 35 | mfile.delete() | ||
| 36 | mfile.save() | ||
| 37 | |||
| 38 | def is_clean(self): | ||
| 39 | ''' | ||
| 40 | Check if the "metadata" block is present in the file | ||
| 41 | ''' | ||
| 42 | mfile = OggVorbis(self.filename) | ||
| 43 | print mfile.tags | ||
| 44 | if mfile.tags == []: | ||
| 45 | return True | ||
| 46 | else: | ||
| 47 | return False | ||
| 48 | |||
| 49 | def get_meta(self): | ||
| 50 | ''' | ||
| 51 | Return the content of the metadata block if present | ||
| 52 | ''' | ||
| 53 | metadata = {} | ||
| 54 | mfile = OggVorbis(self.filename) | ||
| 55 | for key, value in mfile.tags: | ||
| 56 | metadata[key] = value | ||
| 57 | return metadata | ||
| 58 | |||
| 59 | class Apev2Stripper(parser.GenericParser): | ||
| 60 | ''' | ||
| 61 | Represent a Apev2 audio file | ||
| 62 | ''' | ||
| 63 | def remove_all(self): | ||
| 64 | ''' | ||
| 65 | Remove the "metadata" block from the file | ||
| 66 | ''' | ||
| 67 | if self.backup is True: | ||
| 68 | shutil.copy2(self.filename, self.output) | ||
| 69 | self.filename = self.output | ||
| 70 | |||
| 71 | mfile = APEv2(self.filename) | ||
| 72 | mfile.delete() | ||
| 73 | mfile.save() | ||
| 74 | |||
| 75 | def is_clean(self): | ||
| 76 | ''' | ||
| 77 | Check if the "metadata" block is present in the file | ||
| 78 | ''' | ||
| 79 | mfile = APEv2(self.filename) | ||
| 80 | if mfile.tags is None: | ||
| 81 | return True | ||
| 82 | else: | ||
| 83 | return False | ||
| 84 | |||
| 85 | def get_meta(self): | ||
| 86 | ''' | ||
| 87 | Return the content of the metadata block if present | ||
| 88 | ''' | ||
| 89 | metadata = {} | ||
| 90 | mfile = APEv2(self.filename) | ||
| 91 | if mfile.tags is None: | ||
| 92 | return metadata | ||
| 93 | for key, value in mfile.tags: | ||
| 94 | metadata[key] = value | ||
| 95 | return metadata | ||
| 96 | |||
| 97 | |||
| 19 | class FlacStripper(parser.GenericParser): | 98 | class FlacStripper(parser.GenericParser): |
| 20 | ''' | 99 | ''' |
| 21 | Represent a Flac audio file | 100 | Represent a Flac audio file |
| @@ -30,6 +109,7 @@ class FlacStripper(parser.GenericParser): | |||
| 30 | 109 | ||
| 31 | mfile = FLAC(self.filename) | 110 | mfile = FLAC(self.filename) |
| 32 | mfile.delete() | 111 | mfile.delete() |
| 112 | mfile.clear_pictures() | ||
| 33 | mfile.save() | 113 | mfile.save() |
| 34 | 114 | ||
| 35 | def is_clean(self): | 115 | def is_clean(self): |
| @@ -37,7 +117,7 @@ class FlacStripper(parser.GenericParser): | |||
| 37 | Check if the "metadata" block is present in the file | 117 | Check if the "metadata" block is present in the file |
| 38 | ''' | 118 | ''' |
| 39 | mfile = FLAC(self.filename) | 119 | mfile = FLAC(self.filename) |
| 40 | if mfile.tags is None: | 120 | if mfile.tags is None and mfile.pictures == []: |
| 41 | return True | 121 | return True |
| 42 | else: | 122 | else: |
| 43 | return False | 123 | return False |
| @@ -52,4 +132,6 @@ class FlacStripper(parser.GenericParser): | |||
| 52 | return metadata | 132 | return metadata |
| 53 | for key, value in mfile.tags: | 133 | for key, value in mfile.tags: |
| 54 | metadata[key] = value | 134 | metadata[key] = value |
| 135 | if mfile.pictures != []: | ||
| 136 | metadata['picture :'] = 'yes' | ||
| 55 | return metadata | 137 | return metadata |
| @@ -39,17 +39,29 @@ STRIPPERS = { | |||
| 39 | try: | 39 | try: |
| 40 | import mutagen | 40 | import mutagen |
| 41 | STRIPPERS['audio/x-flac'] = audio.FlacStripper | 41 | STRIPPERS['audio/x-flac'] = audio.FlacStripper |
| 42 | STRIPPERS['audio/x-ape'] = audio.Apev2Stripper | ||
| 43 | STRIPPERS['audio/x-wavpack'] = audio.Apev2Stripper | ||
| 44 | STRIPPERS['audio/vorbis'] = audio.OggStripper | ||
| 42 | except ImportError: | 45 | except ImportError: |
| 43 | print('unable to import python-mutagen : limited audio format support') | 46 | print('unable to import python-mutagen : limited audio format support') |
| 44 | 47 | ||
| 48 | |||
| 45 | def secure_remove(filename): | 49 | def secure_remove(filename): |
| 46 | ''' | 50 | ''' |
| 47 | securely remove the file | 51 | securely remove the file |
| 48 | ''' | 52 | ''' |
| 53 | removed = False | ||
| 49 | try: | 54 | try: |
| 50 | subprocess.call('shred --remove %s' % filename, shell=True) | 55 | subprocess.call('shred --remove %s' % filename, shell=True) |
| 56 | removed = True | ||
| 51 | except: | 57 | except: |
| 52 | logging.error('Unable to remove %s' % filename) | 58 | logging.error('Unable to securely remove %s' % filename) |
| 59 | |||
| 60 | if removed is False: | ||
| 61 | try: | ||
| 62 | os.remove(filename) | ||
| 63 | except: | ||
| 64 | logging.error('Unable to remove %s' % filename) | ||
| 53 | 65 | ||
| 54 | 66 | ||
| 55 | def is_secure(filename): | 67 | def is_secure(filename): |
| @@ -72,8 +84,6 @@ def create_class_file(name, backup, add2archive): | |||
| 72 | return | 84 | return |
| 73 | 85 | ||
| 74 | filename = '' | 86 | filename = '' |
| 75 | realname = name | ||
| 76 | |||
| 77 | try: | 87 | try: |
| 78 | filename = hachoir_core.cmd_line.unicodeFilename(name) | 88 | filename = hachoir_core.cmd_line.unicodeFilename(name) |
| 79 | except TypeError: # get rid of "decoding Unicode is not supported" | 89 | except TypeError: # get rid of "decoding Unicode is not supported" |
| @@ -85,6 +95,7 @@ def create_class_file(name, backup, add2archive): | |||
| 85 | return | 95 | return |
| 86 | 96 | ||
| 87 | mime = parser.mime_type | 97 | mime = parser.mime_type |
| 98 | print mime | ||
| 88 | 99 | ||
| 89 | if mime.startswith('application/vnd.oasis.opendocument'): | 100 | if mime.startswith('application/vnd.oasis.opendocument'): |
| 90 | mime = 'application/vnd.oasis.opendocument' # opendocument fileformat | 101 | mime = 'application/vnd.oasis.opendocument' # opendocument fileformat |
| @@ -92,7 +103,7 @@ def create_class_file(name, backup, add2archive): | |||
| 92 | try: | 103 | try: |
| 93 | stripper_class = STRIPPERS[mime] | 104 | stripper_class = STRIPPERS[mime] |
| 94 | except KeyError: | 105 | except KeyError: |
| 95 | logging.info('Don\'t have stripper for %s\' format' % filename) | 106 | logging.info('Don\'t have stripper for %s\'s format' % name) |
| 96 | return | 107 | return |
| 97 | 108 | ||
| 98 | return stripper_class(filename, parser, mime, backup, add2archive) | 109 | return stripper_class(filename, parser, mime, backup, add2archive) |
