我使用以下代码获取视频的持续时间,
if (substr(php_uname(), 0, 7) == "Windows"){
$time = exec("{$ffmpeg} -i $path 2>&1 | findstr Duration");
echo $time;
exit;
$duration = explode(":", $time);
$seconds = ($duration[0] * 3600) + ($duration[1] * 60) + round($duration[2]);
$minutes = $seconds/60;
$real_minutes = floor($minutes);
$real_seconds = round(($minutes-$real_minutes)*60);
$length = $real_minutes.':'.$real_seconds;
}
$time
像Duration: 00:00:06.52, start: 0.000000, bitrate: 350 kb/s
一样显示输出,但$duration
只显示Array
$length
只显示所有视频的0:0。那么我如何获得视频长度请帮助我。
使用此函数获取持续时间。将文件的绝对路径作为参数传递:
public static function getDuration($filePath)
{
exec('ffmpeg -i'." '$filePath' 2>&1 | grep Duration | awk '{print $2}' | tr -d ,",$O,$S);
if(!empty($O[0]))
{
return $O[0];
}else
{
return false;
}
}
**确保您的$filePath是绝对
的如果您有ffmpeg.exe或安装了此功能,请使用此功能,因为它对我来说非常好用,我认为这对您来说应该是一样的
function getDuration($file){
$dur = shell_exec("ffmpeg -i ".$file." 2>&1");
if(preg_match("/: Invalid /", $dur)){
return false;
}
preg_match("/Duration: (.{2}):(.{2}):(.{2})/", $dur, $duration);
if(!isset($duration[1])){
return false;
}
return $duration[1].":".$duration[2].":".$duration[3];
}
演示
echo getDuration($input(; #input 是获取持续时间的文件路径