def prepframe(frame, enlargex=1): """Takes raw luminance data frame and converts it to RGB yuvFrame suitable for encoding with pymedia""" height, width = np.asarray(frame.shape) * enlargex frame = frame.T frame = enlarge(frame, enlargex) frame = np.asarray([frame, frame, frame]).T bmpFrame = vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, (width, height), (frame.tostring(), None, None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P) return yuvFrame
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 next_frame(self): if self.debug: print >> stderr, 'prepare_image:', (self.screen.out_width, self.screen.out_height) img = self.screen.prepare_image(self.cursor_image, self.cursor_offset, self.cursor_pos) strFrame = convert_image_to_string_rgb(img) bmpFrame = vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, (self.screen.out_width, self.screen.out_height), (strFrame, None, None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P) encFrame = self.encoder.encode(yuvFrame) self.out_file.write(encFrame.data) MovieOutputStream.next_frame(self) return
def doFrameDump(self): if self.dumpEncoder: import pymedia.video.vcodec as vcodec ss = pygame.image.tostring(manager.surface, "RGB") bmpFrame = vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, manager.surface.get_size(), (ss, None, None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P) d = self.dumpEncoder.encode(yuvFrame) self.dumpFile.write(d.data) return if self.dumpAppend: filename = self.dumpFilename else: filename = self.dumpFilename % self.PlayFrame print(filename) if self.dumpPPM: # Dump a PPM file. We do PPM by hand since pygame # doesn't support it directly, but it's so easy and # useful. w, h = manager.surface.get_size() if self.dumpAppend: f = open(filename, 'ab') else: f = open(filename, 'wb') f.write('P6\n%s %s 255\n' % (w, h)) f.write(pygame.image.tostring(manager.surface, 'RGB')) else: # Ask pygame to dump the file. We trust that pygame knows # how to store an image in the requested format. pygame.image.save(manager.surface, filename)
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 #s = mux.write(astreamid, encodedframe) # returns a stream #f.write(s) # the stream can then be written to disk for frame in m.data: frame = frame.T frame = enlarge(frame, enlargex) frame = np.asarray([frame, frame, frame]).T bmpFrame = vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, (width, height), (frame.tostring(), None, None)) yuvFrame = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P) encodedframe = ve.encode(yuvFrame) s = mux.write(vstreamid, encodedframe) # returns a stream f.write(s) # the stream can then be written to disk #f.write(d.data) f.close() #create muxer #mux = muxer.Muxer( 'avi' ) # or mpg or something #s1= vc1.encode( vfr ) #m_data.append( ( v_id1, s1 ))