Is there any way that I can speed up the ffmpeg processing time
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Optimizing FFMPEG Processing Time for Smooth Video Conversion
Introduction: FFmpeg is an extremely powerful tool that allows developers to easily convert videos between various formats and perform complex operations on them. However, it can sometimes take a long time to process the video as seen in your case where uploaded videos of different sizes still take a significant amount of processing time. This blog post will provide practical solutions for speeding up FFMPEG processing times while maintaining its functionality.
1. Analyzing Your Code: First, let's examine your provided code block to see if there are any obvious bottlenecks or areas that can be improved:
```php
public function handle() {
//... (your existing code)
}
```
2. Understanding FFMPEG Processing Time: The FFMPEG processing time largely depends on the complexity of the video, its original format and quality, as well as the desired output format and quality. As your code is creating several different formats for each video, this could increase the overall processing time considerably.
3. Optimizing the Code for Speed: To speed up the FFMPEG processing time, we can refactor the code to combine similar tasks into single operations and use advanced features provided by FFMPEG. For example:
```php
public function handle() {
// Use one process that creates multiple output formats and qualities simultaneously
$formats = [
['bitrate' => 613, 'quality' => 'low', 'profile' => 'x264_low'],
['bitrate' => 906, 'quality' => 'medium-low', 'profile' => 'x264_midLow'],
//... (include other desired qualities and profiles)
];
FFMpeg::fromDisk('s3')
->open($this->movie->path)
->exportForHLS()
->onProgress(function ($percent) {
$this->movie->update([
'percent' => $percent
]);
})
->setSegmentLength(10) // optional
// Use a batch operation to create multiple output formats and qualities simultaneously
->batchMapOutputFiles($formats, function ($format, callback) use (&$bitrate, &$profile) {
$bitrate = new X264('aac');
// Set the bitrate and profile for each format based on the input array
$bitrate->setKiloBitrate($this->format['bitrate']);
$videoProfile = 'x264_' . strtolower(str_replace(' ', '_', $this->format['profile']));
$audioCodec = 'aac';
// Create the new video file with the desired format and output it to both S3 and your local storage
FFMpeg::fromDisk('s3')
->inputFormatContext()
->outputOptions("-c:v $videoProfile -c:a $audioCodec")
->addInputFile($this->movie->path)
->setOutputFileNameBase("public/Movies/{$this->movie->id}/{$this->movie->id}_" . strtolower($this->format['quality']) . "_" . str_replace(' ', '_', $videoProfile))
)
->save(callback, 's3');
})
// Save the master HLS playlist file for your server
->toDisk('s3')
->save("public/Movies/{$this->movie->id}/{$this->movie->id}.m3u8");
}
```
4. Other Optimization Strategies: If the above changes do not significantly improve your processing time, consider the following options:
- Upgrade or change your server hosting provider for better performance.
- Use a dedicated media-encoding service like AWS Elemental MediaConvert or Google Cloud Video Intelligence to handle the video processing.
- Pre-process videos on the user's device before uploading them, so the files are already in the desired format by the time it reaches your server.
Conclusion: In order to speed up FFMPEG processing times and maintain its functionality, we discussed analyzing our existing code for opportunities to improve performance and optimizing the code by combining tasks into batch operations. Additionally, upgrading or changing hosting providers and pre-processing videos before uploading them can also help in achieving faster video conversions. By following these strategies, you can have a more efficient and responsive system for handling video processing tasks.