PHP從FLV文件獲取視頻預(yù)覽圖的方法
更新時(shí)間:2015年03月12日 15:02:25 作者:鑒客
這篇文章主要介紹了PHP從FLV文件獲取視頻預(yù)覽圖的方法,實(shí)例分析了php操作flv文件獲取截圖的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了PHP從FLV文件獲取視頻預(yù)覽圖的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
<?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");
}
?>
// 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");
}
?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- PHP編程獲取音頻文件時(shí)長(zhǎng)的方法【基于getid3類(lèi)】
- 解析用PHP讀寫(xiě)音頻文件信息的詳解(支持WMA和MP3)
- PHP獲取音頻文件的相關(guān)信息
- php利用ffmpeg提取視頻中音頻與視頻畫(huà)面的方法詳解
- Thinkphp5框架實(shí)現(xiàn)圖片、音頻和視頻文件的上傳功能詳解
- PHP 獲取視頻時(shí)長(zhǎng)的實(shí)例代碼
- PHP基于ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻,截圖及生成縮略圖的方法
- php使用FFmpeg接口獲取視頻的播放時(shí)長(zhǎng)、碼率、縮略圖以及創(chuàng)建時(shí)間
- php截取視頻指定幀為圖片
- PHP getID3類(lèi)的使用方法學(xué)習(xí)筆記【附getID3源碼下載】
相關(guān)文章
PHP實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建XML文檔的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建XML文檔的方法,結(jié)合實(shí)例形式分析了php針對(duì)xml格式數(shù)據(jù)的構(gòu)建及文件讀寫(xiě)相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
淺談PHP的排列組合(如輸入a,b,c 輸出他們的全部組合)
下面小編就為大家?guī)?lái)一篇淺談PHP的排列組合(如輸入a,b,c 輸出他們的全部組合)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
PHP常用函數(shù)之根據(jù)生日計(jì)算年齡功能示例
這篇文章主要介紹了PHP常用函數(shù)之根據(jù)生日計(jì)算年齡功能,結(jié)合實(shí)例形式分析了php日期相關(guān)轉(zhuǎn)換與計(jì)算操作技巧,需要的朋友可以參考下2019-10-10

