How To Convert A Video File To GIF with PHP

profile picture

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 ๐Ÿ˜„

convert
gif
video
php

2020 My Dynamic Production SPRL All rights Reserved.