项目需求:现在有一个音频文件上传的功能,在上传后PHP需要获取这个音频文件的相关信息,例如:时长等,由于这个文件是放在买的空间上的,没有像ffmpeg这样的扩展来处理,那么PHP能不能获取到这些信息?
下面是之前在项目中用到的一个用PHP进行音频文件头部信息的读取与写入操作的实现,主要针对 WMA 和 MP3 两种格式,供参考。
<"\0\0" $str = substr($str, 0, -2); return mb_convert_encoding($str, 'GBK', 'UCS-2LE'); } // gb2312 => DBCS function gbk_dbcs($str) { $str = mb_convert_encoding($str, 'UCS-2LE', 'GBK'); $str .= "\0\0"; return $str; } // file exif class _AudioExif { var $fd; var $head; var $head_off; var $head_buf; // init the file handler function _file_init($fpath, $write = false) { $mode = ($write ""); $et_body .= pack('v', strlen($nbuf)) . $nbuf . pack('vv', 0, strlen($vbuf)) . $vbuf; $inum++; } $head_body .= pack('H32Va4v', '40a4d0d207e3d21197f000a0c95ea850', strlen($et_body)+26, '', $inum); $head_body .= $et_body; $this->head['num']++; } // after save $new_len = strlen($head_body) + 30; $old_len = $this->head['len']; if ($new_len < $old_len) { $head_body .= str_repeat("\0", $old_len - $new_len); $new_len = $old_len; } $tmp = $this->head; $head_buf = pack('H32VVVH4', $tmp['id'], $new_len, $tmp['len2'], $tmp['num'], $tmp['unused']); $head_buf .= $head_body; $this->_file_save($head_buf, $old_len, $new_len); // close the file & return $this->_file_deinit(); return true; } // get info function get_info($file) { $ret = array(); if (!$this->_file_init($file)) return false; if (!$this->_init_header()) { $this->_file_deinit(); return false; } // get the data from head_buf $head_num = $this->head['num']; // num of head_frame while (($tmp = $this->_get_head_frame()) && $head_num > 0) { $head_num--; if ($tmp['id'] == '3326b2758e66cf11a6d900aa0062ce6c') { // Standard Info $lenx = unpack('v*', $this->_read_head_buf(10)); for ($i = 1; $i <= count($this->items1); $i++) { $k = $this->items1[$i-1]; $ret[$k] = dbcs_gbk($this->_read_head_buf($lenx[$i])); } } else if ($tmp['id'] == '40a4d0d207e3d21197f000a0c95ea850') { // Extended Info $inum = $this->_read_head_short(); $tmp['len'] -= 26; while ($inum > 0 && $tmp['len'] > 0) { // attribute name $nlen = $this->_read_head_short(); $nbuf = $this->_read_head_buf($nlen); // the flag & value length $flag = $this->_read_head_short(); $vlen = $this->_read_head_short(); $vbuf = $this->_read_head_buf($vlen); // update the XX $tmp['len'] -= (6 + $nlen + $vlen); $inum--; $name = dbcs_gbk($nbuf); $k = substr($name, 3); if (in_array($k, $this->items2)) { // all is string value (refer to falg for other tags) $ret[$k] = dbcs_gbk($vbuf); } } } else { // skip only if ($tmp['len'] > 24) $this->head_off += ($tmp['len'] - 24); } } $this->_file_deinit(); return $ret; } // get the header"\0")) $ret[$tmp] = substr($ret[$tmp], 0, $pos); } // count the Genre, [Track] if ($this->head1['Reserved'] == 0) $ret['Track'] = $this->head1['Track']; else $ret['Description'] .= chr($ret['Reserved']) . chr($ret['Track']); // Genre_idx $g = $this->head1['Genre']; if (!isset($this->genres[$g])) $ret['Genre'] = 'Unknown'; else $ret['Genre'] = $this->genres[$g]; // return the value $ret['ID3v1'] = 'yes'; return $ret; } // get v2 info function _get_v2_info() { $ret = array(); $items = array( 'TCOP'=>'Copyright', 'TPE1'=>'Artist', 'TIT2'=>'Title', 'TRCK'=> 'Track', 'TCON'=>'Genre', 'COMM'=>'Description', 'TYER'=>'Year', 'TALB'=>'AlbumTitle'); while (true) { $buf = $this->_read_head_buf(10); if (strlen($buf) != 10) break; $tmp = unpack('a4fid/Nsize/nflag', $buf); if ($tmp['size'] == 0) break; $tmp['dat'] = $this->_read_head_buf($tmp['size']); // 0x6000 (11000000 00000000) if ($tmp['flag'] & 0x6000) continue; // mapping the data if ($k = $items[$tmp['fid']]) { // If first char is "\0", just skip if (substr($tmp['dat'], 0, 1) == "\0") $tmp['dat'] = substr($tmp['dat'], 1); $ret[$k] = $tmp['dat']; } } // reset the genre if ($g = $ret['Genre']) { if (substr($g,0,1) == '(' && substr($g,-1,1) == ')') $g = substr($g, 1, -1); if (is_numeric($g)) { $g = intval($g); $ret['Genre'] = (isset($this->genres[$g]) "padding"] = ($tmp[2] & 0x02) "private"] = ($tmp[2] & 0x01) "\0" [replace] $data = "\0" . $pa[$k]; unset($pa[$k]); } $head_body .= pack('a4Nn', $tmp['fid'], strlen($data), $tmp['flag']) . $data; } // reverse the items & set the new tags $items = array_flip($items); foreach ($pa as $k => $v) { if ($fid = $items[$k]) { $head_body .= pack('a4Nn', $fid, strlen($v) + 1, 0) . "\0" . $v; } } // new length $new_len = strlen($head_body) + 10; $old_len = $this->head['size'] + 10; if ($new_len < $old_len) { $head_body .= str_repeat("\0", $old_len - $new_len); $new_len = $old_len; } // count the size1,2,3,4, no include the header // 较为变态的算法... :p (28bytes integer) $size = array(); $nlen = $new_len - 10; for ($i = 4; $i > 0; $i--) { $size[$i] = ($nlen & 0x7f); $nlen = 7; } $tmp = $this->head; //echo "old_len : $old_len new_len: $new_len\n"; $head_buf = pack('a3CCCCCCC', $tmp['id'], $tmp['ver'], $tmp['rev'], $tmp['flag'], $size[1], $size[2], $size[3], $size[4]); $head_buf .= $head_body; $this->_file_save($head_buf, $old_len, $new_len); }
以上所述就是本文的全部内容了,希望大家能够喜欢。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]