加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP获取音频文件的相关信息

发布时间:2020-05-23 21:47:44 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了PHP获取音频文件的相关信息的相关资料,非常的实用,有需要的小伙伴可以参考下。

项目需求:现在有一个音频文件上传的功能,在上传后PHP需要获取这个音频文件的相关信息,例如:时长等,由于这个文件是放在买的空间上的,没有像ffmpeg这样的扩展来处理,那么PHP能不能获取到这些信息?

下面是之前在项目中用到的一个用PHP进行音频文件头部信息的读取与写入操作的实现,主要针对 WMA 和 MP3 两种格式,供参考。

CheckSize($file); // // 2. 读取信息,返回值由信息组成的数组,键名解释参见上方 // // print_r($AE->GetInfo($file)); // // 3. 写入信息,第二参数是一个哈希数组,键->值,支持的参见上方的,mp3也支持 Track // 要求第一参数的文件路径可由本程序写入 // $pa = array('Title' => '新标题','AlbumTitle' => '新的专辑名称'); // $AE->SetInfo($file,$pa); // // 其它: 该插件花了不少时间搜集查找 wma及mp3 的文件格式说明文档与网页,希望对大家有用. // 其实网上已经有不少类似的程序,但对 wma 实在太少了,只能在 win 平台下通过 M$ 的 // API 来操作,而 MP3 也很少有可以在 unix/linux 命令行操作的,所以特意写了这个模块 // // 如果发现 bug 或提交 patch,或加以改进使它更加健壮,请告诉我. // (关于 ID3和Wma的文件格式及结构 在网上应该都可以找到参考资料) // if (!extension_loaded('mbstring')){ trigger_error('PHP Extension module `mbstring` is required for AudioExif',E_USER_WARNING); return true; } // the Main Class class AudioExif{ // public vars var $_wma = false; var $_mp3 = false; // Construct function AudioExif() { // nothing to do } // check the filesize function CheckSize($file) { $handler = &$this->_get_handler($file); if (!$handler) return false; return $handler->check_size($file); } // get the infomations function GetInfo($file) { $handler = &$this->_get_handler($file); if (!$handler) return false; return $handler->get_info($file); } // write the infomations function SetInfo($file,$pa) { if (!is_writable($file)) { trigger_error('AudioExif: file `' . $file . '` can not been overwritten',E_USER_WARNING); return false; } $handler = &$this->_get_handler($file); if (!$handler) return false; return $handler->set_info($file,$pa); } // private methods function &_get_handler($file) { $ext = strtolower(strrchr($file,'.')); $ret = false; if ($ext == '.mp3') { // MP3 $ret = &$this->_mp3; if (!$ret) $ret = new _Mp3Exif();

}
else if ($ext == '.wma')
{ // wma
$ret = &$this->_wma;
if (!$ret) $ret = new _WmaExif();
}
else
{ // unknown
trigger_error('AudioExif not supported ' . $ext . ' file.',E_USER_WARNING);
}
return $ret;
}
}

// DBCS => gb2312
function dbcs_gbk($str)
{
// strip the last ""
$str = substr($str,-2);
return mb_convert_encoding($str,'GBK','UCS-2LE');
}

// gb2312 => DBCS
function gbk_dbcs($str)
{
$str = mb_convert_encoding($str,'UCS-2LE','GBK');
$str .= "";
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 ? 'rb+' : 'rb');
$this->fd = @fopen($fpath,$mode);
if (!$this->fd)
{
trigger_error('AudioExif: ' . $fpath . ' can not be opened with mode ' . $mode . '',E_USER_WARNING);
return false;
}
$this->head = false;
$this->head_off = 0;
$this->head_buf = '';
return true;
}

// read buffer from the head_buf & move the off pointer
function _read_head_buf($len)
{
if ($len <= 0) return NULL;
$buf = substr($this->head_buf,$this->head_off,$len);
$this->head_off += strlen($buf);
return $buf;
}

// read one short value
function _read_head_short()
{
$ord1 = ord(substr($this->head_buf,1));
$ord2 = ord(substr($this->head_buf,$this->head_off+1,1));
$this->head_off += 2;
return ($ord1 + ($ord2<<8));
}

// save the file head
function _file_save($head,$olen,$nlen = 0)
{
if ($nlen == 0) $nlen = strlen($head);
if ($nlen == $olen)
{
// shorter
flock($this->fd,LOCK_EX);
fseek($this->fd,SEEK_SET);
fwrite($this->fd,$head,$nlen);
flock($this->fd,LOCK_UN);
}
else
{
// longer,buffer required
$stat = fstat($this->fd);
$fsize = $stat['size'];

// buf required (4096?) 应该不会 nlen - olen > 4096 吧
$woff = 0;
$roff = $olen;

// read first buffer
flock($this->fd,$roff,SEEK_SET);
$buf = fread($this->fd,4096);

// seek to start
fseek($this->fd,$woff,$nlen);
$woff += $nlen;

// seek to woff & write the data
do
{
$buf2 = $buf;
$roff += 4096;
if ($roff < $fsize)
{
fseek($this->fd,4096);
}

// save last buffer
$len2 = strlen($buf2);
fseek($this->fd,$buf2,$len2);
$woff += $len2;
}
while ($roff < $fsize);
ftruncate($this->fd,$woff);
flock($this->fd,LOCK_UN);
}
}

// close the file
function _file_deinit()
{
if ($this->fd)
{
fclose($this->fd);
$this->fd = false;
}
}
}

// wma class
class _WmaExif extends _AudioExif
{
var $items1 = array('Title','Artist','Copyright','Description','Reserved');
var $items2 = array('Year','Genre','AlbumTitle');

// check file size (length) maybe invalid file
function check_size($file)
{
$ret = false;
if (!$this->_file_init($file)) return true;
if ($this->_init_header())
{
$buf = fread($this->fd,24);
$tmp = unpack('H32id/Vlen/H8unused',$buf);
if ($tmp['id'] == '3626b2758e66cf11a6d900aa0062ce6c')
{
$stat = fstat($this->fd);
$ret = ($stat['size'] == ($this->head['len'] + $tmp['len']));
}
}
$this->_file_deinit();
return $ret;
}

// set info (save the infos)
function set_info($file,$pa)
{
// check the pa
settype($pa,'array');
if (!$this->_file_init($file,true)) return false;
if (!$this->_init_header())
{
$this->_file_deinit();
return false;
}

// parse the old header & generate the new header
$head_body = '';
$st_found = $ex_found = false;
$head_num = $this->head['num'];
while (($tmp = $this->_get_head_frame()) && ($head_num > 0))
{
$head_num--;
if ($tmp['id'] == '3326b2758e66cf11a6d900aa0062ce6c')
{ // Standard Info
// 1-4
$st_found = true;
$st_body1 = $st_body2 = '';
$lenx = unpack('v5',$this->_read_head_buf(10));
$tmp['len'] -= 34; // 10 + 24
for ($i = 0; $i < count($this->items1); $i++)
{
$l = $lenx[$i+1];
$k = $this->items1[$i];
$tmp['len'] -= $l;

$data = $this->_read_head_buf($l);
if (isset($pa[$k])) $data = gbk_dbcs($pa[$k]);

$st_body2 .= $data;
$st_body1 .= pack('v',strlen($data));
}
// left length
if ($tmp['len'] > 0) $st_body2 .= $this->_read_head_buf($tmp['len']);

// save to head_body
$head_body .= pack('H32VH8',$tmp['id'],strlen($st_body1 . $st_body2)+24,$tmp['unused']);
$head_body .= $st_body1 . $st_body2;

$st_body2 .= $data;
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读