Laravel Packages

Laravel FFmpeg 9: Encode Video and Stream HLS and DASH

Published
Laravel FFmpeg 9: Encode Video and Stream HLS and DASH image

Laravel FFmpeg 9 is a full rewrite of the package that Pascal Baljet started in 2016. Earlier versions wrapped the PHP-FFMpeg library. Version 9 drops that dependency and runs FFmpeg through Laravel's own Process, Storage, Http, and Queue features, so exports use the disks and queue connections your app already has.

The API is also new. Format objects and filter factories are gone. You pass FFmpeg settings as named arguments, and save() returns a result that lists every file the export created. This example makes a 480p copy of an uploaded recording:

use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;

$result = FFMpeg::disk('uploads')
    ->open('webinars/raw-recording.mov')
    ->export()
    ->resize(width: 854)
    ->video('libx264', crf: 26)
    ->audio('aac', bitrate: '96k')
    ->toDisk('videos')
    ->save('webinars/480p.mp4');

Version 9 also has builders for storyboards, GIF previews, and background music.

Adaptive HLS With Encryption

The hls() method creates an adaptive stream. You add one variant for each quality level, and encrypt() saves the AES-128 keys to a separate disk. Here, the keys go to the hls-keys disk, and a new key starts every five segments:

use ProtoneMedia\LaravelFFMpeg\HlsVariant;

$recording = FFMpeg::disk('uploads')->open('webinars/raw-recording.mov');

$result = $recording->hls(segmentDuration: 6)
    ->variant('480p', fn (HlsVariant $variant) => $variant
        ->resize(height: 480)
        ->video('libx264', bitrate: '1200k'))
    ->variant('1080p', fn (HlsVariant $variant) => $variant
        ->resize(height: 1080)
        ->video('libx264', bitrate: '5000k'))
    ->encrypt('hls-keys', static fn (string $keyPath) => route('webinars.key', ['key' => basename($keyPath)], absolute: false),
        rotateEvery: 5)
    ->toDisk('streams')
    ->save('webinars/17/master.m3u8');

Version 8 users might know this setup from Laravel HLS, a package we covered last year that converts Eloquent models to HLS video on top of Laravel FFmpeg 8.

DASH is new in version 9. The dash() builder groups representations into adaptation sets. If you call sharedHls(), the same segments also serve HLS players, so you store the media one time.

Serving Protected HLS Streams

The Hls facade registers signed routes for playback. After an export, you store the result's playback inventory on your model:

$webinar->hls_playback = $result->playback()->toArray();
$webinar->save();

Then you bind the model, tell the package how to find the inventory and who can watch it, and register the route:

use App\Models\Webinar;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use ProtoneMedia\LaravelFFMpeg\Http\HlsPresentation;
use ProtoneMedia\LaravelFFMpeg\Support\Hls;

Route::model('webinar', Webinar::class);

Hls::define('webinar',
    resolve: fn (Webinar $webinar) => HlsPresentation::fromArray($webinar->hls_playback),
    authorize: fn (Webinar $webinar) => Gate::authorize('view', $webinar),
);

Hls::routes('/webinars/{webinar}/hls', name: 'webinars.hls', playback: 'webinar');

To give a viewer a playback link, generate a signed URL with an expiry time. If you leave out expires, the link lasts 30 minutes:

$url = Hls::url('webinars.hls', ['webinar' => $webinar], expires: now()->addMinutes(15));

The package signs each playlist, segment, and key URL, and it runs your authorize callback again on every request. Players such as Video.js detect HLS from the URL, so you do not need to pass a type option. Pascal notes that signed URLs and AES-128 are not DRM.

Queued and Parallel Conversions

Encoding can take minutes, so you can call queued() before save() to send the export to a queue worker. If you pass parallel: true on an HLS export, independent variants encode on separate workers in a Laravel batch. Variants that share an audio or subtitle group stay in one job. A final job writes the master playlist:

FFMpeg::disk('uploads')
    ->open('webinars/raw-recording.mov')
    ->hls(segmentDuration: 6)
    ->toDisk('streams')
    ->audio('aac', bitrate: '128k')
    ->variant('sd', fn ($v) => $v->resize(width: 854)->video(bitrate: '1200k'))
    ->variant('hd', fn ($v) => $v->resize(width: 1920)->video(bitrate: '5000k'))
    ->queued(queue: 'video', parallel: true)
    ->save('webinars/17/master.m3u8');

You can track progress with onProgress(), which receives a Progress object with a nullable percentage. If an export fails, the package throws a MediaException. It has a category, such as launch, encoder, or permission, and diagnostics with FFmpeg's error output.

Checking Your FFmpeg Setup

Pascal used AI agents to sort through the package's issue tracker. The most common problem was that PHP could not find or start the FFmpeg binary, often because of a wrong path. The new ffmpeg:doctor command checks for this. It also confirms that FFmpeg and FFprobe report the same version and that the temp directory is writable. You can repeat the --encoder, --muxer, and --filter options, and the command exits with an error if one of them is missing from your FFmpeg build:

php artisan ffmpeg:doctor --encoder=libx264 --encoder=aac --muxer=hls

The package also includes guidelines for Laravel Boost, so AI agents that work in your app use the version 9 API and not calls from older releases. Run php artisan boost:install to add them.

Installation and Upgrading

Version 9 requires PHP 8.4, Laravel 13.44.1, and FFmpeg and FFprobe 9.0 or later. The CI suite runs against a real FFmpeg 9.0.2 build. Install it with Composer:

composer require pbmedia/laravel-ffmpeg

Apps on Laravel 9 through 12 can stay on version 8, which has its docs on the 8.x branch. If you upgrade from version 8, read the upgrade guide. It shows version 8 and version 9 code side by side. You can read more about the rewrite in Pascal's announcement post, and the source code and full documentation are on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Migration Durations and Job Memory Usage in Laravel 13.45 image

Migration Durations and Job Memory Usage in Laravel 13.45

Read article
Laravel CLI: One Binary for New PHP and Rust Laravel Apps image

Laravel CLI: One Binary for New PHP and Rust Laravel Apps

Read article
Phactorial: Render LaTeX Math to SVG and PNG in PHP image

Phactorial: Render LaTeX Math to SVG and PNG in PHP

Read article
Laravel, reimagined image

Laravel, reimagined

Read article
Laravel Rust: A New Rust Web Framework From Laravel image

Laravel Rust: A New Rust Web Framework From Laravel

Read article
Laravel Converse and Agent Kit for Chat Bots and AI Agents image

Laravel Converse and Agent Kit for Chat Bots and AI Agents

Read article