Thursday, April 28, 2011

What is the better way to create thumbnails from video in PHP?

I ma creating a video broadcasting site, in which i need to know how to create thumbnails from the video. Please Help. Any Suggestions or References will be highly appreciated

From stackoverflow
  • You can use ffmpeg.

    Once it's installed on your server, you can use it in PHP by writing a command line, and calling exec on it.

    e.g.:

    exec('ffmpeg -i mymovie.mov -vcodec mjpeg -vframes 1 -an -f rawvideo -s 64x64 foo.jpg');
    
  • Here's a snippet to grab a frame from the middle of a video. It is old code I have lying around, so I'm sure it could be simplified. Be sure to adjust the ffmpeg path in line 1 to match the location of your install.

    $output = shell_exec("/usr/local/bin/ffmpeg -i {$path}");
    preg_match('/Duration: ([0-9]{2}):([0-9]{2}):([^ ,])+/', $output, $matches);
    $time = str_replace("Duration: ", "", $matches[0]);
    $time_breakdown = explode(":", $time);
    $total_seconds = round(($time_breakdown[0]*60*60) + ($time_breakdown[1]*60) + $time_breakdown[2]);
    shell_exec("/usr/local/bin/ffmpeg -y  -i {$input_filepath} -f mjpeg -vframes 1 -ss " . ($total_seconds / 2) . " -s {$w}x{$h} {$output_filepath}";
    
  • I've used ffmpeg, starting a task with exec(), and append an ampersand to the command so that teh php script can continue. If I want ffmpeg to finish first, I don't have an ampersand.

    From the FAQ: "How do I encode movie to single pictures?" http://ffmpeg.org/faq.html#SEC15

    ffmpeg -i movie.mpg movie%d.jpg
    

    This generates many images.

    From the docs: "Video and Audio file format conversion" http://ffmpeg.org/ffmpeg-doc.html#SEC5

    ffmpeg -i foo.avi -r 1 -s WxH -f image2 -vframes 1 foo.jpeg
    

    Replace W & H with width/height (see http://ffmpeg.org/ffmpeg-doc.html#SEC9 ), replace vframes with number of frames wanted, add %d to filename for sequence number if you're doing multiple images. To grab at a certain point in the video, use -ss (see http://ffmpeg.org/ffmpeg-doc.html#SEC8 )

    Rajasekar : How to install ffmpeg? plz help
    Derek Gathright : @Rajasekar: What OS are you using? Google either "install ffmpeg windows/osx/linux" depending on what you are using.
    Rajasekar : I need it for php. To Install ffmpeg they r asking to edit php.ini file. I am working in my local system. I want to know while hosting any problem would arise? will the hoster allow us to edit php.ini??. Or we have to use any special type of hosting???@Derek Gathright
    Mead : Depends on the hosting provider - if you're not paying much, don't expect to be allowed to install software or have php.ini altered.
    derobert : I've edited your post for you to enable the links. Hope this helps :-)

0 comments:

Post a Comment