2011-01-31 66 views

回答

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/

+0

嗨aendrew,謝謝,但這個workes很好的音頻文件,但沒有像MP4視頻文件的正常工作,WMV等..請讓我知道從這除了任何其他解決方案。 – Chandan 2011-02-11 07:32:32

+1

使用不同的庫中,然後,或PHP擴展一樣的ffmpeg的PHP(http://ffmpeg-php.sourceforge.net/) – aendrew 2011-02-11 07:58:39

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>'; 
?> 
相關問題