PHP从FLV文件获取视频预览图的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP从FLV文件获取视频预览图的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
<?php
// references http://www.longtailvideo.com/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程序设计有所帮助。

相关文章

PHP框架Swoole定时器Timer特性分析

PHP框架Swoole定时器Timer特性分析

本文所述PHP框架Swoole定时器Timer特性分析具有很好的实用价值,值得PHP程序开发人员学习借鉴。具体分析如下: 首先,在各种业务型系统中,往往需要服务器在后台扫描相关数据,触发...

PHP中Cookie的使用详解(简单易懂)

PHP中Cookie的使用---添加/更新/删除/获取Cookie 及 自动填写该用户的用户名和密码和判断是否第一次登陆 什么是cookie 服务器在客户端保存用户的信息,比如登录名,密...

PHP中break及continue两个流程控制指令区别分析

以下举例说明break 用来跳出目前执行的循环,并不再继续执行循环了。 复制代码 代码如下: <?php $i = 0; while ($i < 7) { if ($arr[...

PHP中overload与override的区别

override(重写,覆盖) 1、方法名、参数、返回值相同。 2、子类方法不能缩小父类方法的访问权限。 3、子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。 4、存...

php发送http请求的常用方法分析

本文实例讲述了php发送http请求的常用方法。分享给大家供大家参考,具体如下: http请求有get,post。 php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。...