All chapters
Cookbook · Chapter 6

Working with video

Playing movie files, reading their pixels, controlling speed, seeking, and blending.

Umfeld decodes video through FFmpeg natively — no third-party library to install.

Playing a video

Movie loads a file; the movieEvent() callback fires when a new frame has decoded, which is where you call read(). Draw the movie like an image.

#include "Movie.h"

Movie* movie;

void setup() {
    movie = new Movie("video.mp4");
    movie->loop();                 // or play()
}

void movieEvent(Movie* m) {
    m->read();                     // swap in the freshly decoded frame
}

void draw() {
    background(0);
    image(movie, 0, 0);            // or image(movie, x, y, w, h)
}

Play, pause, stop, loop

void keyPressed() {
    if (key == 'p') movie->play();
    if (key == 's') movie->pause();
    if (key == 'S') movie->stop();
    if (key == 'l') movie->loop();
}

Controlling the speed

speed() is a multiplier — 1.0 is normal, 2.0 is double, 0.5 is half:

movie->speed(2.0f);
movie->speed(0.5f);
movie->speed(1.0f);

Jumping to a specific frame

jump() seeks to a time in seconds. time() and duration() report the playhead and the clip length, so you can step relative to where you are:

movie->jump(0.0f);                                  // back to the start
movie->jump(movie->time() + 5.0f);                  // forward 5 s
movie->jump(std::max(0.0f, movie->time() - 5.0f));  // back 5 s

Manipulating pixels in a video

A Movie exposes its frame like a PImage. After read(), load the pixels and read or rewrite them — the same loadPixels() / pixels[] / updatePixels() API as a still image:

void movieEvent(Movie* m) {
    m->read();
    m->loadPixels();
    for (int i = 0; i < m->width * m->height; i++) {
        color_t c = m->pixels[i];
        // e.g. invert: m->pixels[i] = color(255 - red(c), 255 - green(c), 255 - blue(c));
    }
    m->updatePixels();
}

Blending video

Stack a movie over other graphics with a blend mode (BLEND, ADD) or fade it with tint():

blendMode(ADD);
image(movie, 0, 0);
blendMode(BLEND);

tint(255, 128);          // 50% opacity
image(movie, 0, 0);
noTint();

On filters: Umfeld’s filter() is only partially implemented — some modes (such as GRAY) aren’t available yet. For per-pixel effects on video, prefer a shader (see the Camera & computer vision and shader examples) or hand-written pixels[] loops, which are fully supported.