php获取视频大小代码怎么写,如何在PHP中获取视频时长,尺寸和大小?

如果您在服务器(http://www.mysql-apache-php.com/ffmpeg-install.htm)上安装了FFMPEG ,则可以使用命令“ -vstats ” 获取视频的属性并解析带有正则表达式的结果-如下例所示。然后,您需要PHP函数的filesize()来获取大小。

$ffmpeg_path = 'ffmpeg'; //or: /usr/bin/ffmpeg , or /usr/local/bin/ffmpeg - depends on your installation (type which ffmpeg into a console to find the install path)

$vid = 'PATH/TO/VIDEO'; //Replace here!

if (file_exists($vid)) {

$finfo = finfo_open(FILEINFO_MIME_TYPE);

$mime_type = finfo_file($finfo, $vid); // check mime type

finfo_close($finfo);

if (preg_match('/video\/*/', $mime_type)) {

$video_attributes = _get_video_attributes($vid, $ffmpeg_path);

print_r('Codec: ' . $video_attributes['codec'] . '
');

print_r('Dimension: ' . $video_attributes['width'] . ' x ' . $video_attributes['height'] . '
');

print_r('Duration: ' . $video_attributes['hours'] . ':' . $video_attributes['mins'] . ':'

. $video_attributes['secs'] . '.' . $video_attributes['ms'] . '
');

print_r('Size:  ' . _human_filesize(filesize($vid)));

} else {

print_r('File is not a video.');

}

} else {

print_r('File does not exist.');

}

function _get_video_attributes($video, $ffmpeg) {

$command = $ffmpeg . ' -i ' . $video . ' -vstats 2>&1';

$output = shell_exec($command);

$regex_sizes = "/Video: ([^,]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; // or : $regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; (code from @1owk3y)

if (preg_match($regex_sizes, $output, $regs)) {

$codec = $regs [1] ? $regs [1] : null;

$width = $regs [3] ? $regs [3] : null;

$height = $regs [4] ? $regs [4] : null;

}

$regex_duration = "/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})/";

if (preg_match($regex_duration, $output, $regs)) {

$hours = $regs [1] ? $regs [1] : null;

$mins = $regs [2] ? $regs [2] : null;

$secs = $regs [3] ? $regs [3] : null;

$ms = $regs [4] ? $regs [4] : null;

}

return array('codec' => $codec,

'width' => $width,

'height' => $height,

'hours' => $hours,

'mins' => $mins,

'secs' => $secs,

'ms' => $ms

);

}

function _human_filesize($bytes, $decimals = 2) {

$sz = 'BKMGTP';

$factor = floor((strlen($bytes) - 1) / 3);

return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值