PHP to get information about audio files

Source: Internet
Author: User
Tags extend file size file upload flock fread ord strlen talb

This article mainly introduces the PHP to obtain audio files related information, very practical, the need for small partners can refer to.

Project requirements: now have an audio file upload function, after uploading PHP need to get the relevant information of this audio file, such as: time and so on, because this file is placed in the buy space, not like ffmpeg such an extension to deal with, then PHP can not get this information?

The following is a project used in the use of PHP for audio file header information of the implementation of the read and write operations, mainly for WMA and MP3 two of 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 4 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 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 1 19 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-1 80 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-2 41 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-3 08 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-3 69 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-4 30 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-4 91 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-5 58 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-6 19 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-6 80 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 <?php//AudioExif.class.php///php for audio file header information Read and write//currently only supports WMA and MP3 two formats, only support a few commonly used header information///write Information support: Title (name), Ar Tist (artist), Copyright (copyright), Description (description)//year (chronology), genre (genre), Albumtitle (album title)//where MP3 and WMA slightly different, the specific return of the information may also be more, However, only the above information can be written//MP3 also support Track (track number Write)//for MP3 file support ID3v1 also support ID3v2, read priority v2, write always write to v1, if necessary write V2////Usage instructions: (Because WMA uses Unicode access, it also requires mb_convert_encoding () extension//Return of data and write data are ANSI-encoded, that is, what is displayed (Chinese _gb2312)////require (' AudioExif.class.ph P '); $AE = new Audioexif; $file = '/path/to/test.mp3 '; 1. Check that the file is complete (only for WMA, MP3 always returns TRUE)////$AE->checksize ($file); 2. Read the information, return the value by the information of the array, the key name interpretation see above////Print_r ($AE->getinfo ($file)); 3. Write information, the second parameter is a hash array, the key-> value, supported see above, MP3 also supports Track//requires the first parameter of the file path can be written by this program//$pa = Array (' title ' => ' new title ', ' Albumtitle ' => ' new album name '); $AE->setinfo ($file, $PA); Other: This plugin spent a lot of time to find WMA and MP3 file format description documents and Web pages, I hope to be useful. In fact, there are many similar programs on the Internet, but for WMA is too little, only in the win platform through the m$/API to the exercise, and MP3 is rarely able to operate on the Unix/linux command line, so deliberately write this module////If bugs are found or submitted patch, or improved to make it more robust, please let me know. (References to file formats and structures on ID3 and WMA should be available on the Web)//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 ( {//No 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 FA Lse 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, 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 ' is 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 pointer function _read_head_buf ($len) {if ($len <= 0) ret Urn 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 () {$ord 1 = Ord (substr ($this->head_buf, $this->head_off, 1)); $ord 2 = Ord (substr ($this->head_buf, $this->head_off+1, 1)); $this->head_off + 2; Return ($ord 1 + ($ord 2<<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, $n Len); Flock ($this->FD, Lock_un); else {//longer, buffer required $stat = Fstat ($this->fd); $fsize = $stat [' size '];  /buf required (4096?) should Not Nlen-olen > 4096 $woff = 0; $roff = $olen;  //read-A-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 {$buf 2 = $buf $roff + + = 4096; if ($roff < $fsize) {fseek ($this->fd, $roff, Seek_set); $buf = Fread ($this->fd, 4096); }  //Save last buffer $len 2 = strlen ($buf 2); Fseek ($this->fd, $woff, Seek_set); Fwrite ($this->fd, $buf 2, $len 2); $woff + + $len 2; 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 $items 1 = Array (' Title ', ' Artist ', ' Copyright ', ' Description ', ' Reserved '); var $items 2 = Arra Y (' 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); $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-&G T;_file_init ($file, True)) return false; if (! $this->_init_header ()) {$this->_file_deinit (); return false;}  //parse the old header & Generate T He 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; Ten 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 _body 2) +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 = (Isse T ($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); 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 are string value (refer to Falg for other tags) $ret [$k] = DBCS_GBK ($VBUF);} }   Else {//Skip only if ($tmp [' Len '] >) $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); if (strlen ($BUF)!=) 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; if (strlen ($BUF)!=) re Turn false; $tmp = Unpack (' h32id/vlen/h8unused ', $buf); return $tmp; }  //MP3 class (if nOT IDv2 then select IDv1) class _mp3exif extends _audioexif {var $head 1; var $genres = array (' Blues ', ' Classic rock ', ' Coun Try ', ' 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 ', ' 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 $this need ($PA); } $this->_file_deinit (); return true;  //Get the header Information[v1+v2] file_init function _init_header () {$this->head1 = false; $th Is->head = false;  //try to get ID3v1-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/a30albumti Tle/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) = = && 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 ', ' Descripti On ', ' year ', ' Albumtitle '); foreach ($tmpa as $tmp) {$ret [$tmp] = $this->head1[$tmp]; if ($pos = Strpos ($ret [$tmp], "")) $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 ' => ') Arti St ', ' TIT2 ' => ' Title ', ' Trck ' => ' Track ', ' tcon ' => ' genre ', ' COMM ' => ' Description ', ' Tyer ' => ' year ', ' Talb ' => ' Albumtitle '); while (true) {$buf = $this->_read_head_buf, if (strlen ($BUF)!=), $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 ' just ' "", Skip if (substr ($tmp [' dat '], 0, 1) = = "") $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])? $this->genres[$g]: ' Unknown '); }   $ret [' id3v1 '] = ' no '; return $ret; }//Get meta info of MP3 function _get_meta_info () {//seek to the lead buf:0xff $off = 0; if ($this->head) $off = $ this->head[' size '] + 10; Fseek ($this->fd, $off, Seek_set); while (!feof ($this->fd)) {$skip = Ord (fread ($this->fd, 1)); if ($skip = = 0xff) break;} if ($skip!= 0xff) return F Alse; $buf = Fread ($this->fd, 3); if (strlen ($BUF)!= 3) return false; $tmp = Unpack (' C3 ', $buf); if ($tmp [1] & 0xf0)!= 0xf0) return false; Get the meta info $meta = array (); Get MPEG version $meta [' mpeg '] = ($tmp [1] & 0x08 1:2); $meta [' layer '] = ($tmp [1] & 0x04)? (($tmp [1] & 0x02) 1:2): (($tmp [1] & 0x02)? 3:0); $meta [' ePRO '] = ($tmp [1] & 0x01)? ' No ': ' Yes '; Bit rates $bit _rates = Array (1 => Array (1 => Array (0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0), 2 => Array (0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0), 3 => Array ( 0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0)), 2 => Array (1 => Array ( 0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0), 2 => Array ( 0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0), 3 => Array (0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0 ) ) ); $i = $meta [' mpeg ']; $j = $meta [' Layer ']; $k = ($tmp [2]>>4); $meta [' bitrate '] = $bit _rates[$i [$j] [$k]; Sample Rates < sampling rate > $sam _rates = Array (1=>array (44100,48000,32000,0), 2=>array (22050,24000,16000,0)); $meta [' samrate '] = $sam _rates[$i] [$k]; $meta ["padding"] = ($tmp [2] & 0x02)? ' On ': ' Off '; $meta ["private"] = ($tmp [2] & 0x01)? ' On ': ' Off '; Mode & Mode_ext $k = ($tmp [3]>>6); $channel _modes = Array (' Stereo ', ' joint Stereo ', ' dualChannel ', ' Single channel '); $meta [' mode '] = $channel _modes[$k]; $k = (($tmp [3]>>4) & 0x03); $extend _modes = Array (' MPG_MD_LR_LR ', ' Mpg_md_lr_i ', ' mpg_md_ms_lr ', ' mpg_md_ms_i '); $meta [' ext_mode '] = $extend _modes[$k]; $meta [' copyright '] = ($tmp [3] & 0x08)? ' Yes ': ' No '; $meta [' original '] = ($tmp [3] & 0x04)? ' Yes ': ' No '; $emphasis = Array (' None ', ' 50/15 microsecs ', ' rreserved ', ' CCITT J 17 '); $k = ($tmp [3] & 0x03); $meta [' emphasis '] = $emphasis [$k]; return $meta; }//Set V1 info function _set_v1_info ($PA) {//ID3v1 (simpled) $off = -128; if (!) ( $tmp = $this->head1)) {$off = 0; $tmp [' id '] = ' TAG '; $tmp [' Title '] = $tmp [' Artist '] = $tmp [' albumtitle '] = $tmp [' Year ' = $tmp [' Description '] = '; $tmp [' Reserved '] = $tmp [' Track '] = $tmp [' genre '] = 0;  //Basic Items $items = Array (' Title ', ' Artist ', ' Copyright ', ' Description ', ' Year ', ' Albumtitle '); foreach ($items as $k) {if (Isset ($pa [$k])) $tmp [$k] = $pa [$k];}//Genre index if (isset ($pa [' genre '])) {$g = 0; foreach ($this->genres as $gtmp) {if (!strcasecmp ($gtmp, $pa [' genre '])) break; $g + +;} $tmp [' genre '] = $g; if (Isset ($pa [' Track ']) $tmp [' Track '] = intval ($pa [' Track ']); Pack the Data $buf = Pack (' A3A30A30A30A4A28CCC ', $tmp [' id '], $tmp [' Title '], $tmp [' Artist '], $tmp [' Albumtitle '], $tmp [' Year '], $tmp [' Description '], 0, $tmp [' Track '], $tmp [' genre ']); Flock ($this->fd, LOCK_EX); Fseek ($this->fd, $off, seek_end); Fwrite ($this->fd, $BUF, 128); Flock ($this->fd, Lock_un); }//Set V2 info function _set_v2_info ($PA) {if (! $this->head) {//insert ID3 return;//No, not even/** $tmp = array (' id ' = > ' ID3 ', ' ver ' =>3, ' rev ' =>0, ' flag ' =>0); $tmp [' size '] =-10; +10 => 0 $this->head = $tmp; $this->head_buf = '; $this->head_off = 0; **/} $items = Array (' Tcop ' => ' Copyright ', ' TPE1 ' => ' Artist ', ' TIT2 ' => ' Title ', ' TRAC ' => ' Track ', ' Tcon ' => ') Genre ', ' COMM ' => ' Description ', ' Tyer ' => ' year ', ' Talb ' => ' albumtitle '); $head _body = '; while (TruE) {$buf = $this->_read_head_buf (d); if (strlen ($buf)!=) break; $tmp = Unpack (' A4fid/nsize/nflag ', $buf); if ($tm p[' size '] = = 0) break; $data = $this->_read_head_buf ($tmp [' size ']); if ($k = $items [$tmp [' FID]]] && isset ($pa [$k])) {//The data should prefix by "[Replace] $data =" ". $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). "" . $v; }//New length $new _len = strlen ($head _body) + 10; $old _len = $this->head[' size '] + 10; if ($new _len < $old _len) {$head _body. = Str_repeat ("", $old _len-$new _len); $new _len = $old _len; //Count the size1,2,3,4, no include the header//More abnormal algorithm ...: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 _lenn"; $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); }

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.