Playing videos

OPENRNDR comes with FFMPEG-backed video support.

A simple video player

Relevant APIs
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.renderTarget
import org.openrndr.extra.fx.blur.BoxBlur
import org.openrndr.ffmpeg.VideoPlayerFFMPEG
import org.openrndr.ffmpeg.loadVideo

fun main() = application {
    program {
        val videoPlayer = loadVideo("data/video.mp4")
        videoPlayer.play()
        extend {
            drawer.clear(ColorRGBa.BLACK)
            videoPlayer.draw(drawer)
        }
    }
}

Looping a video

// Loop: restart when reaching the end
videoPlayer.ended.listen {
    videoPlayer.restart()
}

extend {
    videoPlayer.draw(drawer)
}

Video from camera devices

The VideoPlayerFFMPEG class can be used to get and display video data from camera devices. To open a camera device you use the fromDevice(null) method. The first argument specifies the audio device to use, which we can set to null for video-only.

VideoPlayerFFMPEG has minimal device listing capabilities. The device names of available input devices can be listed using VideoPlayerFFMPEG.listDeviceNames.

Relevant APIs
Examples
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.renderTarget
import org.openrndr.extra.fx.blur.BoxBlur
import org.openrndr.ffmpeg.VideoPlayerFFMPEG
import org.openrndr.ffmpeg.loadVideo

fun main() = application {
    program {
        val videoPlayer = VideoPlayerFFMPEG.fromDevice(null)
        videoPlayer.play()
        extend {
            drawer.clear(ColorRGBa.BLACK)
            videoPlayer.draw(drawer)
        }
    }
}

Processing video

Video playback can be combined with render targets and filters.

In the following example the video player output is blurred before presenting it on the screen.

import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.renderTarget
import org.openrndr.extra.fx.blur.BoxBlur
import org.openrndr.ffmpeg.VideoPlayerFFMPEG
import org.openrndr.ffmpeg.loadVideo

fun main() = application {
    program {
        val videoPlayer = loadVideo("data/video.mp4")
        val blur = BoxBlur()
        val renderTarget = renderTarget(width, height) {
            colorBuffer()
        }
        videoPlayer.play()

        extend {
            drawer.clear(ColorRGBa.BLACK)
            // -- draw the video on the render target
            drawer.withTarget(renderTarget) {
                videoPlayer.draw(drawer)
            }
            // -- apply a blur on the render target's color attachment
            blur.apply(renderTarget.colorBuffer(0), renderTarget.colorBuffer(0))
            // -- draw the blurred color attachment
            drawer.image(renderTarget.colorBuffer(0))
        }
    }
}

edit on GitHub