FFMPEG: get a thumbnail from a video with PHP

Here’s a simple tutorial in order to extract a thumbnail from a video using FFMPEG and PHP.

First of all let’s install FFMPEG ( you probably already got it if you know what kind of kick-ass video manipulator it is ):

sudo apt-get install ffmpeg

Then let’s set up our script: the main problem is the use of the shell_exec() function, in order to run a shell command inside PHP.

If you don’t know anything about cmd(), shell_exec() or shell escaping please don’t use this tecnique and go ahed to php.net to read some documentation about it.

At least, you also need to have shell_exec() enabled in your server, so shared hosting won’t fit your requirements.

Here’s the way:


$video = 'path/to/video.flv';
$thumbnail = 'path/to/thumbnail.jpg';

// shell command [highly simplified, please don't run it plain on your script!]
shell_exec("ffmpeg -i $video -deinterlace -an -ss 1 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $thumbnail 2>&1");

so you’ll be able to see your thumbnail of the video at the location you specified.

Hi there! I recently wrote an ebook on web application security, currently sold on leanpub, the Amazon Kindle store and gumroad.

It contains 160+ pages of content dedicated to securing web applications and improving your security awareness when building web apps, with chapters ranging from explaining how to secure HTTP cookies with the right flags to understanding why it is important to consider joining a bug bounty program.

Feel free to skim through some of the free chapters published on this blog and, if the content seems interesting enough to you, grab a copy on leanpub, the Amazon Kindle store, gumroad or simply checkout right down below!

Buy the Web Application Security ebook for $9.99

In the mood for some more reading?

...or check the archives.