def initMPEG(self, filename, width, height, codec): """ initialize MPEG capture bool <- initMPEG(filename, width, height): filename is hte name of the MPEG file width is the width of a frame in pixel height is the height of a frame in pixel """ self.videoOutputFile = open(filename, 'wb') assert self.videoOutputFile, 'ERROR, failed to open file: ' + filename if width is None: width = self.width w = self.videoFrameWidth = width - width % 2 if height is None: height = self.height h = self.videoFrameHeight = height - height % 2 if codec == 'mpeg2': params = { 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'width': w, 'height': h, 'frame_rate': 2997, 'deinterlace': 0, 'bitrate': 9800000, 'id': vcodec.getCodecID('mpeg2video') } else: # default is mpeg1 params = { 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'width': w, 'height': h, 'frame_rate': 2997, 'deinterlace': 0, 'bitrate': 2700000, 'id': vcodec.getCodecID('mpeg1video') } self.encoder = vcodec.Encoder(params) self.nbFrames = 0
def open (self): MovieOutputStream.open(self) print >>stderr, 'Creating MPEG: %r: codec=%s, size=%dx%d, framerate=%s' % \ (self.info.filename, self.mpeg_codec, self.info.width, self.info.height, self.info.framerate) (x,y,w,h) = self.info.clipping # Crop to match codec constraints w = w - w % 2 # width constraint : multiple of 2 h = h - h % 2 # height constraint : multiple of 2 self.screen = SWFScreen(x, y, w, h, scaling=self.info.scaling) params = { 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'height': self.screen.out_height, 'width': self.screen.out_width, 'frame_rate': 2997, 'deinterlace': 0, 'id': vcodec.getCodecID(self.mpeg_codec), 'format': vcodec.formats.PIX_FMT_YUV420P } params['frame_rate'] = int(params['frame_rate_base'] * self.info.framerate) if self.mpeg_codec == 'mpeg1video': params['bitrate'] = 2700000 else: params['bitrate'] = 9800000 if self.debug: print >>stderr, 'Setting codec to ', params self.encoder = vcodec.Encoder(params) self.out_file = open(self.info.filename, 'wb') return
def open(self): MovieOutputStream.open(self) print >>stderr, 'Creating MPEG: %r: codec=%s, size=%dx%d, framerate=%s' % \ (self.info.filename, self.mpeg_codec, self.info.width, self.info.height, self.info.framerate) (x, y, w, h) = self.info.clipping # Crop to match codec constraints w = w - w % 2 # width constraint : multiple of 2 h = h - h % 2 # height constraint : multiple of 2 self.screen = SWFScreen(x, y, w, h, scaling=self.info.scaling) params = { 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'height': self.screen.out_height, 'width': self.screen.out_width, 'frame_rate': 2997, 'deinterlace': 0, 'id': vcodec.getCodecID(self.mpeg_codec), 'format': vcodec.formats.PIX_FMT_YUV420P } params['frame_rate'] = int(params['frame_rate_base'] * self.info.framerate) if self.mpeg_codec == 'mpeg1video': params['bitrate'] = 2700000 else: params['bitrate'] = 9800000 if self.debug: print >> stderr, 'Setting codec to ', params self.encoder = vcodec.Encoder(params) self.out_file = open(self.info.filename, 'wb') return
def initVideo(self): dm = muxer.Demuxer(self.format) #try to get multiple streams of a multiplexed file junk = dm.parse(self.rawData[30000]) if len(dm.streams) == 0: #set bitrate if self.codec == 'mpeg1video': bitrate = 2700000 else: bitrate = 9800000 self.params= { \ 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'width': 800, 'height': 600, 'frame_rate': 3125, 'deinterlace': 0, 'bitrate': bitrate, 'id': vcodec.getCodecID( self.codec ) } elif len(dm.streams) == 1: self.params = dm.streams[0] else: #loop through to find the first video stream in the file for vindex in xrange(len(dm.streams)): if dm.streams[vindex]['type'] == muxer.CODEC_TYPE_VIDEO: self.params = dm.streams[vindex] break print self.params try: # Set the initial sound delay to 0 for now # It defines initial offset from video in the beginning of the stream self.initADelta = -1 self.seekADelta = 0 # Setting up the HW video codec self.vc = ext_codecs.Decoder(self.params) except: self.vc = vcodec.Decoder(self.params) try: # Fall back to SW video codec self.vc = vcodec.Decoder(self.params) except: traceback.print_exc() self.err.append(sys.exc_info()[1])
def initVideo(self): dm= muxer.Demuxer( self.format ) #try to get multiple streams of a multiplexed file junk = dm.parse( self.rawData[30000] ) if len(dm.streams)==0: #set bitrate if self.codec== 'mpeg1video': bitrate= 2700000 else: bitrate= 9800000 self.params= { \ 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'width': 800, 'height': 600, 'frame_rate': 3125, 'deinterlace': 0, 'bitrate': bitrate, 'id': vcodec.getCodecID( self.codec ) } elif len(dm.streams)==1: self.params = dm.streams[0] else: #loop through to find the first video stream in the file for vindex in xrange( len( dm.streams )): if dm.streams[ vindex ][ 'type' ]== muxer.CODEC_TYPE_VIDEO: self.params= dm.streams[ vindex ] break print self.params try: # Set the initial sound delay to 0 for now # It defines initial offset from video in the beginning of the stream self.initADelta= -1 self.seekADelta= 0 # Setting up the HW video codec self.vc= ext_codecs.Decoder( self.params ) except: self.vc= vcodec.Decoder( self.params ) try: # Fall back to SW video codec self.vc= vcodec.Decoder( self.params ) except: traceback.print_exc() self.err.append( sys.exc_info()[1] )
def makeVideo( inPattern, outFile, outCodec ): # Get video stream dimensions from the image size pygame.init() i= 1 # Opening mpeg file for output e= None i= 1 fw= open( outFile, 'wb' ) while i: print "Adding Frame %i (%s)" % (i, inPattern % i ) if os.path.isfile( inPattern % i ): s= pygame.image.load( inPattern % i ) if not e: if outCodec== 'mpeg1video': bitrate= 2700000 else: bitrate= 9800000 params= { \ 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'height': s.get_height(), 'width': s.get_width(), 'frame_rate': 2997, 'deinterlace': 0, 'bitrate': bitrate, 'id': vcodec.getCodecID( outCodec ) } print 'Setting codec to ', params e= vcodec.Encoder( params ) t= time.time() # Create VFrame ss= pygame.image.tostring(s, "RGB") bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None)) yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P ) d= e.encode( yuvFrame ) #print "\n\n", type(e), type(d), "\n\n" fw.write( d ) i+= 1 else: print '%d frames written in %.2f secs( %.2f fps )' % ( i, time.time()- t, float( i )/ ( time.time()- t ) ) i= 0 fw.close() pygame.quit()
def makeMPEG(filename, images, codec='mpeg1video', codecParams=None, verbose=False): if not havePyMedia: logging.error('pymedia (www.pymedia.org) needed to make mpeg movies') return 0 fw = open(filename, 'wb') t = time.time() # set bitrate if codec == 'mpeg1video': bitrate = 2700000 else: bitrate = 9800000 # set other params (or receive params dictionary) if codecParams is None: codecParams = { 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'width': images[0].size[0], 'height': images[0].size[1], 'frame_rate': 3125, 'deinterlace': 0, 'bitrate': bitrate, 'id': vcodec.getCodecID(codec) } logging.info('Setting codec to ' + str(codecParams)) encoder = vcodec.Encoder(codecParams) for im in images: # Create VFrame imStr = im.tostring() bmpFrame = vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, im.size, (imStr, None, None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P, im.size) d = encoder.encode(yuvFrame) try: fw.write(d.data) # this is what works! except Exception: fw.write(d) # this is what pymedia demo recommends else: logging.info('%d frames written in %.2f secs' % (len(images), time.time() - t)) i = 0 fw.close()
def makeVideo( inPattern, outFile, outCodec ): # Get video stream dimensions from the image size pygame.init() i= 1 # Opening mpeg file for output e= None i= 1 fw= open( outFile, 'wb' ) while i: if os.path.isfile( inPattern % i ): s= pygame.image.load( inPattern % i ) if not e: if outCodec== 'mpeg1video': bitrate= 2700000 else: bitrate= 9800000 params= { \ 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'height': s.get_height(), 'width': s.get_width(), 'frame_rate': 2997, 'deinterlace': 0, 'bitrate': bitrate, 'id': vcodec.getCodecID( outCodec ) } print 'Setting codec to ', params e= vcodec.Encoder( params ) t= time.time() # Create VFrame if inPattern.index( '.bmp' )> 0: ss= pygame.image.tostring(s, "RGB") bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None)) yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P ) d= e.encode( yuvFrame ) fw.write( d.data ) i+= 1 else: print '%d frames written in %.2f secs( %.2f fps )' % ( i, time.time()- t, float( i )/ ( time.time()- t ) ) i= 0 fw.close() pygame.quit()
def makeMPEG(filename, images, codec='mpeg1video', codecParams=None, verbose=False): if not havePyMedia: logging.error('pymedia (www.pymedia.org) needed to make mpeg movies') return 0 fw = open(filename, 'wb') t = time.time() # set bitrate if codec == 'mpeg1video': bitrate = 2700000 else: bitrate = 9800000 # set other params (or receive params dictionary) if codecParams is None: codecParams = { 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'width': images[0].size[0], 'height': images[0].size[1], 'frame_rate': 3125, 'deinterlace': 0, 'bitrate': bitrate, 'id': vcodec.getCodecID(codec) } logging.info('Setting codec to ' + str(codecParams)) encoder = vcodec.Encoder(codecParams) for im in images: # Create VFrame imStr = im.tostring() bmpFrame = vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, im.size, (imStr, None, None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P, im.size) d = encoder.encode(yuvFrame) try: fw.write(d.data) # this is what works! except Exception: fw.write(d) # this is what pymedia demo recommends else: logging.info('%d frames written in %.2f secs' % (len(images), time.time() - t)) i = 0 fw.close()
def createQuicklookVideo(dataset, filename, images_per_sec=2): """ Creates an mpeg2 video clip of quicklook images of images in the dataset. The dataset argument should be an allskyData.dataset object, the filename is the output filename. """ #ensure correct file extension if not filename.endswith((".mpg",".mpeg")): filename = filename + ".mpeg" #work out size that a quicklook will be first_im = dataset.getImage(dataset.getTimes()[0]) size = first_im.createQuicklook().size #create video encoder params= { \ 'type': 0, 'gop_size': 12, 'frame_rate_base': 1, 'max_b_frames': 0, 'height': size[1], 'width': size[0], 'frame_rate': images_per_sec, 'deinterlace': 2000, 'bitrate': 9800000, 'id': vcodec.getCodecID('mpeg2video') } encoder = vcodec.Encoder( params ) with open(filename, 'wb') as ofp: for im in dataset: ql = im.createQuicklook() if ql.size != size: ql = ql.resize(size) bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, size, (ql.convert('RGB').tostring(),None,None)) yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P ) d = encoder.encode( yuvFrame ) ofp.write(d.data) #add the last image to the stream again - otherwise you won't see it! bmpFrame = vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, size, (ql.convert('RGB').tostring(),None,None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P) d = encoder.encode(yuvFrame) ofp.write(d.data)
def makeMPEG(filename, images, codec="mpeg1video", codecParams=None, verbose=False): if not havePyMedia: logging.error("pymedia (www.pymedia.org) needed to make mpeg movies") return 0 fw = open(filename, "wb") t = time.time() # set bitrate if codec == "mpeg1video": bitrate = 2700000 else: bitrate = 9800000 # set other params (or receive params dictionary) if codecParams == None: codecParams = { "type": 0, "gop_size": 12, "frame_rate_base": 125, "max_b_frames": 0, "width": images[0].size[0], "height": images[0].size[1], "frame_rate": 3125, "deinterlace": 0, "bitrate": bitrate, "id": vcodec.getCodecID(codec), } logging.info("Setting codec to " + str(codecParams)) encoder = vcodec.Encoder(codecParams) for im in images: # Create VFrame imStr = im.tostring() bmpFrame = vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, im.size, (imStr, None, None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P, im.size) d = encoder.encode(yuvFrame) try: fw.write(d.data) # this is what works! except: fw.write(d) # this is what pymedia demo recommends else: logging.info("%d frames written in %.2f secs" % (len(images), time.time() - t)) i = 0 fw.close()
def recodeVideo( inFile, outFile, outCodec ): dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] ) f= open( inFile, 'rb' ) fw= open( outFile, 'wb' ) s= f.read( 400000 ) r= dm.parse( s ) v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams ) if len( v )== 0: raise 'There is no video stream in a file %s' % inFile v_id= v[ 0 ][ 'index' ] print 'Assume video stream at %d index: ' % v_id c= vcodec.Decoder( dm.streams[ v_id ] ) e= None while len( s )> 0: for fr in r: if fr[ 0 ]== v_id: d= c.decode( fr[ 1 ] ) if d and d.data: if e== None: params= c.getParams() 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 codec to ', params e= vcodec.Encoder( params ) dw= e.encode( d ) print 'Frame size ', len( dw ) fw.write( dw ) s= f.read( 400000 ) r= dm.parse( s )
def recodeVideo( inFile, outFile, outCodec ): dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] ) f= open( inFile, 'rb' ) fw= open( outFile, 'wb' ) s= f.read( 400000 ) r= dm.parse( s ) v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams ) if len( v )== 0: raise 'There is no video stream in a file %s' % inFile v_id= v[ 0 ][ 'index' ] print 'Assume video stream at %d index: ' % v_id c= vcodec.Decoder( dm.streams[ v_id ] ) e= None while len( s )> 0: for fr in r: if fr[ 0 ]== v_id: d= c.decode( fr[ 1 ] ) if e== None and d: params= c.getParams() 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 codec to ', params e= vcodec.Encoder( params ) if e and d: dw= e.encode( d ) #print 'Frame size ', len( dw ) fw.write( dw ) s= f.read( 400000 ) r= dm.parse( s )
blankframe = np.zeros((height, width), dtype=np.int8) + 127 blankyuvFrame = prepframe(blankframe, enlargex=enlargex) f = file(out, 'wb') params = { 'type': 0, 'gop_size': 12, 'max_b_frames': 0, 'frame_rate_base': 1, 'frame_rate': 50, 'width': width, 'height': height, 'deinterlace': 0, 'bitrate': 3000000, # normally 2700000 for a framerate of 2997/125 Hz 'id': vcodec.getCodecID('mpeg1video') } e = vcodec.Encoder(params) for runi in range(nruns): for frame in m.data: yuvFrame = prepframe(frame, enlargex=enlargex) d = e.encode(yuvFrame) f.write(d.data) sys.stdout.write('.') if iri: for framei in range(iri): d = e.encode(blankyuvFrame) f.write(d.data) sys.stdout.write('.')
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()
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()
def setupDump(self): # Capture the output as a sequence of numbered frame images. self.PlayTime = 0 self.PlayStartTime = 0 self.PlayFrame = 0 self.State = STATE_CAPTURING self.dumpFrameRate = manager.options.dump_fps assert self.dumpFrameRate filename = manager.options.dump base, ext = os.path.splitext(filename) ext_lower = ext.lower() self.dumpEncoder = None if ext_lower == '.mpg': # Use pymedia to convert frames to an mpeg2 stream # on-the-fly. import pymedia import pymedia.video.vcodec as vcodec self.dumpFile = open(filename, 'wb') frameRate = int(self.dumpFrameRate * 100 + 0.5) self.dumpFrameRate = float(frameRate) / 100.0 params= { \ 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'height': manager.options.size_y, 'width': manager.options.size_x, 'frame_rate': frameRate, 'deinterlace': 0, 'bitrate': 9800000, 'id': vcodec.getCodecID('mpeg2video') } self.dumpEncoder = vcodec.Encoder( params ) return # Don't dump a video file; dump a sequence of frames instead. self.dumpPPM = (ext_lower == '.ppm' or ext_lower == '.pnm') self.dumpAppend = False # Convert the filename to a pattern. if '#' in filename: hash = filename.index('#') end = hash while end < len(filename) and filename[end] == '#': end += 1 count = end - hash filename = filename[:hash] + '%0' + str(count) + 'd' + filename[end:] else: # There's no hash in the filename. if self.dumpPPM: # We can dump a series of frames all to the same file, # if we're dumping ppm frames. Mjpegtools likes this. self.dumpAppend = True try: os.remove(filename) except OSError: pass else: # Implicitly append a frame number. filename = base + '%04d' + ext self.dumpFilename = filename
def setupDump(self): # Capture the output as a sequence of numbered frame images. self.PlayTime = 0 self.PlayStartTime = 0 self.PlayFrame = 0 self.State = STATE_CAPTURING self.dumpFrameRate = manager.options.dump_fps assert self.dumpFrameRate filename = manager.options.dump base, ext = os.path.splitext(filename) ext_lower = ext.lower() self.dumpEncoder = None if ext_lower == '.mpg': # Use pymedia to convert frames to an mpeg2 stream # on-the-fly. import pymedia import pymedia.video.vcodec as vcodec self.dumpFile = open(filename, 'wb') frameRate = int(self.dumpFrameRate * 100 + 0.5) self.dumpFrameRate = float(frameRate) / 100.0 params= { \ 'type': 0, 'gop_size': 12, 'frame_rate_base': 125, 'max_b_frames': 0, 'height': manager.options.size_y, 'width': manager.options.size_x, 'frame_rate': frameRate, 'deinterlace': 0, 'bitrate': 9800000, 'id': vcodec.getCodecID('mpeg2video') } self.dumpEncoder = vcodec.Encoder(params) return # Don't dump a video file; dump a sequence of frames instead. self.dumpPPM = (ext_lower == '.ppm' or ext_lower == '.pnm') self.dumpAppend = False # Convert the filename to a pattern. if '#' in filename: hash = filename.index('#') end = hash while end < len(filename) and filename[end] == '#': end += 1 count = end - hash filename = filename[:hash] + '%0' + str( count) + 'd' + filename[end:] else: # There's no hash in the filename. if self.dumpPPM: # We can dump a series of frames all to the same file, # if we're dumping ppm frames. Mjpegtools likes this. self.dumpAppend = True try: os.remove(filename) except OSError: pass else: # Implicitly append a frame number. filename = base + '%04d' + ext self.dumpFilename = filename
width = m.ncellswide * enlargex height = m.ncellshigh * enlargex vparams = {'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