|
ID3 FunctionsThese functions let you read and manipulate ID3 tags. ID3 tags are used in MP3 files to store title of the song, as well as information about the artist, album, genre, year and track number. Since version 0.2 it is also possible to extract text frames from ID3 v2.2+ tags. id3 is part of PECL and can be installed using the PEAR installer. To compile PHP with id3 support, download the sourcecode, put it in php-src/ext/id3 and compile PHP using --enable-id3. Most of the id3 functions either let you specify or return a tag version. In order to specify the version please use on of these constants.
Table of Contents
Code Examples / Notes » ref.id3jbwalker
Windows XP users may be having trouble with routines provided here and may (as above) only have access to "read" classes. The following very simplified "write" can be used for replacing Windows Media Player file tags, and with care and adjustments, can be used more generally. define(_Title,"TIT2"); define(_Artist,"TPE1"); define(_Group,"TPE2"); define(_Album,"TALB"); define(_Genre,"TCON"); define(_TrackNo,"TRCK"); define(_Year,"TYER"); $frames = array(_Album=>"The Ultimate Experience", _TrackNo=>"1", _Title=>"All along the watchtower", _Artist=>"Jimi Hendrix", _Group=>"", _Year=>"19xx", _Genre=>"Rock"); #.......................................... # WRITE ID3 TAGS (Write MP3 [v1, v2] #.......................................... function writeTags($mp3) { $fl = file_get_contents($mp3); $Header = substr($fl,0,10); $tagLen = calcDecTagLen(substr($Header,6,4),$tagLen); $music = substr($fl,$tagLen+10,-128); # Can use input Header for output but you may # wish to change the output filename for testing file_put_contents($mp3,mkV2Tag($Header,$tagLen).$music.mkV1Tag()); } # Create the V2 tag function mkV2Tag($Hdr,$tagLen) { Global $frames; $null = chr(0); $nl3 = $null.$null.$null; # 0 bytes for flags and encoding $out = ""; foreach($frames as $ky=>$val) { $n=strlen($val)+1; $out.= $ky.mkFrmLen($n).$nl3.$val; } return $Hdr.str_pad($out,$tagLen,$null); } # Calculate Tag Length from bytes 6-10 of existing header function calcDecTagLen($word) { $m = 1; $int = 0; for ($i=strlen($word)-1;$i>-1;$i--) { $int +=$m*ord($word[$i]); $m=$m*128; } return $int; } # Make the 4 byte frame length value for the V2tag function mkFrmLen($int) { $hx = ""; while ($int>0) { $n = $int % 256; $hx = chr($n).$hx; $int=floor($int/256); } return str_pad($hx,4,chr(0),STR_PAD_LEFT); } # Create the 128 byte V1 tag function mkV1Tag() { Global $frames; $tagOut = "TAG". adj($frames[_Title]). adj($frames[_Artist]). adj($frames[_Album]). str_pad($frames[_Year],4). str_pad(" ",29," "). chr($frames[_TrackNo]). chr($n); return $tagOut; } # Pad the header to 30 characters function adj($str) { return substr(str_pad($str,30,chr(0)),0,30); } # This is a simple example for an mp3 in current folder writeTags("01-Cognac Blues.mp3"); regindk
Using the ID3 extension you might be interested in some more functionality for working with MP3-files that is missing in the PHP C-modules. Such as extracting for instance the first 10 seconds of a song, merging MP3 files, calculating the exact length of the MP3 file. The following class in pure PHP is available for that: http://www.sourcerally.net/Scripts/20-PHP-MP3-Class wmd
There are several highly developed id3 reader classes written in php that include id3v2 support, and support for other file formats (not just mpeg & id3). I recommend http://getid3.sourceforge.net/ as well as the id3 reader class integrated in the Zina is not Andromeda project (http://pancake.org/zina.html)
jbwalker
Oops! The code below was inelegant to begin with and stripped down further to limit space taken up here, contains one serious bug: the assumption that the input tag header (first 10 bytes) will do for output. This likely works fine if your own finely tuned tags take up less real estate than MS's bloated headers but not if you read mp3s from a product that sets a tag length less than the length of your own header! A simple fix for this is to insert the code: $tagLen = 1024; # or whatever you like >your actual $Header = substr($Header,0,6).setHexTagLen($tagLen); after the line '$music = etc. The setHexTagLen can be figured out from the calcDecTagLen but here's some sample code. function setHexTagLen($int) { $n = pow(128,3); $intVar = $int; $m = ""; for ($i=0;$i<4;$i++) { $m .= chr(floor($intVar/$n)); $intVar = $intVar % $n; $n=$n/128; } return $m; } |
Change Language.NET Functions Apache-specific Functions Alternative PHP Cache Advanced PHP debugger Array Functions Aspell functions [deprecated] BBCode Functions BCMath Arbitrary Precision Mathematics Functions PHP bytecode Compiler Bzip2 Compression Functions Calendar Functions CCVS API Functions [deprecated] Class/Object Functions Classkit Functions ClibPDF Functions [deprecated] COM and .Net (Windows) Crack Functions Character Type Functions CURL Cybercash Payment Functions Credit Mutuel CyberMUT functions Cyrus IMAP administration Functions Date and Time Functions DB++ Functions Database (dbm-style) Abstraction Layer Functions dBase Functions DBM Functions [deprecated] dbx Functions Direct IO Functions Directory Functions DOM Functions DOM XML Functions enchant Functions Error Handling and Logging Functions Exif Functions Expect Functions File Alteration Monitor Functions Forms Data Format Functions Fileinfo Functions filePro Functions Filesystem Functions Filter Functions Firebird/InterBase Functions Firebird/Interbase Functions (PDO_FIREBIRD) FriBiDi Functions FrontBase Functions FTP Functions Function Handling Functions GeoIP Functions Gettext Functions GMP Functions gnupg Functions Net_Gopher Haru PDF Functions hash Functions HTTP Hyperwave Functions Hyperwave API Functions i18n Functions IBM Functions (PDO_IBM) IBM DB2 iconv Functions ID3 Functions IIS Administration Functions Image Functions Imagick Image Library IMAP Informix Functions Informix Functions (PDO_INFORMIX) Ingres II Functions IRC Gateway Functions PHP / Java Integration JSON Functions KADM5 LDAP Functions libxml Functions Lotus Notes Functions LZF Functions Mail Functions Mailparse Functions Mathematical Functions MaxDB PHP Extension MCAL Functions Mcrypt Encryption Functions MCVE (Monetra) Payment Functions Memcache Functions Mhash Functions Mimetype Functions Ming functions for Flash Miscellaneous Functions mnoGoSearch Functions Microsoft SQL Server Functions Microsoft SQL Server and Sybase Functions (PDO_DBLIB) Mohawk Software Session Handler Functions mSQL Functions Multibyte String Functions muscat Functions MySQL Functions MySQL Functions (PDO_MYSQL) MySQL Improved Extension Ncurses Terminal Screen Control Functions Network Functions Newt Functions NSAPI-specific Functions Object Aggregation/Composition Functions Object property and method call overloading Oracle Functions ODBC Functions (Unified) ODBC and DB2 Functions (PDO_ODBC) oggvorbis OpenAL Audio Bindings OpenSSL Functions Oracle Functions [deprecated] Oracle Functions (PDO_OCI) Output Control Functions Ovrimos SQL Functions Paradox File Access Parsekit Functions Process Control Functions Regular Expression Functions (Perl-Compatible) PDF Functions PDO Functions Phar archive stream and classes PHP Options&Information POSIX Functions Regular Expression Functions (POSIX Extended) PostgreSQL Functions PostgreSQL Functions (PDO_PGSQL) Printer Functions Program Execution Functions PostScript document creation Pspell Functions qtdom Functions Radius Rar Functions GNU Readline GNU Recode Functions RPM Header Reading Functions runkit Functions SAM - Simple Asynchronous Messaging Satellite CORBA client extension [deprecated] SCA Functions SDO Functions SDO XML Data Access Service Functions SDO Relational Data Access Service Functions Semaphore SESAM Database Functions PostgreSQL Session Save Handler Session Handling Functions Shared Memory Functions SimpleXML functions SNMP Functions SOAP Functions Socket Functions Standard PHP Library (SPL) Functions SQLite Functions SQLite Functions (PDO_SQLITE) Secure Shell2 Functions Statistics Functions Stream Functions String Functions Subversion Functions Shockwave Flash Functions Swish Functions Sybase Functions TCP Wrappers Functions Tidy Functions Tokenizer Functions Unicode Functions URL Functions Variable Handling Functions Verisign Payflow Pro Functions vpopmail Functions W32api Functions WDDX Functions win32ps Functions win32service Functions xattr Functions xdiff Functions XML Parser Functions XML-RPC Functions XMLReader functions XMLWriter Functions XSL functions XSLT Functions YAZ Functions YP/NIS Functions Zip File Functions Zlib Compression Functions |