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

about me

profile picture

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 me

follow me

newsletter

A weekly email with the latests articles

support my work

Start trading on binance
  • BTC

    BTC

    18SY81ejLGFuJ9KMWQu5zPrDGuR5rDiauM

  • ETH

    ETH

    0x519e0eaa9bc83018bb306880548b79fc0794cd08

  • Monero

    XMR

    895bSneY4eoZjsr2hN2CAALkUrMExHEV5Pbg8TJb6ejnMLN7js1gLAXQySqbSbfzjWHQpQhQpvFtojbkdZQZmM9qCFz7BXU

2024 © My Dynamic Production SRL All rights Reserved.