PHP从FLV文件获取视频预览图的方法
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了PHP从FLV文件获取视频预览图的方法,实例分析了php操作flv文件获取截图的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了PHP从FLV文件获取视频预览图的方法。分享给大家供大家参考。具体实现方法如下
代码如下:
<?php
// references http://.longtailvideo./support/forum/Modules/12661/External-PHP-with-FFmpeg-using-readfile-
// generate a preview image from an 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), 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] 60 + $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");
}
?>
// references http://.longtailvideo./support/forum/Modules/12661/External-PHP-with-FFmpeg-using-readfile-
// generate a preview image from an 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), 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] 60 + $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");
}
?>
希望本文所述对大家的php程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程