This example describes how PHP obtains a video preview from an FLV file. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
<?php
References http://www.longtailvideo.com/support/forum/modules/12661/external-php-with-ffmpeg-using-readfile-
Generate a preview image from the FLV file On-the-fly, or to save
Call With:ffmpeg_image.php?file=video.flv&time=00:00:05&browser=true
Call With:ffmpeg_image.php?file=video.flv&percent=75.3&browser=true
No time defaults to "00:00:01" (one second) and no browser defaults to "true"
$videofile = (isset ($_get[' file '))? Strval ($_get[' file ']): ' video.flv ';
$image = substr ($videofile, 0, strlen ($videofile)-4);
$time = (isset ($_get[' time '))? Strval ($_get[' time ']): ' 00:00:01 ';
Debug ("File:", $videofile);
Debug ("Image:", $image);
Debug ("Time:", $time);
Check time format
if (!preg_match ('/\d\d:\d\d:\d\d/', $time))
{
$time = "00:00:00";
}
if (Isset ($_get[' percent '))
{
$percent = $_get[' percent '];
Debug ("Percent:", $percent);
Ob_start ();
EXEC ("/usr/bin/ffmpeg-i \" ". $videofile. "\" 2>&1 ");
$duration = Ob_get_contents ();
Ob_end_clean ();
Debug ("Duration:", $duration);
Preg_match ('/duration: (. *?),/', $duration, $matches);
$duration = $matches [1];
Debug ("Duration:", $duration);
$duration _array = Split (': ', $duration);
$duration = $duration _array[0] * 3600 + $duration _array[1] * + $duration _array[2];
$time = $duration * $percent/100;
Debug ("Time:", $time);
$time = intval ($time/3600). ":" . Intval (($time-(Intval ($time/3600) *3600))/60). ":" . sprintf ("%01.3f", ($time-(Intval ($time/60) *60));
Debug ("Time:", $time);
}
$browser = (isset ($_get[' browser '))? Strval ($_get[' browser ']): ' true ';
Debug ("Browser:", $browser);
if ($browser = = "true")
{
Header (' content-type:image/png ');
EXEC ("/usr/bin/ffmpeg-vcodec png-i \" ". $videofile. "\"-ss ". $time. "-vframes 1-f Image2-");
Header (' Content-type:image/jpeg ');
EXEC ("/usr/bin/ffmpeg-vcodec mjpeg-i \" ". $videofile. "\"-ss ". $time. "-vframes 1-f Image2-");
}
Else
{
EXEC ("/usr/bin/ffmpeg-vcodec png-i \" ". $videofile. "\"-ss ". $time. "-vframes 1-f image2" ". $image. "\"%d.png ");
EXEC ("/usr/bin/ffmpeg-vcodec mjpeg-i \" ". $videofile. "\"-ss ". $time. "-vframes 1-f image2" ". $image. "\"%d.jpg ");
}
?>
I hope this article will help you with your PHP program design.