我有一个 flv 文件上传到服务器。我想以以下格式显示其持续时间“分钟:秒”。有人可以帮我吗?
谢谢
还有一个 FFMPEG PHP 扩展,即。apt-get install php5-ffmpeg
然后
$movie = new ffmepg_movie("path/to/movie.flv"); $duration_in_seconds = $movie->getDuration();
这以前对我有用。该扩展适用于检索元数据和测试上传的文件是否为 FLV 等。
这是我抓取帧并从视频中生成图像的代码...
// get the duration and a random place within that
$cmd = "ffmpeg -i " . $videoPath . " 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$second = rand(1, ($total - 1));
}
exec($cmd);
// get the screenshot
exec("ffmpeg -i " . $videoPath . " -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg " . $imageOutput . " 2>&1");
$second 变量是介于 0 和总持续时间之间的随机数。第二个 exec() 是从选定的帧创建图像文件。
$imageOutput 是生成图像的绝对路径位置。例如:/home/ariawan/image-generated.jpg
我会使用getID3 PHP library
, 用普通的旧 PHP 编写,没有任何依赖项。
它不仅以秒为单位为您提供.flv
电影的持续时间,而且minute:seconds
已经将其转换为格式。这是 v. 1.7.9(最新稳定版本getid3
)的代码示例:
<?php
// getId3 library uses deprecated eregi_* functions
// which generate errors under PHP 5.3 - so I excluded them
error_reporting(E_ALL ^ E_DEPRECATED);
// just for debugging/sample
header('Content-Type: text/plain');
// include the getid3 base class in order to use the lib
require_once('./lib/getid3.php');
// path to your .flv file
$filename = './sample.flv';
$getID3 = new getID3();
$fileInfo = $getID3->analyze($filename);
// echoes something like 127.8743
print 'Playtime in seconds: ' . $fileInfo['playtime_seconds'];
print chr(10);
// echoes something like: 2:07
print 'Playtime in minute:seconds format: ' . $fileInfo['playtime_string'];
我正在使用 php 和 ffmpeg 来获取视频的持续时间。
$cmd = "ffmpeg -i " . $videoPath . " 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
}
exec($cmd);
print_r() 要查看的 $time 变量。确保您的机器上安装了ffmpeg ..希望这会有所帮助。