Category
Top News in Month
Archives
.:: Create a random thumbnail of a video file  » Tutorials  

Create a random thumbnail of a video file

Looking at sites like YouTube, you may think it’s quite hard to create a lot of different thumbnails from video files, and have them from random times within that file. But, no, it’s not! As this article shows, by using the very fabulous FFmpeg library, it’s actually a very short amount of code that’s required to create all those lovely random thumbnails.

 

First a little information about FFmpeg. If you haven’t come across this yet then you’re missing out on an excellent bit of software to put in your tool kit. It can convert and stream all manner of video and audio files, is open source (under LGPL, but with some optional GPL bits), and is always being worked on and developed.

FFmpeg’s homepage is at http://ffmpeg.mplayerhq.hu/, where you can grab the latest revision from SVN. If you want it for Windows, then one location I found with quite recent builds is http://ffdshow.faireal.net/mirror/ffmpeg/, but if you know any better ones then let me know.

So on with the code…

01.<?php
02. 
03.// where ffmpeg is located, such as /usr/sbin/ffmpeg
04.$ffmpeg = 'ffmpeg';
05. 
06.// the input video file
07.$video  = dirname(__FILE__) . '/demo.mpg';
08. 
09.// where you'll save the image
10.$image  = dirname(__FILE__) . '/demo.jpg';
11. 
12.// default time to get the image
13.$second = 1;
14. 
15.// get the duration and a random place within that
16.$cmd = "$ffmpeg -i $video 2>&1";
17.if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
18.    $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
19.    $second = rand(1, ($total - 1));
20.}
21. 
22.// get the screenshot
23.$cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
24.$return = `$cmd
25. 
26.// done! <img src="http://blog.amnuts.com/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley">
27.echo 'done!';
28. 
29.?>

Nice and short, and that’s even with me being generous over white space!

The two key portions in this code are the ones to get the random time location of the video, which we do by parsing the output from a call to ffmpeg, and then running another command to generate the image.

When running the first command it’s important to remember the 2>&1 to redirect the stderr to stdout, as it’s actually an error message we want to capture. To be on the safe side, I simple add it to all my command line calls to FFmpeg (which in this example is only two!)

Now that you have your thumbnail image you may be thinking that you need to use GD, ImageMagick, or some other graphics library to resize the image, right? Well, you could, if you wanted to, but you could also do it in the very same command line to create the thumbnail! Basically, you’d use the -s flag to resize the thumbnail. For example:

1.$cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -s 320x240 -vcodec mjpeg -f mjpeg $image 2>&1";

You may also notice the -deinterlace flag in there. You may not need this – it depends on whether you video file needs deinterlacing or not. All of the flags are detailed in the FFmpeg documentation.

Oh, and if you’re taking user input for the video file or output image file, then please remember to escape your variables accordingly! Use something like the escapeshellcmd() PHP function.

Have fun grabbing your video frames!!

 

Source http://blog.amnuts.com/2007/06/22/create-a-random-thumbnail-of-a-video-file/

Check Download Links
Report Dead Link:
 (Votes #: 0)
Information
Members of Guest cannot leave comments.

Disclaimer: None of the files shown here are hosted or transmitted by this server,We only index and link to content provided by other sites

Donate via PAYPAL
Search
Last News
Calendar
«    May 2012    »
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
Online
{online}
Site Info
{siteinfo}
Statistics

IP

free counters

Site Visits
Today Visits: 66
Yesterday Visits: 942
This Week Visits: 3713
This Month Visits: 28443
This Year Visits: 125394
All Visits: 432126
Record: 7437 In 01.01.2011