getID3 是一个 PHP 库,用于获取音频、视频等媒体文件的相关信息,包括文件格式、时长、比特率、采样率、编码等
1、首先从github克隆getid3的软件包
git clone https://github.com/JamesHeinrich/getID3.git
2、克隆下来,软件目录如下,已包含demos文件夹,有一些示例
3、用编辑器打开/getID3/demos/demo.simple.php
文件,核心代码如下
require_once('../getid3/getid3.php'); $getID3 = new getID3; $DirectoryToScan = '/opt/homebrew/var/www/getID3/audio'; $dir = opendir($DirectoryToScan); while (($file = readdir($dir)) !== false) { $FullFileName = realpath($DirectoryToScan.'/'.$file); if ((substr($file, 0, 1) != '.') && is_file($FullFileName)) { set_time_limit(30); $ThisFileInfo = $getID3->analyze($FullFileName); $getID3->CopyTagsToComments($ThisFileInfo); print_r($ThisFileInfo); } }
4、返回结果如下,截取部分数组
Array ( [GETID3_VERSION] => 1.9.22-202308100852 [filesize] => 2093241 [filepath] => /opt/homebrew/var/www/getID3/audio [filename] => bgm.m4a [filenamepath] => /opt/homebrew/var/www/getID3/audio/bgm.m4a [avdataoffset] => 30424 [avdataend] => 2093241 [fileformat] => mp4 [audio] => Array ( [dataformat] => mp4 [bitrate] => 96038 [codec] => ISO/IEC 14496-3 AAC [sample_rate] => 44100 [channels] => 2 [bits_per_sample] => 16 [lossless] => [channelmode] => stereo [compression_ratio] => 0.068054138321995 [streams] => Array ( [0] => Array ( [dataformat] => mp4 [bitrate] => 96038 [codec] => ISO/IEC 14496-3 AAC [sample_rate] => 44100 [channels] => 2 [bits_per_sample] => 16 [lossless] => [channelmode] => stereo [compression_ratio] => 0.068054138321995 ) ) )
5、不难看出,getid3返回了该音频绝大部分信息,如编码、时长等,视频方法类似
读取视频信息和音频集成方法类似