def record(window, filename, fps): ''' Record an animated window to a movie file. :param window: window that is being recorded. :param filename: name of the file to write the movie to. :param fps: framerate at which to record. ''' writer = FFMPEG_VideoWriter(filename, (window.width, window.height), fps=fps) fbuffer = np.zeros((window.height, window.width, 3), dtype=np.uint8) # Function to write one frame def write_frame(writer): gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, fbuffer) writer.write_frame(np.flipud(fbuffer)) # Modify the on_draw event handler to also write a # movie frame every time it is called old_on_draw = window.get_handler('on_draw') @window.event def on_draw(dt): old_on_draw(dt) write_frame(writer) yield writer.close()
def declare_writer(self): """ Create a ffmpeg writer and define the output file name. For this to work a change to the FFMPEG_VideoWriter backend has to be done: - search and remove -an - optional so the song works: add '-shortest' to the audio option - https://github.com/Zulko/moviepy/pull/968 if merged maybe this is not needed anymore Returns: Ffmpeg writer and the frame buffer. """ shape = 512 try: audio_file = song_dict[self.song_name] except KeyError: audio_file = None from glumpy.ext.ffmpeg_writer import FFMPEG_VideoWriter # NOQA self.output_file_name = ( str(self.video_folder) + f"/{self.song_name}_" + datetime.now().strftime("%d:%m_%H:%M") + ".mp4" ) ffpmeg_vw = FFMPEG_VideoWriter( filename=self.output_file_name, size=(shape, shape), fps=self.fps, preset="veryfast", codec="libx264", # h264 audiofile=audio_file, ) frame_buffer = np.zeros((shape, shape, 3), dtype=np.uint8) return ffpmeg_vw, frame_buffer
fragment = """ uniform sampler2D texture; varying vec2 v_texcoord; void main() { float r = texture2D(texture, v_texcoord).r; gl_FragColor = vec4(vec3(r),1.0); } """ width, height = 512, 512 window = app.Window(width, height, color=(0.30, 0.30, 0.35, 1.00)) duration = 5.0 framerate = 60 writer = FFMPEG_VideoWriter("cube.mp4", (width, height), fps=framerate) fbuffer = np.zeros((window.height, window.height, 3), dtype=np.uint8) @window.event def on_draw(dt): global phi, theta, writer, duration window.clear() gl.glEnable(gl.GL_DEPTH_TEST) cube.draw(gl.GL_TRIANGLES, faces) # Write one frame if writer is not None: if duration > 0: gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
def run(clock=None, framerate=None, interactive=None, duration=sys.maxint, framecount=sys.maxint): """ Run the main loop Parameters ---------- clock : Clock clock to use to run the app (gives the elementary tick) framerate : int frames per second duration : float Duration after which the app will be stopped framecount : int Number of frame to display before stopping. """ global __running__ clock = __init__(clock=clock, framerate=framerate, backend=__backend__) options = parser.get_options() if interactive is None: interactive = options.interactive writer = None if options.record: from glumpy.ext.ffmpeg_writer import FFMPEG_VideoWriter framerate = 60 window = __backend__.windows()[0] width, height = window.width, window.height filename = "movie.mp4" writer = FFMPEG_VideoWriter(filename, (width, height), fps=framerate) fbuffer = np.zeros((height, width, 3), dtype=np.uint8) data = fbuffer.copy() log.info("Recording movie in '%s'" % filename) if interactive: # Set interactive python session os.environ['PYTHONINSPECT'] = '1' import readline readline.parse_and_bind("tab: complete") def run(): while not stdin_ready(): __backend__.process(clock.tick()) return 0 inputhook_manager.set_inputhook(run) else: __running__ = True count = len(__backend__.windows()) while count and duration > 0 and framecount > 0 and __running__: dt = clock.tick() duration -= dt framecount -= 1 count = __backend__.process(dt) # Record one frame (if there is writer available) if writer is not None: gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, fbuffer) data[...] = fbuffer[::-1, :, :] writer.write_frame(data) if writer is not None: writer.close()