summaryrefslogtreecommitdiff
path: root/lib/audio.py
blob: 613a0cb9a474de73df37c8a67fd119c555880518 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'''
    Care about audio fileformat
'''
try:
    from mutagen.flac import FLAC
    from mutagen.apev2 import APEv2File
    from mutagen.oggvorbis import OggVorbis
except ImportError:
    pass


import parser
import shutil


class MpegAudioStripper(parser.GenericParser):
    '''
        Represent mpeg audio file (mp3, ...)
    '''
    def _should_remove(self, field):
        if field.name in ("id3v1", "id3v2"):
            return True
        else:
            return False


class OggStripper(parser.GenericParser):
    '''
        Represent an ogg vorbis file
    '''
    def remove_all(self):
        if self.backup is True:
            shutil.copy2(self.filename, self.output)
            self.filename = self.output

        mfile = OggVorbis(self.filename)
        mfile.delete()
        mfile.save()

    def is_clean(self):
        '''
            Check if the "metadata" block is present in the file
        '''
        mfile = OggVorbis(self.filename)
        if mfile.tags == []:
            return True
        else:
            return False

    def get_meta(self):
        '''
            Return the content of the metadata block if present
        '''
        metadata = {}
        mfile = OggVorbis(self.filename)
        [metadata[key] = value for key, value in mfile.tags]
        return metadata

class Apev2Stripper(parser.GenericParser):
    '''
        Represent a Apev2 audio file
    '''
    def remove_all(self):
        '''
            Remove the "metadata" block from the file
        '''
        if self.backup is True:
            shutil.copy2(self.filename, self.output)
            self.filename = self.output

        mfile = APEv2File(self.filename)
        mfile.delete()
        mfile.save()

    def is_clean(self):
        '''
            Check if the "metadata" block is present in the file
        '''
        mfile = APEv2File(self.filename)
        if mfile.tags is None:
            return True
        else:
            return False

    def get_meta(self):
        '''
            Return the content of the metadata block if present
        '''
        metadata = {}
        mfile = APEv2File(self.filename)
        if mfile.tags is not None:
            [metadata[key] = value for key, value in mfile.tags]
        return metadata


class FlacStripper(parser.GenericParser):
    '''
        Represent a Flac audio file
    '''
    def remove_all(self):
        '''
            Remove the "metadata" block from the file
        '''
        if self.backup is True:
            shutil.copy2(self.filename, self.output)
            self.filename = self.output

        mfile = FLAC(self.filename)
        mfile.delete()
        mfile.clear_pictures()
        mfile.save()

    def is_clean(self):
        '''
            Check if the "metadata" block is present in the file
        '''
        mfile = FLAC(self.filename)
        if mfile.tags is None and mfile.pictures == []:
            return True
        else:
            return False

    def get_meta(self):
        '''
            Return the content of the metadata block if present
        '''
        metadata = {}
        mfile = FLAC(self.filename)
        if mfile.tags is not None:
            [metadata[key] = value for key, value in mfile.tags]
            if mfile.pictures != []:
                metadata['picture :'] = 'yes'
        return metadata