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程序设计有所帮助。

相关文章

简单了解将WordPress中的工具栏移到底部的小技巧

简单了解将WordPress中的工具栏移到底部的小技巧

从 WordPress 3.1 开始引入了工具栏的概念,当用户登录后在前台和后台的页面顶部会显示一个黑色的工具栏,通过工具栏可以快捷地新建文章、页面、媒体、用户,以及查看文章和评论等功能...

Smarty模板学习笔记之Smarty简介

1、简介 Smarty是一个使用PHP写出来的模板PHP模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML...

php基于登陆时间判断实现一天多次登录只积分一次功能示例

本文实例讲述了php基于登陆时间判断实现一天多次登录只积分一次功能。分享给大家供大家参考,具体如下: 在网上找了很多的案例,感觉都差不多,有的还比较的繁琐,就自己尝试了一下,如何实现这个...

WordPress判断用户是否登录的代码

is_user_logged_in() 说明 根据当前访问者是否登录返回布尔值true或false。 参数 该函数不接受任何参数。 用法 复制代码 代码如下: <?php if (...

PHP使用feof()函数读文件的方法

本文实例讲述了PHP使用feof()函数读文件的方法。分享给大家供大家参考。具体用法如下: feof应用于PHP 4, PHP 5 -用来测试文件指针是否到了文件结束的位置。 如果服务...