From fecebfd39809f862e098805219b15c50588bb81a Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 30 Aug 2026 22:41:25 +0530 Subject: Development setup + toggling recording --- .../kotlin/phenax/extensions/HeadlessRecording.kt | 53 ++++++++++++++++++++++ src/main/kotlin/phenax/utils/program.kt | 9 ++++ 2 files changed, 62 insertions(+) create mode 100644 src/main/kotlin/phenax/extensions/HeadlessRecording.kt create mode 100644 src/main/kotlin/phenax/utils/program.kt (limited to 'src/main/kotlin/phenax') diff --git a/src/main/kotlin/phenax/extensions/HeadlessRecording.kt b/src/main/kotlin/phenax/extensions/HeadlessRecording.kt new file mode 100644 index 0000000..78cde2c --- /dev/null +++ b/src/main/kotlin/phenax/extensions/HeadlessRecording.kt @@ -0,0 +1,53 @@ +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() + } + } +} diff --git a/src/main/kotlin/phenax/utils/program.kt b/src/main/kotlin/phenax/utils/program.kt new file mode 100644 index 0000000..2d3e04b --- /dev/null +++ b/src/main/kotlin/phenax/utils/program.kt @@ -0,0 +1,9 @@ +package phenax.utils + +fun recordDuration(durationSeconds: Int) { + System.setProperty("phenax.record.frameCount", (60 * durationSeconds).toString()) +} + +fun recordDuration(): Int? = System.getProperty("phenax.record.frameCount")?.toIntOrNull() + +fun recordingEnabled(): Boolean = System.getenv("RECORD") != null -- cgit v1.3.1