Skip to content

Source filters

Most streams come from a file. Source filters make one from nothing — a solid color, SMPTE bars, a test pattern, a sine tone, silence. They have no input to attach to, so they don’t go through apply(); they’re an entry point of their own, typed by the kind of stream they produce.

A source comes from generate(), which takes a 1:1-named source class from the catalog — a VideoFilter for VideoStream::generate, an AudioFilter for AudioStream::generate:

use FFmpeg\{VideoStream, AudioStream};
use FFmpeg\Filter\{Color, Sine, Smptebars, Anoisesrc};
$black = VideoStream::generate(new Color(color: 'black', size: '1920x1080', duration: 3.0));
$tone = AudioStream::generate(new Sine(frequency: 440.0, duration: 2.0));
$bars = VideoStream::generate(new Smptebars(size: '1280x720'));
$noise = AudioStream::generate(new Anoisesrc(amplitude: 0.1));

The types keep the two halves from crossing: a source can’t be apply()’d (it has no input), and an ordinary filter can’t be generate()’d — both are compile-time errors, backed by a runtime check that points you at the right method.

The result is an ordinary stream node — filter it, overlay onto it, encode it, exactly like one from a file:

use FFmpeg\Filter\Scale;
$bars = VideoStream::generate(new Smptebars(size: '1280x720', duration: 5.0))
->apply(new Scale(640, 360));

A source is infinite unless you give it a duration. Inside a graph it’s bounded by whatever it’s combined with — the companion stream, or shortest on a mix — so you often don’t set one:

// silence as long as the video, to give a silent clip an audio track
use FFmpeg\Filter\Anullsrc;
$video = Media::open('timelapse.mp4')->videoStream();
$track = AudioStream::generate(new Anullsrc(duration: $video->duration));

Either way the result is an ordinary stream node — filter it, overlay onto it, encode it, exactly like one from a file. See Examples → Generate a stream from nothing.

ClassFFmpegProduces
Filter\ColorcolorA solid, uniformly colored frame
Filter\Smptebars, Filter\SmptehdbarssmptebarsSMPTE color bars
Filter\Testsrc, Filter\Testsrc2testsrcA test pattern
Filter\MandelbrotmandelbrotA Mandelbrot fractal
Filter\NullsrcnullsrcBlank video
Filter\SinesineA sine-wave tone
Filter\AnullsrcanullsrcSilence
Filter\AnoisesrcanoisesrcA noise signal
Filter\AevalsrcaevalsrcAudio from an expression

An Artisan Build project.

Built on FFmpeg — an independent binding, not affiliated with or endorsed by the FFmpeg project. Support FFmpeg.