FFMPeg is a complete, cross-platform solution for recording, converting, and streaming audio and video. It is useful when we want to convert between video/audio formats and make some edits. See this tutorial to discover how you can add it to Laravel. First, create a project. The tutorial is here.
Also, it is always better to do the heavy computation on the server side to prevent our client from freezing. Have you ever experienced a website that blocks your browser?
Installation
First, we will install the package for Laravel: https://github.com/protonemedia/laravel-ffmpeg
composer require pbmedia/laravel-ffmpeg
You don’t require to add the next info for Laravel 11 (optional):
// config/app.php
'providers' => [
...
ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider::class,
...
];
'aliases' => [
...
'FFMpeg' => ProtoneMedia\LaravelFFMpeg\Support\FFMpeg::class
...
];
Now, run the next command to generate the configuration file for FFMpeg:
php artisan vendor:publish --provider="ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider"
The next step is to download it from Download FFmpeg:
If you are using Windows, an easy way is to run the next command in the Powershell:
winget install "FFmpeg (Essentials Build)"
- See the installation files with
winget --info
- It should be in
echo %LOCALAPPDATA%\Microsoft\WinGet\Package
Locate the installation file and, if you prefer, copy the complete folder to C:ffmpeg
and add the next vars in the .env file in Laravel, you can specify a different route:
FFMPEG_BINARIES=C:\FFMPEG\bin\ffmpeg.exe
FFPROBE_BINARIES=C:\FFMPEG\bin\ffprobe.exe
Testing it
You can test it by converting a video type to another; the default file directory that will open is located at storage\app:
You can download the example video here
- storage\app\ocean.webm
Now, let’s create a Controller and place the next code:
php artisan make:controller HomeController
<?php
namespace App\Http\Controllers;
use FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
class HomeController extends Controller
{
public function test(){
FFMpeg::open('ocean.webm')
->export()
->inFormat(new \FFMpeg\Format\Video\X264)
->resize(640, 480)
->save('ocean.mp4');
}
}
Add the next line in routes\web.php
Route::get('/test', [App\Http\Controllers\HomeController::class, 'test'])->name('home');
Open the next URL to call the method 127.0.0.1:8000/test
And verify that storage\app\private\ocean.mp4 exists
For more safety, you can also add a try-catch
block that will allow you to display custom error messages
try {
FFMpeg::open('yesterday.mp3')
->export()
->inFormat(new Aac)
->save('yesterday.aac');
} catch (EncodingException $exception) {
$command = $exception->getCommand();
$errorLog = $exception->getErrorOutput();
}
Why use FFmpeg with Laravel?
FFmpeg is an open-source multimedia framework that allows developers to handle video, audio, and other multimedia files. Laravel is one of the most popular PHP frameworks, known for its elegant syntax and robust features. Integrating it with Laravel offers many benefits, especially for developers working on projects involving media processing, such as video streaming, editing, or conversion.
- Simplifies Multimedia Processing
- High Efficiency
- Extensive File Format Support (mp4,avi, mp3, webm, etc)
- Powerful Video Transcoding
- Real-Time Media Handling
- Audio Processing
- Adding Watermarks and Subtitles
- Handling Large Files
Happy coding 🙂
Thanks, good tutorial