def recodeAudio(fName, fOutput, type, bitrate=None): # ------------------------------------ import pymedia.audio.acodec as acodec import pymedia.muxer as muxer # Open demuxer dm = muxer.Demuxer(fName.split('.')[-1].lower()) f = open(fName, 'rb') s = f.read(90000) dec = enc = mx = None print 'Recoding %s into %s' % (fName, fOutput) while len(s): frames = dm.parse(s) if frames: for fr in frames: # Assume for now only audio streams if dec == None: # Open decoder dec = acodec.Decoder(dm.streams[fr[0]]) print 'Decoder params:', dm.streams[fr[0]] # Decode audio frame r = dec.decode(fr[1]) if r: if bitrate == None: bitrate = r.bitrate # Open muxer and encoder if enc == None: params = { 'id': acodec.getCodecID(type), 'bitrate': bitrate, 'sample_rate': r.sample_rate, 'channels': r.channels } print 'Encoder params:', params mx = muxer.Muxer(type) stId = mx.addStream(muxer.CODEC_TYPE_AUDIO, params) enc = acodec.Encoder(params) fw = open(fOutput, 'wb') ss = mx.start() fw.write(ss) enc_frames = enc.encode(r.data) if enc_frames: for efr in enc_frames: ss = mx.write(stId, efr) if ss: fw.write(ss) s = f.read(100000) f.close() if fw: if mx: ss = mx.end() if ss: fw.write(ss) fw.close()
def run(self): print "recording to", self.name self.f = open(self.name, 'wb') # Minimum set of parameters we need to create Encoder cparams = { 'id': acodec.getCodecID('mp3'), 'bitrate': 128000, 'sample_rate': 44100, 'channels': 1 } self.ac = acodec.Encoder(cparams) self.snd = sound.Input(44100, 1, sound.AFMT_S16_LE) self.mux = muxer.Muxer("mp3") self.stream_index = self.mux.addStream(muxer.CODEC_TYPE_AUDIO, self.ac.getParams()) block = self.mux.start() if block: self.f.write(block) # Loop until Ctrl-C pressed or finished set from outside self.finished = False thread = threading.Thread(target=self.record) thread.start() try: while not self.finished: time.sleep(.003) except KeyboardInterrupt: self.finished = True print "finishing recording to", self.name # Stop listening the incoming sound from the microphone or line in thread.join() footer = self.mux.end() if footer is not None: self.f.write(footer) self.f.close() print "finished recording to", self.name print "snipping leading zeroes..." f = open(self.name, "rb") buffer = f.read() f.close() buffer = buffer.lstrip(chr(0)) f = open(self.name, "wb") f.write(buffer) f.close() print "snipped leading zeroes"
def main(self): mux = muxer.Muxer(self.codec) streamId = mux.addStream(muxer.CODEC_TYPE_AUDIO, self.params) enc = acodec.Encoder(self.params) data = mux.start() if data: self.send(data, "outbox") shutdown = False data = "" MINSIZE = 4096 while self.anyReady() or not shutdown: while self.dataReady("inbox"): newdata = self.recv("inbox") data = data + newdata['audio'] if len(data) >= MINSIZE: frames = enc.encode(data) for frame in frames: muxed = mux.write(streamId, frame) if muxed: self.send(muxed, "outbox") data = "" while self.dataReady("control"): msg = self.recv("control") if isinstance(msg, (producerFinished, shutdownMicroprocess)): shutdown = True self.send(msg, "signal") if not shutdown: self.pause() yield 1 data = mux.end() if data: self.send(data, "outbox")
def recodeVideo( inFile, outFile, outCodec ): dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] ) f= open( inFile, 'rb' ) open( outFile, 'wb' ).close() s= f.read( 600000 ) r= dm.parse( s ) v= [ x for x in dm.streams if x[ 'type' ]== muxer.CODEC_TYPE_VIDEO ] if len( v )== 0: raise 'There is no video stream in a file %s' % inFile v_id= v[ 0 ][ 'index' ] print 'Video stream at %d index: ' % v_id a_id= -1 a= [ x for x in dm.streams if x[ 'type' ]== muxer.CODEC_TYPE_AUDIO ] if len( a )!= 0: a_id= a[ 0 ][ 'index' ] print 'Assume audio stream at %d index: ' % a_id vc= vcodec.Decoder( v[ 0 ] ) if a_id!= -1: ac= acodec.Decoder( a[ 0 ] ) ea= ev= None bA= bV= 0 # Create muxer first mx= muxer.Muxer( outFile.split( '.' )[ -1 ] ) for fr in r: if fr[ 0 ]== a_id and not bA: d= ac.decode( fr[ 1 ] ) if d and d.data: # --------- Just a test ! params= ac.getParams() # --------- End of test #params= { 'id': acodec.getCodecID('ac3'), # 'bitrate': d.bitrate, # 'sample_rate': d.sample_rate, # 'channels': d.channels } # It should be some logic to work with frame rates and such. # I'm not aware of what it would be... print 'Setting audio codec to ', params #ea= acodec.Encoder( params ) aId= mx.addStream( muxer.CODEC_TYPE_AUDIO, params ) bA= 1 if fr[ 0 ]== v_id and not bV: d= vc.decode( fr[ 1 ] ) if d and d.data: # --------- Just a test ! params= vc.getParams() # --------- End of test #params[ 'id' ]= vcodec.getCodecID( outCodec ) # Just try to achive max quality( 2.7 MB/sec mpeg1 and 9.8 for mpeg2 ) #if outCodec== 'mpeg1video': # params[ 'bitrate' ]= 2700000 #else: # params[ 'bitrate' ]= 9800000 # It should be some logic to work with frame rates and such. # I'm not aware of what it would be... print 'Setting video codec to ', params #ev= vcodec.Encoder( params ) vId= mx.addStream( muxer.CODEC_TYPE_VIDEO, params ) bV= 1 # Reset decoders #if a_id!= -1: # ac= acodec.Decoder( a[ 0 ] ) #vc= vcodec.Decoder( v[ 0 ] ) # Start muxer fw= open( outFile, 'ab' ) fw.write( mx.start() ) fw.close() while len( s )> 0: for fr in r: ss= None if fr[ 3 ]> 0: #print 'PTS', fr[ 0 ], fr[ 3 ], fr[ 4 ] pass #if fr[ 3 ]> 3000000: # return if fr[ 0 ]== a_id: # --------- Just a test ! ss= mx.write( aId, fr[ 1 ], fr[ 3 ], fr[ 4 ] ) #print len(fr[ 1 ]), fr[ 3 ] # --------- End of test #d= ac.decode( fr[ 1 ] ) #if d and d.data: # enc_frames= ea.encode( d.data ) # if enc_frames: # for efr in enc_frames: # ss= mx.write( aId, efr ) if fr[ 0 ]== v_id: # --------- Just a test ! ss= mx.write( vId, fr[ 1 ], fr[ 3 ], fr[ 4 ] ) #if ss: fw.write(ss) #continue # --------- End of test #d= vc.decode( fr[ 1 ] ) #if d and d.data: # efr= ev.encode( d ) # if efr and efr.data: # ss= mx.write( vId, efr.data ) if ss: fw= open( outFile, 'ab' ) fw.write( ss ) fw.close() s= f.read( 4000 ) r= dm.parse( s ) if fw and mx: ss= mx.end() if ss: fw= open( outFile, 'ab' ) fw.write( ss ) fw.close()
def encodeVideo(fName, fOutFile, codecName): # ------------------------------------ import time, traceback import pymedia.muxer as muxer import pymedia.audio.acodec as acodec import pymedia.video.vcodec as vcodec format = fName.split('.')[-1].lower() dm = muxer.Demuxer(format) f = open(fName, 'rb') fw = open(fOutFile, 'wb') s = f.read(390000) r = dm.parse(s) print "Streams found:", dm.streams ss = filter(lambda x: x['type'] == muxer.CODEC_TYPE_AUDIO, dm.streams) if len(ss) == 0: raise 'No suitable audio streams in a file' a_id = ss[0]['index'] ac = acodec.Decoder(ss[0]) ss = filter(lambda x: x['type'] == muxer.CODEC_TYPE_VIDEO, dm.streams) if len(ss) == 0: raise 'No video streams in a file' v_id = ss[0]['index'] vc = vcodec.Decoder(ss[0]) #create muxer mux = muxer.Muxer('ts') header = a_id1 = v_id1 = None m_data = [] while len(s): for d in r: if d[0] == v_id: vfr = vc.decode(d[1]) if vfr: if v_id1 == None: params = vc.getParams() params['id'] = vcodec.getCodecID(codecName) if codecName == 'mpeg1video': params['bitrate'] = 2700000 elif codecName == 'mpeg2video': params['bitrate'] = 9800000 print 'Video: encoder params ', params vc1 = vcodec.Encoder(params) v_id1 = mux.addStream(muxer.CODEC_TYPE_VIDEO, vc1.getParams()) print "video stream id in muxer:", v_id1 s1 = vc1.encode(vfr) m_data.append((v_id1, s1)) elif d[0] == a_id: frame = ac.decode(d[1]) if a_id1 == None: params = { 'channels': frame.channels, 'sample_rate': frame.sample_rate, 'bitrate': frame.bitrate, 'id': acodec.getCodecID('mp3') } print 'Audio: encoder params ', params ac1 = acodec.Encoder( params) #or any other supported audio encoder a_id1 = mux.addStream(muxer.CODEC_TYPE_AUDIO, ac1.getParams()) print "audio stream id in muxer:", a_id1 encFrames = ac1.encode(frame.data) for fr in encFrames: m_data.append((a_id1, fr)) # We know that video and audio exists already if a_id1 != None and v_id1 != None: # Start header when all streams are captured if header == None: header = mux.start() fw.write(header) for st_id, ss in m_data: s1 = mux.write(st_id, ss) if s1: fw.write(s1) m_data = [] s = f.read(100000) r = dm.parse(s) fw.close()
'type': 0, 'gop_size': 12, 'max_b_frames': 0, 'frame_rate_base': 1, 'frame_rate': 50, 'width': width, 'height': height, 'deinterlace': 0, 'bitrate': 300000, # normally 2700000 for a framerate of 2997/125 Hz 'id': vcodec.getCodecID('mpeg1video') } ve = vcodec.Encoder(vparams) # encodes raw video data into the desired format mux = muxer.Muxer( 'avi' ) # create muxer, one of: ['avi', 'wmv', 'mov', 'mp4', 'wma', 'mp2', 'mp3', 'ac3', 'aac', 'flac', 'mpeg', 'mpg', 'mpeg', 'dat', 'vob', 'm1v', 'ogg'] vstreamid = mux.addStream(muxer.CODEC_TYPE_VIDEO, vparams) # add a video stream #astreamid = mux.addStream(muxer.CODEC_TYPE_AUDIO, aparams) # add an audio stream #import pdb; pdb.set_trace() # write header to disk header = mux.start() print 'hello' f.write(header) # write mp3 to disk #f.write(mp3data) s = mux.write(astreamid, mp3data) # returns a stream f.write(s) # the stream can then be written to disk