Package timeside :: Package encoder :: Module mp3
[hide private]
[frames] | no frames]

Source Code for Module timeside.encoder.mp3

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (C) 2007-2012 Parisson SARL 
  4  # Copyright (c) 2006-2012 Guillaume Pellerin <pellerin@parisson.com> 
  5  # Copyright (c) 2010-2012 Paul Brossier <piem@piem.org> 
  6  # Copyright (c) 2009-2010 Olivier Guilyardi <olivier@samalyse.com> 
  7   
  8   
  9  # This file is part of TimeSide. 
 10   
 11  # TimeSide is free software: you can redistribute it and/or modify 
 12  # it under the terms of the GNU General Public License as published by 
 13  # the Free Software Foundation, either version 2 of the License, or 
 14  # (at your option) any later version. 
 15   
 16  # TimeSide is distributed in the hope that it will be useful, 
 17  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 18  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 19  # GNU General Public License for more details. 
 20   
 21  # You should have received a copy of the GNU General Public License 
 22  # along with TimeSide.  If not, see <http://www.gnu.org/licenses/>. 
 23   
 24  # Authors: Guillaume Pellerin <yomguy@parisson.com> 
 25  #          Paul Brossier <piem@piem.org> 
 26   
 27  from timeside.core import implements, interfacedoc 
 28  from timeside.encoder.core import GstEncoder 
 29  from timeside.api import IEncoder 
 30  from timeside.tools import * 
 31   
 32  import mutagen 
33 34 -class Mp3Encoder(GstEncoder):
35 """ gstreamer-based mp3 encoder """ 36 implements(IEncoder) 37 38 @interfacedoc
39 - def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None):
40 super(Mp3Encoder, self).setup(channels, samplerate, blocksize, totalframes) 41 42 self.pipe = '''appsrc name=src 43 ! audioconvert 44 ! lamemp3enc target=quality quality=2 encoding-engine-quality=standard 45 ! xingmux 46 ! id3v2mux 47 ''' 48 49 if self.filename and self.streaming: 50 self.pipe += ''' ! tee name=t 51 ! queue ! filesink location=%s 52 t. ! queue ! appsink name=app sync=False 53 ''' % self.filename 54 55 elif self.filename : 56 self.pipe += '! filesink location=%s async=False sync=False ' % self.filename 57 else: 58 self.pipe += '! queue ! appsink name=app sync=False ' 59 60 self.start_pipeline(channels, samplerate)
61 62 @staticmethod 63 @interfacedoc
64 - def id():
65 return "gst_mp3_enc"
66 67 @staticmethod 68 @interfacedoc
69 - def description():
70 return "MP3 GStreamer based encoder"
71 72 @staticmethod 73 @interfacedoc
74 - def format():
75 return "MP3"
76 77 @staticmethod 78 @interfacedoc
79 - def file_extension():
80 return "mp3"
81 82 @staticmethod 83 @interfacedoc
84 - def mime_type():
85 return "audio/mpeg"
86 87 @interfacedoc
88 - def set_metadata(self, metadata):
89 self.metadata = metadata
90
91 - def write_metadata(self):
92 """Write all ID3v2.4 tags to file from self.metadata""" 93 from mutagen import id3 94 id3 = id3.ID3(self.filename) 95 for tag in self.metadata.keys(): 96 value = self.metadata[tag] 97 frame = mutagen.id3.Frames[tag](3,value) 98 try: 99 id3.add(frame) 100 except: 101 raise IOError('EncoderError: cannot tag "'+tag+'"') 102 try: 103 id3.save() 104 except: 105 raise IOError('EncoderError: cannot write tags')
106