This article mainly introduces the relevant information of PHP obtaining relevant information of audio files. It is very practical. Friends in need can refer to it. Down.
Project requirements: There is now a function to upload audio files. After uploading, PHP needs to obtain the relevant information of the audio file, such as: duration, etc. Since this file is placed on the purchased space, there is no such thing as ffmpeg extension to handle it, can PHP get this information?
The following is an implementation of reading and writing audio file header information using PHP that was used in the project before, mainly for WMA and MP3 formats, for reference.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
// AudioExif.class.php // 用PHP进行音频文件头部信息的读取与写入 // 目前只支持 WMA 和 MP3 两种格式, 只支持常用的几个头部信息 // // 写入信息支持: Title(名称), Artist(艺术家), Copyright(版权), Description (描述) // Year(年代), Genre (流派), AlbumTitle (专辑标题) // 其中 mp3 和 wma 略有不同, 具体返回的信息还可能更多, 但只有以上信息可以被写入 // mp3 还支持 Track (曲目编号写入) // 对于 MP3 文件支持 ID3v1也支持ID3v2, 读取时优先 v2, 写入时总是会写入v1, 必要时写入v2 // // 用法说明: (由于 wma 使用 Unicode 存取, 故还需要 mb_convert_encoding() 扩展 // 返回数据及写入数据均为 ANSI 编码, 即存什么就显示什么 (中文_GB2312) // // require ('AudioExif.class.php'); // $AE = new AudioExif; // $file = '/path/to/test.mp3'; // // 1. 检查文件是否完整 (only for wma, mp3始终返回 true) // // $AE->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, 或加以改进使它更加健壮, 请告诉我. // (You should be able to find reference materials on the Internet about the file format and structure of ID3 and 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 be 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, 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 .= " 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, $this->head_off, 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, 0, 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, LOCK_EX); fseek($this->fd, $roff, SEEK_SET); $buf = fread($this->fd, 4096);
// seek to start fseek($this->fd, $woff, SEEK_SET); fwrite($this->fd, $head, $nlen); $woff = $nlen;
// seek to woff & write the data do { $buf2 = $buf; $roff = 4096; if ($roff < $fsize) { fseek($this->fd, $roff, SEEK_SET); $buf = fread($this->fd, 4096); }
// save last buffer $len2 = strlen($buf2); fseek($this->fd, $woff, SEEK_SET); fwrite($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; }
// save to head_body $head_body .= pack('H32Va4', '3326b2758e66cf11a6d900aa0062ce6c', strlen($st_body1 . $st_body2) 24, ''); $head_body .= $st_body1 . $st_body2; $this->head['num'] ; } // ex not found? if (!$ex_found) { $inum = 0; $et_body = ''; foreach ($this->items2 as $k) { $nbuf = gbk_dbcs('WM/' . $k); $vbuf = (isset($pa[$k]) ? gbk_dbcs($pa[$k]) : ""); $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(" ", $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? function _init_header() { fseek($this->fd, 0, SEEK_SET); $buf = fread($this->fd, 30); if (strlen($buf) != 30) return false; $tmp = unpack('H32id/Vlen/Vlen2/Vnum/H4unused', $buf); if ($tmp['id'] != '3026b2758e66cf11a6d900aa0062ce6c') return false;
$this->head_buf = fread($this->fd, $tmp['len'] - 30); $this->head = $tmp; return true; }
// _get_head_frame() function _get_head_frame() { $buf = $this->_read_head_buf(24); if (strlen($buf) != 24) return false; $tmp = unpack('H32id/Vlen/H8unused', $buf); return $tmp; } }
// mp3 class (if not IDv2 then select IDv1) class _Mp3Exif extends _AudioExif { var $head1; var $genres = array('Blues','Classic Rock','Country','Dance','Disco','Funk','Grunge','Hip-Hop','Jazz','Metal','New Age','Oldies','Other','Pop','R&B','Rap','Reggae','Rock','Techno','Industrial','Alternative','Ska','Death Metal','Pranks','Soundtrack','Euro-Techno','Ambient','Trip-Hop','Vocal','Jazz Funk','Fusion','Trance','Classical','Instrumental','Acid','House','Game','Sound Clip','Gospel','Noise','AlternRock','Bass','Soul','Punk','Space','Meditative','Instrumental Pop','Instrumental Rock','Ethnic','Gothic','Darkwave','Techno-Industrial','Electronic','Pop-Folk','Eurodance','Dream','Southern Rock','Comedy','Cult','Gangsta','Top 40','Christian Rap','Pop/Funk','Jungle','Native American','Cabaret','New Wave','Psychadelic','Rave','Showtunes','Trailer','Lo-Fi','Tribal','Acid Punk','Acid Jazz','Polka','Retro','Musical','Rock & Roll','Hard Rock','Unknown');
// MP3 always return true function check_size($file) { return true; }
// get info function get_info($file) { if (!$this->_file_init($file)) return false; $ret = false; if ($this->_init_header()) { $ret = ($this->head ? $this->_get_v2_info() : $this->_get_v1_info()); $ret['meta'] = $this->_get_meta_info(); } $this->_file_deinit(); return $ret; }
// set info function set_info($file, $pa) { if (!$this->_file_init($file, true)) return false; if ($this->_init_header()) { // always save v1 info $this->_set_v1_info($pa); // set v2 first if need $this->_set_v2_info($pa); } $this->_file_deinit(); return true; }
// get the header information[v1 v2], call after file_init function _init_header() { $this->head1 = false; $this->head = false;
// try to get ID3v1 first fseek($this->fd, -128, SEEK_END); $buf = fread($this->fd, 128);
if (strlen($buf) == 128 && substr($buf, 0, 3) == 'TAG') { $tmp = unpack('a3id/a30Title/a30Artist/a30AlbumTitle/a4Year/a28Description/CReserved/CTrack/CGenre', $buf); $this->head1 = $tmp; }
// try to get ID3v2 fseek($this->fd, 0, SEEK_SET); $buf = fread($this->fd, 10); if (strlen($buf) == 10 && substr($buf, 0, 3) == 'ID3') { $tmp = unpack('a3id/Cver/Crev/Cflag/C4size', $buf); $tmp['size'] = ($tmp['size1']<<21)|($tmp['size2']<<14)|($tmp['size3']<<7)|$tmp['size4']; unset($tmp['size1'], $tmp['size2'], $tmp['size3'], $tmp['size4']);
$this->head = $tmp; $this->head_buf = fread($this->fd, $tmp['size']); } return ($this->head1 || $this->head); }
// get v1 info function _get_v1_info() { $ret = array(); $tmpa = array('Title', 'Artist', 'Copyright', 'Description', 'Year', 'AlbumTitle'); foreach ($tmpa as $tmp) { $ret[$tmp] = $this->head1[$tmp]; if ($pos = strpos($ret[$tmp], " |