getID3() PHP Library

repository·master·Indexed 22 days ago

https://github.com/jamesheinrich/getid3

A comprehensive PHP library for reading and parsing metadata (tags) from audio, video, image, and data files, with support for writing specific tag formats. It supports a wide array of formats including MP3, FLAC, Ogg, MP4, and JPEG. The library provides different error handling mechanisms across versions, using data arrays in 1.x and exceptions in 2.x. It can be installed via Composer and requires PHP 4.2.0 or higher depending on the version used.

Tokens
1.7K
Snippets
3
Records
11
Agent score
67%

What's inside getID3

  1. Handle errors and warnings in getID3() 1.x

    master

    In version 1.x, the parser communicates issues through the returned data structure:

    • $fileinfo['error']: Contains critical problems. If this key is present, the file was not correctly parsed and the returned data may be incomplete or incorrect.
    • $fileinfo['warning']: Contains non-critical issues or notices. If only warnings are present (and no errors), the data is generally considered OK, though some information might be missing due to known bugs in other software.
  2. Handle errors and warnings in getID3

    master

    The behavior of error reporting depends on the version of getID3 you are using:

    getID3 1.x

    • Critical Errors: If a parser encounters a critical problem, it returns information in $fileinfo['error']. If this key is present, the file was not correctly parsed and the returned data may be incomplete or incorrect.
    • Non-critical Warnings: Less critical issues appear in $fileinfo['warning']. If warnings exist but ['error'] does not, the data is generally considered OK; the library is simply reporting file issues it has successfully worked around.

    getID3 2.x

    • Errors are thrown as exceptions (only one error is returned).
  3. Configure helper applications for Windows support

    master

    On Windows, getID3() requires several binary helper applications to support specific file formats. These binaries should be placed in a dedicated directory. You can specify the location of this directory by defining the GETID3_HELPERAPPSDIR constant in /getid3/getid3.php.

    If the directory is empty or files are missing, download the "getID3()-WindowsSupport" package from the official getID3 website.

  4. Analyze remote files over HTTP or FTP

    master

    getID3 cannot directly parse remote files via URL. You must first download the file to a local temporary location before calling analyze().

    Since version v1.9.9-20150212, the analyze() method accepts optional second and third parameters: original_filesize and original_filename. This allows you to download only a portion of a large remote file while still obtaining accurate playtime estimates, provided the file format only requires the beginning of the file for analysis.

    // Copy remote file locally to scan with getID3()
    $remotefilename = 'http://www.example.com/filename.mp3';
    if ($fp_remote = fopen($remotefilename, 'rb')) {
    	$localtempfilename = tempnam('/tmp', 'getID3');
    	if ($fp_local = fopen($localtempfilename, 'wb')) {
    		while ($buffer = fread($fp_remote, 32768)) {
    			fwrite($fp_local, $buffer);
    		}
    		fclose($fp_local);
    
    	$remote_headers = array_change_key_case(get_headers($remotefilename, 1), CASE_LOWER);
    	$remote_filesize = (isset($remote_headers['content-length']) ? (is_array($remote_headers['content-length']) ? $remote_headers['content-length'][count($remote_headers['content-length']) - 1] : $remote_headers['content-length']) : null);
    
    // Initialize getID3 engine
    	$getID3 = new getID3;
    
    	$ThisFileInfo = $getID3->analyze($localtempfilename, $remote_filesize, basename($remotefilename));
    
    // Delete temporary file
    	unlink($localtempfilename);
    	}
    	fclose($fp_remote);
    }
  5. Requirements for getID3()

    master

    The PHP version required depends on the version of getID3 you are using:

    • getID3() 1.7.x (and earlier): PHP 4.2.0 up to 5.2.x
    • getID3() 1.8.x (and up): PHP 5.0.5 or higher
    • getID3() 1.9.17 (and up): PHP 5.3.0 or higher
    • getID3() 2.0.x (and up): PHP 5.3.0 or higher

    Memory Requirements:

    • Minimum: 4MB
    • Recommended: 8MB
    • Required with all modules loaded: 12MB
  6. Check PHP requirements for getID3

    master

    Ensure your server meets the following PHP version requirements based on the version of getID3 you are using:

    | getID3 Version | Minimum PHP Version | | :--- | :| | 1.7.x and earlier | PHP 4.2.0 up to 5.2.x | | 1.8.x and up | PHP 5.0.5 | | 1.9.17 and up | PHP 5.3.0 | | 2.0.x and up | PHP 5.3.0 |

    Memory Requirements:

    • Minimum: 4MB
    • Recommended: 8MB
    • Required with all modules loaded: 12MB
  7. Analyze a local file with getID3

    master

    To scan a local file for metadata and format information, instantiate the getID3 class and call the analyze() method. For a basic implementation, refer to the demo.basic.php file in the /demos/ directory.

    // Initialize getID3 engine
    $getID3 = new getID3;
    
    // Analyze the file
    $ThisFileInfo = $getID3->analyze($localtempfilename, $remote_filesize, basename($remotefilename));
  8. Supported formats for reading and writing

    master

    getID3 supports a wide array of formats for reading and, in some cases, writing tags.

    Reading (Parsing)

    • Tags: APE (v1/v2), ID3v1 (& v1.1), ID3v2 (v2.2, v2.3, v2.4), Lyrics3 (v1/v2).
    • Audio-Lossy: MP3/MP2/MP1, MPC, Ogg (Vorbis, OggFLAC, Speex, Opus), AAC/MP4, AC3, DTS, RealAudio, Speex, DSS, VQF.
    • Audio-Lossless: AIFF, AU, Bonk, CD-audio (*.cda), FLAC, LA, LiteWave, LPAC, MIDI, Monkey's Audio, OptimFROG, RKAU, Shorten, TTA, VOC, WAV (RIFF), WavPack.
    • Audio-Video: ASF (WMA, WMV), AVI (RIFF), Flash, Matroska (MKV), MPEG-1/MPEG-2, NSV, Quicktime (including MP4).
    • Still Image: BMP, GIF, JPEG, JPEG XL, PNG, TIFF, SWF, PhotoCD.
    • Data: ISO-9660, SZIP (limited), ZIP, TAR, CUE.

    Writing (Tagging)

    • ID3v1 (& v1.1)
    • ID3v2 (v2.3 & v2.4)
    • VorbisComment on OggVorbis
    • VorbisComment on FLAC (not OggFLAC)
    • APE v2
    • Lyrics3 (delete only)