DDR爱好者之家 Design By 杰米

项目需求:现在有一个音频文件上传的功能,在上传后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);
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米