package phenax.extensions import org.openrndr.* import org.openrndr.draw.* import org.openrndr.ffmpeg.VideoWriter import phenax.utils.recordDuration class HeadlessRecorder : Extension { override var enabled: Boolean = true var videoTarget: RenderTarget? = null var videoWriter: VideoWriter? = null var maxFrameCount: Int? = null override fun setup(program: Program) { val width = program.application.configuration.width val height = program.application.configuration.height program.width = width program.height = height videoWriter = VideoWriter().apply { frameRate = 60 size(width, height) output("video/${program.name}.mp4") start() } videoTarget = renderTarget(width, height) { colorBuffer() depthBuffer() } maxFrameCount = recordDuration() program.clock = { program.frameCount / (videoWriter?.frameRate?.toDouble() ?: 0.0) } } override fun beforeDraw(drawer: Drawer, program: Program) { videoTarget?.bind() program.backgroundColor?.let { drawer.clear(it) } } override fun afterDraw(drawer: Drawer, program: Program) { val target = videoTarget ?: return val writer = videoWriter ?: return target.unbind() writer.frame(target.colorBuffer(0)) if (program.frameCount >= (maxFrameCount ?: 60 * 10)) { writer.stop() program.application.exit() } } }