Package deefuzzer :: Package deefuzzer :: Package tools :: Module mp3
[hide private]
[frames] | no frames]

Source Code for Module deefuzzer.deefuzzer.tools.mp3

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright Guillaume Pellerin (2006-2009) 
  5   
  6  # <yomguy@parisson.com> 
  7   
  8  # This software is a computer program whose purpose is to stream audio 
  9  # and video data through icecast2 servers. 
 10   
 11  # This software is governed by the CeCILL  license under French law and 
 12  # abiding by the rules of distribution of free software.  You can  use, 
 13  # modify and/ or redistribute the software under the terms of the CeCILL 
 14  # license as circulated by CEA, CNRS and INRIA at the following URL 
 15  # "http://www.cecill.info". 
 16   
 17  # As a counterpart to the access to the source code and  rights to copy, 
 18  # modify and redistribute granted by the license, users are provided only 
 19  # with a limited warranty  and the software's author,  the holder of the 
 20  # economic rights,  and the successive licensors  have only  limited 
 21  # liability. 
 22   
 23  # In this respect, the user's attention is drawn to the risks associated 
 24  # with loading,  using,  modifying and/or developing or reproducing the 
 25  # software by the user in light of its specific status of free software, 
 26  # that may mean  that it is complicated to manipulate,  and  that  also 
 27  # therefore means  that it is reserved for developers  and  experienced 
 28  # professionals having in-depth computer knowledge. Users are therefore 
 29  # encouraged to load and test the software's suitability as regards their 
 30  # requirements in conditions enabling the security of their systems and/or 
 31  # data to be ensured and,  more generally, to use and operate it in the 
 32  # same conditions as regards security. 
 33   
 34  # The fact that you are presently reading this means that you have had 
 35  # knowledge of the CeCILL license and that you accept its terms. 
 36   
 37  # Author: Guillaume Pellerin <yomguy@parisson.com> 
 38   
 39  import os 
 40  import string 
 41  import datetime 
 42  from mutagen.easyid3 import EasyID3 
 43  from mutagen.mp3 import MP3, MPEGInfo 
 44  from mutagen import id3 
 45  from tools import * 
 46   
 47  EasyID3.valid_keys["comment"]="COMM::'XXX'" 
 48  EasyID3.valid_keys["copyright"]="TCOP::'XXX'" 
 49   
50 -class Mp3:
51 """A MP3 file object""" 52
53 - def __init__(self, media):
54 self.media = media 55 self.item_id = '' 56 self.source = self.media 57 self.options = {} 58 self.bitrate_default = '192' 59 self.cache_dir = os.sep + 'tmp' 60 self.keys2id3 = {'title': 'TIT2', 61 'artist': 'TPE1', 62 'album': 'TALB', 63 'date': 'TDRC', 64 'comment': 'COMM', 65 'genre': 'TCON', 66 'copyright': 'TCOP', 67 } 68 self.mp3 = MP3(self.media, ID3=EasyID3) 69 self.info = self.mp3.info 70 self.bitrate = int(str(self.info.bitrate)[:-3]) 71 self.length = datetime.timedelta(0,self.info.length) 72 try: 73 self.metadata = self.get_file_metadata() 74 except: 75 self.metadata = {'title': '', 76 'artist': '', 77 'album': '', 78 'date': '', 79 'comment': '', 80 'genre': '', 81 'copyright': '', 82 } 83 84 self.description = self.get_description() 85 self.mime_type = self.get_mime_type() 86 self.media_info = get_file_info(self.media) 87 self.file_name = self.media_info[0] 88 self.file_title = self.media_info[1] 89 self.file_ext = self.media_info[2] 90 self.extension = self.get_file_extension() 91 self.size = os.path.getsize(media)
92 #self.args = self.get_args() 93
94 - def get_format(self):
95 return 'MP3'
96
97 - def get_file_extension(self):
98 return 'mp3'
99
100 - def get_mime_type(self):
101 return 'audio/mpeg'
102
103 - def get_description(self):
104 return "MPEG audio Layer III"
105
106 - def get_file_metadata(self):
107 metadata = {} 108 for key in self.keys2id3.keys(): 109 try: 110 metadata[key] = self.mp3[key][0] 111 except: 112 metadata[key] = '' 113 return metadata
114
115 - def write_tags(self):
116 """Write all ID3v2.4 tags by mapping dub2id3_dict dictionnary with the 117 respect of mutagen classes and methods""" 118 119 self.mp3.add_tags() 120 self.mp3.tags['TIT2'] = id3.TIT2(encoding=2, text=u'text') 121 self.mp3.save() 122 123 #media_id3 = id3.ID3(self.media) 124 #for tag in self.metadata.keys(): 125 #if tag in self.dub2id3_dict.keys(): 126 #frame_text = self.dub2id3_dict[tag] 127 #value = self.metadata[tag] 128 #frame = mutagen.id3.Frames[frame_text](3,value) 129 #try: 130 #media_id3.add(frame) 131 #except: 132 #raise IOError('ExporterError: cannot tag "'+tag+'"') 133 134 #try: 135 #media_id3.save() 136 #except: 137 #raise IOError('ExporterError: cannot write tags') 138 139 media = id3.ID3(self.media) 140 media.add(id3.TIT2(encoding=3, text=self.metadata['title'].decode('utf8'))) 141 media.add(id3.TP1(encoding=3, text=self.metadata['artist'].decode('utf8'))) 142 media.add(id3.TAL(encoding=3, text=self.metadata['album'].decode('utf8'))) 143 media.add(id3.TDRC(encoding=3, text=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) 144 media.add(id3.TCO(encoding=3, text=self.metadata['genre'].decode('utf8'))) 145 try: 146 media.save() 147 except: 148 raise IOError('ExporterError: cannot write tags')
149