我想知道如何獲取PHP上傳的視頻文件的持續時間,尺寸和大小。該文件可以是任何視頻格式。如何在PHP中獲取視頻的持續時間,維度和大小?
33
A
回答
55
getID3支持的視頻格式。請參閱:http://getid3.sourceforge.net/
編輯:那麼,在代碼格式,那會是這樣的:
include_once('pathto/getid3.php');
$getID3 = new getID3;
$file = $getID3->analyze($filename);
echo("Duration: ".$file['playtime_string'].
"/Dimensions: ".$file['video']['resolution_x']." wide by ".$file['video']['resolution_y']." tall".
"/Filesize: ".$file['filesize']." bytes<br />");
注:必須包括getID3類之前,這將工作!看到上面的鏈接。
編輯:如果您需要修改服務器上的PHP安裝的能力,一個PHP擴展用於此目的是ffmpeg的,PHP。請參閱:http://ffmpeg-php.sourceforge.net/
15
如果您已安裝到服務器(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'] . '<br/>');
print_r('Dimension: ' . $video_attributes['width'] . ' x ' . $video_attributes['height'] . ' <br/>');
print_r('Duration: ' . $video_attributes['hours'] . ':' . $video_attributes['mins'] . ':'
. $video_attributes['secs'] . '.' . $video_attributes['ms'] . '<br/>');
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];
}
4
如果你使用WordPress,你可以只使用WordPress的建設功能與視頻ID提供wp_get_attachment_metadata($videoID):
wp_get_attachment_metadata($videoID);
幫了我很多。 這就是爲什麼我張貼了,雖然有人問在2011年
0
https://github.com/JamesHeinrich/getID3 下載getid3壓縮比的項目文件夾中只有getid3命名的文件夾複製粘貼,並使用它,如下所示...
<?php
require_once('/fire/scripts/lib/getid3/getid3/getid3.php');
$getID3 = new getID3();
$filename="/fire/My Documents/video/ferrari1.mpg";
$fileinfo = $getID3->analyze($filename);
$width=$fileinfo['video']['resolution_x'];
$height=$fileinfo['video']['resolution_y'];
echo $fileinfo['video']['resolution_x']. 'x'. $fileinfo['video']['resolution_y'];
echo '<pre>';print_r($fileinfo);echo '</pre>';
?>
相關問題
- 1. 獲取AVFoundation視頻的持續時間
- 2. 獲取視頻的持續時間
- 3. 如何在html5視頻播放器中獲取視頻的持續時間..?
- 4. 如何獲取上傳的視頻的持續時間?
- 5. 獲取上傳的視頻在mvc中的持續時間
- 6. 如何使用API獲取視頻的持續時間Metacafe
- 7. 如何獲取視頻的持續時間?
- 8. 如何從MediaStore獲取視頻的持續時間?
- 9. 在ffmpeg中獲取音頻的持續時間在php
- 10. 如何獲取在android中捕獲的視頻的持續時間?
- 11. 如何獲取視頻全部持續時間和當前播放時間?
- 12. 如何從Graph API獲取視頻持續時間?
- 13. 如何使用simpeXML從youtube API獲取視頻持續時間?
- 14. 如何從Amazon Elastic Transcoder獲取視頻持續時間?
- 15. 如何提取音頻/視頻文件的持續時間?
- 16. 如何顯示視頻的總時間和持續時間
- 17. Python和OpenCV - 獲取視頻在某些時間點的持續時間
- 18. HTML5視頻持續時間
- 19. 如何在iPhone中存儲捕獲的視頻的時間段(持續時間)?
- 20. 獲取MPMoviePlayer中視頻的總播放持續時間
- 21. 使用ffmpeg獲取python中的視頻持續時間
- 22. iOS:獲取視頻持續時間和縮略圖而不播放視頻
- 23. Android:獲取支持視頻大小
- 24. 在Express.js視頻的持續時間:NaN
- 25. 如何在C#asp.net中使用FFMPEG獲取視頻持續時間
- 26. 檢索從kbps和大小的音頻持續時間
- 27. 獲得視頻持續時間與ffmpeg的,PHP函數
- 28. 如何在使用AVCaptureVideoPreviewLayer時保持捕獲的視頻大小
- 29. 如何計算使用php的mp4視頻的持續時間/長度?
- 30. 如何獲取Yii2中視頻文件輸入的時間,大小和尺寸?
嗨aendrew,謝謝,但這個workes很好的音頻文件,但沒有像MP4視頻文件的正常工作,WMV等..請讓我知道從這除了任何其他解決方案。 – Chandan 2011-02-11 07:32:32
使用不同的庫中,然後,或PHP擴展一樣的ffmpeg的PHP(http://ffmpeg-php.sourceforge.net/) – aendrew 2011-02-11 07:58:39