Forum Moderators: coopster

Message Too Old, No Replies

Generating a thumbnail from video files

         

wincode

9:21 am on Dec 26, 2010 (gmt 0)

10+ Year Member



Hi, I've been searching for a while but I'm unable to learn how to work this. I'm trying to learn how to (or at least find a script) that makes a thumbnail image from a video file (.flv) stored on my website. How can I attempt this? I've found some codes online such as:

<?php 

$ffmpegpath = "/usr/bin/ffmpeg";

function make_jpg($input, $output, $fromdurasec="01") {
global $ffmpegpath;
if(!file_exists($input)) return false;

$command = "$ffmpegpath -i $input -an -ss 00:00:$fromdurasec -r 1 -vframes 1 -f mjpeg -y $output";

@exec( $command, $ret );
if(!file_exists($output)) return false;
if(filesize($output)==0) return false;
return true;
}

#extract frame at 00:00:01 as jpeg
make_jpg("/folder/videofile.flv", "/test.jpg", "01");

?>


but I tried it and nothing really happened.

I appreciate any help!
Thanks

rainborick

3:52 pm on Dec 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Start by adding a path to your input and output files. Your current settings all start with "/", which will tell ffmpeg to look for the files in the root directory of the server. You should probably also test the value returned by ffmpeg for errors, rather than just looking for your output file.

Nutter

5:48 pm on Dec 26, 2010 (gmt 0)

10+ Year Member



Try taking the @ off the front of exec so you can see if there are any errors generated.

wincode

7:32 pm on Dec 26, 2010 (gmt 0)

10+ Year Member



I did what you guys told me, and none of it worked.

<?php 

$ffmpegpath = "/usr/bin/ffmpeg";

function make_jpg($input, $output, $fromdurasec="01") {
global $ffmpegpath;
if(!file_exists($input)) return false;

$command = "$ffmpegpath -i $input -an -ss 00:00:$fromdurasec -r 1 -vframes 1 -f mjpeg -y $output";

@exec( $command, $ret );
if(!file_exists($output)) return false;
if(filesize($output)==0) return false;
return true;
}

#extract frame at 00:00:01 as jpeg
make_jpg("folder/videofile.flv", "folder/test.jpg", "01");

?>


I did it with and without the @ in front of exec and none of it worked

Thanks for your time

Readie

2:41 am on Dec 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did it with and without the @ in front of exec and none of it worked

Prepending @ to a function in PHP turns off error reporting just for that function. I advise against it's use as it can make future bug hunting very difficult.

At any rate, it won't cause a function to return different results.

rocknbil

5:27 pm on Dec 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did what you guys told me


But you didn't. :-) Look:

make_jpg("folder/videofile.flv", "folder/test.jpg", "01");

"folder" will be relative to "wherever you are." The program you are referencing will execute from "where it is" - like so:

I am in /var/www/example.com/folder

I execute ffmpeg, which is in /usr/bin/ffmpeg

I pass my function "folder/videofile" so it's going to attempt operation on /usr/bin/ffmpeg/folder/videofile . . . . which doesn't exist.

Same is true of /test.jpg, you may think you're putting it at domain root, but that is from a browser and a url, even if it worked it would attempt to write the file to the server root, next to /var and /etc and all the other files - PHP won't have permissions to do that (and would be hella dangerous if it did.)

Not sure what your system path is, but you need to do something like this:

$root = '/var/www/mysite.com'; // Or $_SERVER['DOCUMENT_ROOT']
$videodir = 'folder';

make_jpg("$root/$videodir/videofile.flv", "$root/test.jpg", "01");

The second path - to write to - may not work, and is not a "great idea." This would mean the root of yoursite.com is writable, which is not all that safe. I'd direct it to a folder too that has appropriate permissions for PHP to write:

make_jpg("$root/$videodir/videofile.flv", "$root/thumbnails/test.jpg", "01");

Another thing you need to do - you're not using error trapping correctly, which will be a big help. Look at this:

if(!file_exists($input)) return false;

So if the file doesn't exist, it returns null, but you're not doing anything with the function call. Do one of these two:

$ok = make_jpg("$root/$videodir/videofile.flv", "$root/thumbnails/test.jpg", "01");

if (! $ok) { die("Whoops. File $root/$videodir/videofile.flv doesn't exist!"); }

or

if (! make_jpg("$root/$videodir/videofile.flv", "$root/thumbnails/test.jpg", "01")) {
die("Whoops. File $root/$videodir/videofile.flv doesn't exist!");
}

Both of those will help tell you what's wrong, at least. ffmpeg is a bugger to get to work at times, but get the paths right and error trap wherever you can for starters.