|
Posted by Andy Jeffries on 08/16/06 11:55
On Tue, 15 Aug 2006 09:01:09 -0500, Karl Groves wrote:
To be honest, you should have everything you need above and my normal
response would be RTFM/try it and come back with some code even if it has
problems for us to fix.
However, I'm in a good mood so below is some code I've just knocked and
had a quick test with. It works fine. The locking code could do with a
usleep/while loop to keep trying in the event of a lock failure, but the
basics are surely close enough now for you.
It also gets round a bug where (on the movie I was testing it on at least)
the first frame image is always the start of the movie, but the second
frame is the real one.
#!/usr/bin/env php
<?php
function movie_get_length_in_seconds($movie_filename)
{
$cmd = "mplayer -identify -vo null -ao null -frames 1 ".
"$movie_filename 2>&1 | grep ID_LENGTH";
$output = exec($cmd);
list(,$length) = explode("=", $output); return $length;
}
function movie_take_screenshot($movie_filename, $time_in_seconds,
$image_filename)
{
if (mkdir(".movie.lck")) { // Try to get a lock
$cmd = "mplayer -ss $time_in_seconds -vo jpeg ".
"-ao null -frames 2 $movie_filename" 2>&1 | ".
"grep ID_LENGTH";
exec($cmd);
rename("00000002.jpg", "$image_filename");
unlink("00000001.jpg");
rmdir(".movie.lck"); // Release lock
return true;
}
return false; // Could not obtain lock
}
$movie_filename = "test.mpg";
$movie_length = movie_get_length_in_seconds($movie_filename);
$random_seconds = rand(0, floor($movie_length));
$was_image_created = movie_take_screenshot($movie_filename,
$random_seconds, "test_image.jpg");
?>
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos
Navigation:
[Reply to this message]
|