def save(self, path): width, height = self.width, self.height count = 0 fd = open(path, 'w') encoder = Encoder(width, height) image = Image(width, height, vpx.VPX_IMG_FMT_I420) timebase = encoder.cfg.g_timebase.den, encoder.cfg.g_timebase.num fd.write(ivf_file_header(width, height, timebase, count)) for t, frame in self.frames: print "streaming frame %i..." % t, with Image(width, height, vpx.VPX_IMG_FMT_RGB24, getPixels(frame)) as src: src.convertTo(image) image.flip() for kind, packet in encoder.encode(image, t): if kind == vpx.VPX_CODEC_CX_FRAME_PKT: fd.write(ivf_frame_header(len(packet), t)) fd.write(packet) count += 1 print "ok." image.free() fd.seek(0) fd.write(ivf_file_header(width, height, timebase, count)) fd.close() encoder.close()
le32(0x30385056), #fourcc? le16(width), le16(height), le32(rate), le32(scale), le32(frame_count), le32(0), #unused ]) def ivf_frame_header(size, pts): return le32(size) + le64(pts) def save(path, (width, height), frames): count = 0 fd = open(path, 'w') image = Image(width, height, vpx.VPX_IMG_FMT_I420) encoder = Encoder(width, height) width = encoder.cfg.g_w height = encoder.cfg.g_h timebase = encoder.cfg.g_timebase.den, encoder.cfg.g_timebase.num fd.write(ivf_file_header(width, height, timebase, count)) for t, frame in frames: image_src = Image(width, height, vpx.VPX_IMG_FMT_RGB24, buffer(frame)) image_src.convertTo(image) image.flip() for kind, packet in encoder.encode(image, t): if kind == vpx.VPX_CODEC_CX_FRAME_PKT: fd.write(ivf_frame_header(len(packet), t)) fd.write(packet) count += 1 image.free()