Laravel + FFmpeg: The Most Powerful Combination No One Told You About!
March 3, 2024
A client who owns hundreds of short exercise videos needed a tool for easily generating workout sets for their patients. I used Laravel and FFmpeg to achieve this.
FFmpeg enabled:
- Merging video files (concatenation)
- Changing video resolution
- Adding watermarks and text
Optimizing files for better performance
How did I do it?
Installing FFmpeg on a Linux server – just one command:
sudo apt install ffmpeg
Integration with Laravel – no external packages, full control via
exec()
.
Using specific FFmpeg commands, such as:
Merging videos:
$command = "ffmpeg -i $oldPath -i $blackBoxPath -filter_complex \"$overlay\" -c:v libx264 -c:a copy $newPath"; exec($command);
Adding a watermark:
$command = "ffmpeg -i $oldPath -i $logoPath -filter_complex \"[0:v][1:v]overlay=W-w-0:0:enable='between(t,0,999999)'\" -c:a copy $newPath"; exec($command);
Reducing file size:
$command = "ffmpeg -i $oldPath -c:v libx264 -preset medium -crf 31 -c:a aac -qscale:a 0.75 -vf \"scale='trunc(iw/2)*2':'trunc(ih/2)*2'\" $newPath"; exec($command);
How did I optimize performance?
- Reducing resolution → Faster encoding
- H.264 codec → High quality, smaller file size
Adjusting parameters → Balancing quality and speed
The biggest challenge?
FFmpeg documentation! Not everything was clear, so testing different settings was key.
What about you? Have you used FFmpeg in your projects?
What was your biggest challenge?
Reach out to me! Find me on linkedin!
Want to stay updated? Join my newsletter and get a weekly report on the most exciting industry news! 🚀