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

Source Code for Module deefuzzer.deefuzzer.tools.ogg

  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.oggvorbis import OggVorbis 
 43  from tools import * 
 44   
 45   
46 -class Ogg:
47 """An OGG file object""" 48
49 - def __init__(self, media):
50 self.media = media 51 self.ogg = OggVorbis(self.media) 52 self.item_id = '' 53 self.source = self.media 54 self.options = {} 55 self.bitrate_default = '192' 56 self.cache_dir = os.sep + 'tmp' 57 self.keys2ogg = {'title': 'title', 58 'artist': 'artist', 59 'album': 'album', 60 'date': 'date', 61 'comment': 'comment', 62 'genre': 'genre', 63 'copyright': 'copyright', 64 } 65 self.info = self.ogg.info 66 self.bitrate = int(str(self.info.bitrate)[:-3]) 67 self.length = datetime.timedelta(0,self.info.length) 68 self.metadata = self.get_file_metadata() 69 self.description = self.get_description() 70 self.mime_type = self.get_mime_type() 71 self.media_info = get_file_info(self.media) 72 self.file_name = self.media_info[0] 73 self.file_title = self.media_info[1] 74 self.file_ext = self.media_info[2] 75 self.extension = self.get_file_extension() 76 self.size = os.path.getsize(media)
77 #self.args = self.get_args() 78
79 - def get_format(self):
80 return 'OGG'
81
82 - def get_file_extension(self):
83 return 'ogg'
84
85 - def get_mime_type(self):
86 return 'application/ogg'
87
88 - def get_description(self):
89 return 'FIXME'
90
91 - def get_file_info(self):
92 try: 93 file_out1, file_out2 = os.popen4('ogginfo "'+self.dest+'"') 94 info = [] 95 for line in file_out2.readlines(): 96 info.append(clean_word(line[:-1])) 97 self.info = info 98 return self.info 99 except: 100 raise IOError('ExporterError: file does not exist.')
101
102 - def set_cache_dir(self,path):
103 self.cache_dir = path
104
105 - def get_file_metadata(self):
106 metadata = {} 107 for key in self.keys2ogg.keys(): 108 try: 109 metadata[key] = self.ogg[key][0] 110 except: 111 metadata[key] = '' 112 return metadata
113
114 - def decode(self):
115 try: 116 os.system('oggdec -o "'+self.cache_dir+os.sep+self.item_id+ 117 '.wav" "'+self.source+'"') 118 return self.cache_dir+os.sep+self.item_id+'.wav' 119 except: 120 raise IOError('ExporterError: decoder is not compatible.')
121
122 - def write_tags(self):
123 #self.ogg.add_tags() 124 for tag in self.metadata.keys(): 125 self.ogg[tag] = str(self.metadata[tag]) 126 self.ogg.save()
127
128 - def get_args(self,options=None):
129 """Get process options and return arguments for the encoder""" 130 args = [] 131 if not options is None: 132 self.options = options 133 if not ('verbose' in self.options and self.options['verbose'] != '0'): 134 args.append('-Q ') 135 if 'ogg_bitrate' in self.options: 136 args.append('-b '+self.options['ogg_bitrate']) 137 elif 'ogg_quality' in self.options: 138 args.append('-q '+self.options['ogg_quality']) 139 else: 140 args.append('-b '+self.bitrate_default) 141 else: 142 args.append('-Q -b '+self.bitrate_default) 143 144 for tag in self.metadata.keys(): 145 value = clean_word(self.metadata[tag]) 146 args.append('-c %s="%s"' % (tag, value)) 147 if tag in self.dub2args_dict.keys(): 148 arg = self.dub2args_dict[tag] 149 args.append('-c %s="%s"' % (arg, value)) 150 151 return args
152