How To Convert A Video File To GIF with PHP
First you will need to install the linux package ffmpeg
sudo apt-get install ffmpeg
In your PHP Project, install the php-ffmpeg
package
composer require php-ffmpeg/php-ffmpeg
Assuming you have the full local path of the video to convert:
$videoPath = '/path/to/video.mp4';
// The gif duration will be as long as the video/
$ffprobe = FFProbe::create();
$duration = (int) $ffprobe->format($videoPath)->get('duration');
// The gif will have the same dimension. You can change that of course if needed.
$dimensions = $ffprobe->streams($videoPath)->videos()->first()->getDimensions();
$gifPath = '/path/to/gifvideo.gif';
// Transform
$ffmpeg = FFMpeg::create();
$ffmpegVideo = $ffmpeg->open($videoPath);
$ffmpegVideo->gif(TimeCode::fromSeconds(0), $dimensions, $duration)->save($gifPath);
That's it !! Easy right π
I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.
more about meBTC
bc1qgw9a8hyqqwvcls9ln7vhql4mad0qkveutr2td7
ETH
0x3A720717Da03dB6f3c4a4Ff08BC64100205A79d0
2025 © My Dynamic Production SRL All rights Reserved.