Pages

Subscribe:

Feb 8, 2010

How to Create Video Thumbnail in PHP and FFMPEG?

Using PHP, you can get the Thumbnail of a Video File uploaded in your website just like in youtube. In order to do this, it is required that you install FFMPEG to your server. FFMPEG website is here.

FFMPEG installation will vary depending on the Operating System of your server whether it is Linux of Windows. For the Steps to install FFMPEg on your linux or windows server, see FFMPEG documentation page here.

Now for the PHP code to get Thumbnail of a video file:


// where ffmpeg is located
                    $ffmpeg = '/usr/bin/ffmpeg';

                    // the input video file
                    $video  = 'URL of video file';

                    // where you'll save the image

                   $image  = 'URL of image file but the file extension of the video file will be replaced by .jpg';

                    // default time to get the image
                    $second = 1;

                    // get the duration and a random place within that
                    $cmd = "$ffmpeg -i $video 2>&1";
                    if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
                        $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
                        $second = rand(1, ($total - 1));
                    }

                    // get the screenshot
                    $cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
                    $return = `$cmd`;

                    echo 'done!';
                    //display the thumbnail.
                    echo '';

3 Comments:

web development said...

Nice post that for share this post. PHP is very famous and helpful for web development application.

Marvin Fernandez said...

thank you for that..

Gangedm said...

Hello, I have not understand this script. Please, explain in more details. or give to download...!

My email id- gangedm@gmail.com