All posts
Pascal Baljet
Pascal Baljet ·

Laravel FFmpeg 9: A reimagined API for video encoding, HLS, and DASH streaming

Laravel FFmpeg 9 is here! It's a complete rewrite, with a reimagined API for encoding video, processing audio, and streaming HLS and DASH in Laravel apps. And yes, that's two posts in two days, as I introduced Inertia Notifications only yesterday. I've been working on this release for the past few weeks, and it feels great to finally get it out the door.

When I wrote about version 7 back in 2020, the package had a few hundred thousand downloads. Today, it's in the millions, and it keeps growing every month. A lot has changed in those six years, both in Laravel and in how I like to build packages, and this release brings all of that together. Let's take a look at what's new!

Why Another Rewrite?

Since the very first release in 2016, Laravel FFmpeg has been built on top of PHP-FFMpeg. That package took care of running the binaries, while Laravel FFmpeg added the elegance of Laravel on top, like opening a video from one disk and exporting it to another. That worked great for ten years, but now it's time to move on and take full control of how the package talks to FFmpeg.

Meanwhile, Laravel itself gained pretty much everything a package like this needs. Storage, queues, and the HTTP client were already there, and Laravel 10 added the Process API, which I also used for Task Runner. Put those together, and you can run FFmpeg the Laravel way, without anything in between. So that's exactly what version 9 does. It uses Laravel's own components under the hood, so it works with the disks and queue connections you already have.

The best part? Leaving PHP-FFMpeg behind gave me the chance to rethink and reimagine the API from scratch. The old API followed the format and filter classes underneath it, so even resizing a video meant creating an X264 format object and writing a filter callback with a Dimension class. Version 9 starts from what you actually want to do, and you pass FFmpeg's settings as plain named arguments. Here's how you make a 720p version of a video:

use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
 
$result = FFMpeg::open('lessons/original.mp4')
->export()
->resize(width: 1280)
->video('libx264', crf: 23)
->audio('aac', bitrate: '128k')
->save('lessons/720p.mp4');

No more format objects. You name the encoder, and that's it! It works exactly the same for VP9 and Opus in WebM, or for MP3 audio:

$media->export()
->video('libvpx-vp9', crf: 31)
->audio('libopus', bitrate: '128k')
->save('lesson.webm');
 
$media->export()
->audio('libmp3lame', bitrate: '192k')
->save('lesson.mp3');

Ten Years of Issues, With a Little Help From AI

Maintaining a long list of open-source packages takes time, and I'll be honest, the issue tracker of Laravel FFmpeg didn't always get the attention it deserved. Over the years, it filled up with bug reports, questions, feature requests, and plenty of "it doesn't work on my server" reports. Many of them deserved a proper look, but FFmpeg problems are hard to reproduce. You need the same FFmpeg build, the same media file, and often the exact same server setup.

So before and during the rewrite, I asked AI agents to go through every open issue, comments included, and the open pull requests as well. For each one, they figured out whether it was a bug in the package, a missing feature, or a problem with the server setup, and they tried to reproduce it with FFmpeg 9 in a fresh Laravel app. That last part turned out to be the most useful. Instead of a pile of old tickets, I ended up with a clear picture of what people actually run into when they use the package.

Some patterns were impossible to miss. The most common problem had nothing to do with video at all. PHP simply couldn't find or start the FFmpeg binary, usually because of a wrong path. So version 9 now tells you exactly what's wrong when that happens, and it comes with an ffmpeg:doctor command to check your setup. Another one that kept coming back was serving encrypted HLS to players like Video.js, which is why version 9 now serves the playlists and keys for you. The same goes for hardware acceleration flags and HLS playlist types. So if you opened an issue over the years, there's a good chance it helped shape this release.

Results, Progress, and Failures

Now let's talk about what happens when you call save(). In version 9, it returns a result that lists every file the export generated. That's especially handy for HLS, where a single export easily produces dozens of playlists, segments, and keys. Progress updates come in as a Progress object, and when something goes wrong, you get a MediaException:

use ProtoneMedia\LaravelFFMpeg\Exceptions\MediaException;
use ProtoneMedia\LaravelFFMpeg\Progress;
 
try {
$result = $media->export()
->onProgress(function (Progress $progress) {
$progress->percentage; // Nullable, like speed and remainingSeconds.
})
->video('libx264')
->audio('aac')
->save('lesson.mp4');
 
$result->files();
} catch (MediaException $exception) {
$exception->category;
$exception->diagnostics;
}

Debugging FFmpeg used to mean digging through pages of output, so I'm really happy with this one. The exception has a category that tells you what went wrong. For example, launch means the binary couldn't start, encoder means your FFmpeg build doesn't include the encoder, and permission means the process can't write to the destination. Need more details? The diagnostics hold FFmpeg's own error output.

Ever wondered what command the package actually runs? The inspect() method returns the FFmpeg arguments without running anything. And remember the ffmpeg:doctor command? It checks that your binaries match, whether your FFmpeg build has the encoders and muxers your app needs, and whether the package can write its temporary files. I'd run it on every new server:

php artisan ffmpeg:doctor --encoder=libx264 --muxer=dash --filter=scale

Storyboards, GIF Previews, Background Music, and More

Some things come up over and over again in video apps: thumbnails, previews, a bit of music under a video. Version 9 has a dedicated builder for each of them. A few existed in version 8 in a different shape, like storyboards and frame exports, while others used to require hand-written FFmpeg filters. Let's look at a few. Here's a storyboard, a sprite sheet of thumbnails with WebVTT cues for the seek bar of your video player:

$media->storyboard(every: 5, columns: 5, rows: 3)
->resize(width: 160)
->vtt('cues/lesson.vtt')
->save('sheets/lesson-%03d.jpg');

Need a looping GIF preview of three seconds, for example to show when someone hovers over a video? That's just as easy:

$media->gif(width: 480, frameRate: 12)
->clip(4, duration: 3)
->save('preview.gif');

And how about some background music? This one mixes a track under the video at 20% volume and loops it until the video ends, while the video itself is copied as is:

$music = FFMpeg::disk('music')->open('background.mp3');
 
$media->backgroundMusic($music)
->video('copy')
->audio('aac')
->save('with-music.mp4');

There's more where that came from. You'll find frame() and frames() for stills, speed() to change the playback speed without changing the pitch, waveformPeaks() for your own waveform renderer, normalizeLoudness() for two-pass loudness normalization, and concat() to join clips.

HLS and DASH

HLS has been part of the package since 2016, and judging by the issue tracker, a lot of you use it. In version 9, HLS exports have their own builder. Each variant gets its own settings, and AES-128 encryption with key rotation is a single method call:

use ProtoneMedia\LaravelFFMpeg\HlsVariant;
 
$result = $media->hls(segmentDuration: 6)
->variant('360p', fn (HlsVariant $variant) => $variant
->resize(height: 360)
->video('libx264', bitrate: '800k'))
->variant('720p', fn (HlsVariant $variant) => $variant
->resize(height: 720)
->video('libx264', bitrate: '2500k'))
->encrypt('secrets', fn (string $keyPath) => basename($keyPath), rotateEvery: 3)
->toDisk('streams')
->save('lessons/42/master.m3u8');

New in this release is DASH, for H.264 or VP9 video with AAC audio. Call sharedHls() on the DASH builder, and the same segments serve an HLS playlist as well. You encode once and serve both formats from the same files, which saves you a lot of encoding time and storage.

Protected HLS Playback

Back in 2020, I wrote about encrypting your HLS exports. Encrypting was the easy part, though. To actually play those videos, you had to write your own routes for the playlists and the keys, and quite a few issues were about exactly that. Version 9 takes care of it with signed routes that serve the playlists, segments, and keys for you. After the export, you store the playback inventory on your model:

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

Next, you tell the package how to find and authorize a presentation, and you register the routes:

// In the boot method of your AppServiceProvider...
Route::model('lesson', Lesson::class);
 
Hls::define('lesson',
resolve: fn (Lesson $lesson) => HlsPresentation::fromArray($lesson->hls_playback),
authorize: fn (Lesson $lesson) => Gate::authorize('view', $lesson),
);
 
// In routes/web.php...
Hls::routes('/lessons/{lesson}/hls', name: 'lessons.hls', playback: 'lesson');

And that's it! Now you can hand out a playback URL that expires after ten minutes:

$url = Hls::url('lessons.hls', ['lesson' => $lesson], expires: now()->addMinutes(10));

Every playlist, segment, and key gets a signed URL with the same deadline, and every request runs your authorize callback again. The controller only serves files from the stored inventory, so a player can't request anything else. Players like Video.js detect the stream from the URL, so there's nothing to configure on the frontend either. To be clear, this isn't DRM, but it does make sure that only users who pass your policy can get to the files.

Queued Exports

Encoding video takes time, so you probably don't want to do it during a request. In version 8, that meant writing your own job around the export. In version 9, you call queued() before save(), and the export runs on a queue worker:

FFMpeg::disk('uploads')
->open('lesson.mp4')
->export()
->video('libx264', preset: 'fast')
->audio('aac')
->toDisk('media')
->queued(queue: 'conversions')
->save('lessons/encoded.mp4');

It gets even better with HLS. Pass parallel: true to the queued() method, and the variants encode on separate workers, as part of a Laravel batch. Once every part is done, a final job writes the master playlist, and the QueuedExportCompleted event tells you where to find it. Got a few workers sitting idle? Now they can all work on the same video.

Your AI Agent Knows Version 9 Too

Speaking of AI, a package with ten years of history has one downside. AI agents have seen a lot of code for the earlier versions, so that's what they tend to write, even when you've installed version 9. That's why the package now ships with Laravel Boost guidelines and a developing-laravel-ffmpeg skill. Install them with Boost, and your agent writes version 9 code from the start:

php artisan boost:install

Upgrading From Version 8

Let's not sugarcoat it. This release has breaking changes, and the format objects, the filter factories, and the calls that went straight to PHP-FFMpeg are gone. But I didn't want to leave you on your own with that. The upgrade guide walks you through every change, with a version 8 and a version 9 example side by side. Before the release, I put the guide to the test by upgrading an existing version 8 app through a long list of scenarios, and I fixed every gap I ran into along the way. Staying on version 8 for now? Its documentation remains available on the 8.x branch.

Installation

Version 9 requires PHP 8.4+, Laravel 13.44.1+, and FFmpeg and FFprobe 9.0+. Before it runs anything, the package checks that both binaries report the same release, so you won't end up debugging a mismatched pair. CI runs the full test suite against a real FFmpeg 9.0.2 build on PHP 8.4, 8.5, and 8.6. You can install it with Composer:

composer require pbmedia/laravel-ffmpeg

Free and Open Source

Laravel FFmpeg is free and open source under the MIT license. The README has the full documentation, and it covers a lot more than I could fit in this post, like hardware acceleration, HDR to SDR conversion, watermarks, and continuous HLS. You can also sponsor me on GitHub.

I'd love to hear what you build with it, so let me know on X. Happy encoding!