コード例 #1
0
ファイル: getid3.php プロジェクト: BackupTheBerlios/sotf
function GetAllFileInfo($filename, $assumedFormat = '', $MD5file = false, $MD5data = false, $MD5dataIfMD5SourceKnown = false)
{
    $ThisFileInfo = array();
    $localfilepointer = null;
    if (!InitializeFilepointerArray($filename, $localfilepointer, $ThisFileInfo)) {
        // remove unneeded/meaningless keys
        CleanUpGetAllMP3info($ThisFileInfo);
        // close & remove local filepointer
        CloseRemoveFilepointer($localfilepointer);
        return $ThisFileInfo;
    }
    // Handle ID3v1 & Lyrics3 tags
    HandleID3v1Tag($localfilepointer, $ThisFileInfo);
    HandleLyrics3Tag($localfilepointer, $ThisFileInfo);
    rewind($localfilepointer);
    //$formattest = fread($localfilepointer, 16);  // 16 bytes is sufficient for any format except ISO CD-image
    $formattest = fread($localfilepointer, 32774);
    // (ISO needs at least 32774 bytes)
    // Handle ID3v2 tag
    if (HandleID3v2Tag($localfilepointer, $ThisFileInfo)) {
        fseek($localfilepointer, $ThisFileInfo['avdataoffset'], SEEK_SET);
        $formattest = fread($localfilepointer, 32774);
        // (ISO9660 needs at least 32774 bytes)
    }
    // Handle APE tags
    HandleAPETag($localfilepointer, $ThisFileInfo);
    // Nothing-but-tags check
    if ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'] > 0) {
        if ($assumedFormat) {
            $format_info = GetFileFormatArray();
            require_once GETID3_INCLUDEPATH . $format_info["{$assumedFormat}"]['include'];
            $VariableFunctionName = $format_info["{$assumedFormat}"]['function'];
            $VariableFunctionName($localfilepointer, $ThisFileInfo);
        } elseif ($DeterminedFormat = GetFileFormat($formattest)) {
            // break if ID3/APE tags found on illegal format
            if (!$DeterminedFormat['allowtags'] && $ThisFileInfo['avdataoffset'] > 0 && $ThisFileInfo['avdataend'] != $ThisFileInfo['filesize']) {
                $ThisFileInfo['error'] .= "\n" . 'Illegal ID3 and/or APE tag found on non multimedia file.';
                break;
            }
            // set mime type
            $ThisFileInfo['mime_type'] = $DeterminedFormat['mimetype'];
            // supported format signature pattern detected
            if (!file_exists(GETID3_INCLUDEPATH . $DeterminedFormat['include'])) {
                die('<HR><FONT COLOR="#FF0000">Cannot find required file: ' . GETID3_INCLUDEPATH . $DeterminedFormat['include'] . '</FONT>');
            }
            require_once GETID3_INCLUDEPATH . $DeterminedFormat['include'];
            $VariableFunctionName = $DeterminedFormat['function'];
            $VariableFunctionName($localfilepointer, $ThisFileInfo);
        } elseif ($assumedFormat == 'mp3' || $assumedFormat == '' && (substr($formattest, 0, 3) == 'ID3' || substr(BigEndian2Bin(substr($formattest, 0, 2)), 0, 11) == '11111111111')) {
            // assume AAC-ADTS format
            require_once GETID3_INCLUDEPATH . 'getid3.aac.php';
            $dummy = $ThisFileInfo;
            if (getAACADTSheaderFilepointer($localfilepointer, $dummy)) {
                $ThisFileInfo = $dummy;
            } else {
                // it's not AAC-ADTS format, probably MP3
                require_once GETID3_INCLUDEPATH . 'getid3.mp3.php';
                getMP3headerFilepointer($localfilepointer, $ThisFileInfo);
            }
        } else {
            // unknown format, do nothing
        }
    }
    if (!empty($ThisFileInfo['audio']['channels'])) {
        switch ($ThisFileInfo['audio']['channels']) {
            case 1:
                $ThisFileInfo['audio']['channelmode'] = 'mono';
                break;
            case 2:
                $ThisFileInfo['audio']['channelmode'] = 'stereo';
                break;
            default:
                // unknown?
                break;
        }
    }
    // Get the MD5 hash of the entire file
    if ($MD5file && empty($ThisFileInfo['md5_file'])) {
        set_time_limit(max($ThisFileInfo['filesize'] / 10000000, 30));
        $ThisFileInfo['md5_file'] = md5_file($filename);
    }
    // Get the MD5 hash of the audio/video portion of the file
    // (without ID3/APE/Lyrics3/etc header/footer tags
    if ($MD5data && empty($ThisFileInfo['md5_data'])) {
        if ($MD5dataIfMD5SourceKnown || empty($ThisFileInfo['md5_data_source'])) {
            // Only calculate ['md5_data'] if ['md5_data_source'] is unknown,
            // or if $MD5dataIfMD5SourceKnown is set to true
            getMD5data($ThisFileInfo);
        }
    }
    CalculateCompressionRatioVideo($ThisFileInfo);
    CalculateCompressionRatioAudio($ThisFileInfo);
    // return tags data in alphabetical order, without duplicates
    $ThisFileInfo['tags'] = array_unique($ThisFileInfo['tags']);
    sort($ThisFileInfo['tags']);
    // remove unneeded/meaningless keys
    CleanUpGetAllMP3info($ThisFileInfo);
    if (isset($ThisFileInfo['fileformat'])) {
        // Calculate combined bitrate - audio + video
        $CombinedBitrate = 0;
        $CombinedBitrate += isset($ThisFileInfo['audio']['bitrate']) ? $ThisFileInfo['audio']['bitrate'] : 0;
        $CombinedBitrate += isset($ThisFileInfo['video']['bitrate']) ? $ThisFileInfo['video']['bitrate'] : 0;
        if ($CombinedBitrate > 0 && !isset($ThisFileInfo['bitrate'])) {
            $ThisFileInfo['bitrate'] = $CombinedBitrate;
        }
        if (isset($ThisFileInfo['video']) && !isset($ThisFileInfo['video']['bitrate']) || isset($ThisFileInfo['audio']) && !isset($ThisFileInfo['audio']['bitrate'])) {
            // for example, VBR MPEG video files cannot determine video bitrate:
            // should not set overall bitrate and playtime from audio bitrate only
            unset($ThisFileInfo['bitrate']);
        }
        if (!isset($ThisFileInfo['playtime_seconds']) && !empty($ThisFileInfo['bitrate'])) {
            $ThisFileInfo['playtime_seconds'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 / $ThisFileInfo['bitrate'];
        }
        // Set playtime string
        if (!empty($ThisFileInfo['playtime_seconds']) && empty($ThisFileInfo['playtime_string'])) {
            $ThisFileInfo['playtime_string'] = PlaytimeString($ThisFileInfo['playtime_seconds']);
        }
    }
    // close & remove local filepointer
    CloseRemoveFilepointer($localfilepointer);
    return $ThisFileInfo;
}
コード例 #2
0
function GetAllMP3info($filename, $assumedFormat = '', $allowedFormats = array('zip', 'ogg', 'riff', 'sdss', 'mpeg', 'midi', 'image', 'mp3'))
{
    include_once GETID3_INCLUDEPATH . 'getid3.lookup.php';
    // Lookup tables
    include_once GETID3_INCLUDEPATH . 'getid3.functions.php';
    // Function library
    include_once GETID3_INCLUDEPATH . 'getid3.getimagesize.php';
    if (!file_exists($filename)) {
        // this code segment is needed for the file browser demonstrated in check.php
        // but may interfere with finding a filename that actually does contain apparently
        // escaped characters (like "file\'name.mp3") and/or
        // %xx-format characters (like "file%20name.mp3")
        $filename = stripslashes($filename);
        if (!file_exists($filename)) {
            $filename = rawurldecode($filename);
        }
    }
    $fp = @fopen($filename, 'rb');
    $MP3fileInfo['getID3version'] = GETID3VERSION;
    $MP3fileInfo['exist'] = (bool) $fp;
    $MP3fileInfo['filename'] = basename($filename);
    $MP3fileInfo['fileformat'] = '';
    // filled in later
    $MP3fileInfo['error'] = '';
    // filled in later, unset if not used
    if ($MP3fileInfo['exist']) {
        if (strpos($filename, 'http://') !== FALSE || strpos($filename, 'ftp://') !== FALSE) {
            // remote file - copy locally first and work from there
            $localfilepointer = tmpfile();
            while ($buffer = fread($fp, FREAD_BUFFER_SIZE)) {
                $MP3fileInfo['filesize'] += fwrite($localfilepointer, $buffer);
            }
            fclose($fp);
        } else {
            clearstatcache();
            $MP3fileInfo['filesize'] = filesize($filename);
            $localfilepointer = $fp;
        }
        rewind($localfilepointer);
        $formattest = fread($localfilepointer, FREAD_BUFFER_SIZE);
        if (ParseAsThisFormat('zip', $assumedFormat, $allowedFormats, $formattest)) {
            $MP3fileInfo['fileformat'] = 'zip';
            include_once GETID3_INCLUDEPATH . 'getid3.zip.php';
            getZipHeaderFilepointer($filename, $MP3fileInfo);
        } else {
            if (ParseAsThisFormat('ogg', $assumedFormat, $allowedFormats, $formattest)) {
                $MP3fileInfo['fileformat'] = 'ogg';
                include_once GETID3_INCLUDEPATH . 'getid3.ogg.php';
                getOggHeaderFilepointer($localfilepointer, $MP3fileInfo);
            } else {
                if (ParseAsThisFormat('riff', $assumedFormat, $allowedFormats, $formattest) || ParseAsThisFormat('sdss', $assumedFormat, $allowedFormats, $formattest)) {
                    $MP3fileInfo['fileformat'] = 'riff';
                    include_once GETID3_INCLUDEPATH . 'getid3.riff.php';
                    getRIFFHeaderFilepointer($localfilepointer, $MP3fileInfo);
                } else {
                    if (ParseAsThisFormat('mpeg', $assumedFormat, $allowedFormats, $formattest)) {
                        $MP3fileInfo['fileformat'] = 'mpg';
                        include_once GETID3_INCLUDEPATH . 'getid3.mpeg.php';
                        getMPEGHeaderFilepointer($localfilepointer, $MP3fileInfo);
                    } else {
                        if (ParseAsThisFormat('midi', $assumedFormat, $allowedFormats, $formattest)) {
                            $MP3fileInfo['fileformat'] = 'midi';
                            include_once GETID3_INCLUDEPATH . 'getid3.midi.php';
                            if ($assumedFormat === FALSE) {
                                // do not parse all MIDI tracks - much faster
                                getMIDIHeaderFilepointer($localfilepointer, $MP3fileInfo, FALSE);
                            } else {
                                getMIDIHeaderFilepointer($localfilepointer, $MP3fileInfo);
                            }
                        } else {
                            if (in_array('image', $allowedFormats) && ($assumedFormat == 'image' || ($imagechunkcheck = GetDataImageSize($formattest)) && $imagechunkcheck[2] >= 1 && $imagechunkcheck[2] <= 3)) {
                                if ($assumedFormat == 'image') {
                                    $imagechunkcheck = GetDataImageSize($formattest);
                                }
                                $imagetypes = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
                                if (isset($imagechunkcheck[2]) && $imagechunkcheck[2] >= 1 && $imagechunkcheck[2] <= 3) {
                                    $MP3fileInfo['fileformat'] = $imagetypes["{$imagechunkcheck[2]}"];
                                    $MP3fileInfo["{$MP3fileInfo['fileformat']}"]['width'] = $imagechunkcheck[0];
                                    $MP3fileInfo["{$MP3fileInfo['fileformat']}"]['height'] = $imagechunkcheck[1];
                                } else {
                                    unset($MP3fileInfo['fileformat']);
                                    $MP3fileInfo['error'] = "\n" . 'Not a supported image format';
                                }
                            } else {
                                if (in_array('mp3', $allowedFormats) && $allowedFormats !== FALSE && ($assumedFormat == 'mp3' || $assumedFormat == '' && (substr($formattest, 0, 3) == 'ID3' || substr(BigEndian2Bin(substr($formattest, 0, 2)), 0, 11) == '11111111111'))) {
                                    // assume MP3 format
                                    include_once GETID3_INCLUDEPATH . 'getid3.mp3.php';
                                    getMP3headerFilepointer($localfilepointer, $MP3fileInfo);
                                    if (!isset($MP3fileInfo['audiodataoffset'])) {
                                        $MP3fileInfo['audiobytes'] = 0;
                                    } else {
                                        $MP3fileInfo['audiobytes'] = $MP3fileInfo['filesize'] - $MP3fileInfo['audiodataoffset'];
                                    }
                                    if (isset($MP3fileInfo['id3']['id3v1'])) {
                                        $MP3fileInfo['audiobytes'] -= 128;
                                    }
                                    if (isset($mp3info['lyrics3']['raw']['lyrics3tagsize'])) {
                                        $MP3fileInfo['audiobytes'] -= $mp3info['lyrics3']['raw']['lyrics3tagsize'];
                                    }
                                    if ($MP3fileInfo['audiobytes'] < 0) {
                                        unset($MP3fileInfo['audiobytes']);
                                    }
                                    if (isset($MP3fileInfo['audiobytes']) && isset($MP3fileInfo['bitrate']) && $MP3fileInfo['bitrate'] > 0) {
                                        $MP3fileInfo['playtime_seconds'] = $MP3fileInfo['audiobytes'] * 8 / $MP3fileInfo['bitrate'];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (isset($MP3fileInfo['playtime_seconds']) && $MP3fileInfo['playtime_seconds'] > 0 && !isset($MP3fileInfo['playtime_string'])) {
        $contentseconds = round(($MP3fileInfo['playtime_seconds'] / 60 - floor($MP3fileInfo['playtime_seconds'] / 60)) * 60);
        $contentminutes = floor($MP3fileInfo['playtime_seconds'] / 60);
        $MP3fileInfo['playtime_string'] = number_format($contentminutes) . ':' . str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
    }
    if (isset($MP3fileInfo['error']) && !$MP3fileInfo['error']) {
        unset($MP3fileInfo['error']);
    }
    if (isset($MP3fileInfo['fileformat']) && !$MP3fileInfo['fileformat']) {
        unset($MP3fileInfo['fileformat']);
    }
    unset($SourceArrayKey);
    if (isset($MP3fileInfo['id3']['id3v2'])) {
        $SourceArrayKey = $MP3fileInfo['id3']['id3v2'];
    } else {
        if (isset($MP3fileInfo['id3']['id3v1'])) {
            $SourceArrayKey = $MP3fileInfo['id3']['id3v1'];
        } else {
            if (isset($MP3fileInfo['ogg'])) {
                $SourceArrayKey = $MP3fileInfo['ogg'];
            } else {
                if (isset($MP3fileInfo['RIFF'])) {
                    $SourceArrayKey = $MP3fileInfo['RIFF'];
                }
            }
        }
    }
    if (isset($SourceArrayKey)) {
        $handyaccesskeystocopy = array('title', 'artist', 'album', 'year', 'genre', 'comment', 'track');
        foreach ($handyaccesskeystocopy as $keytocopy) {
            if (isset($SourceArrayKey["{$keytocopy}"])) {
                $MP3fileInfo["{$keytocopy}"] = $SourceArrayKey["{$keytocopy}"];
            }
        }
    }
    if (isset($fp) && is_resource($fp) && get_resource_type($fp) == 'file') {
        fclose($fp);
    }
    if (isset($localfilepointer) && is_resource($localfilepointer) && get_resource_type($localfilepointer) == 'file') {
        fclose($localfilepointer);
    }
    if (isset($fp)) {
        unset($fp);
    }
    if (isset($localfilepointer)) {
        unset($localfilepointer);
    }
    return $MP3fileInfo;
}
コード例 #3
0
function getMP3header($filename, &$MP3fileInfo)
{
    $fd = fopen($filename, 'rb');
    return getMP3headerFilepointer($fd, $MP3fileInfo);
}
コード例 #4
0
ファイル: getid3.php プロジェクト: BackupTheBerlios/sotf-svn
function GetAllFileInfo($filename, $assumedFormat = '', $MD5file = false, $MD5data = false)
{
    require_once GETID3_INCLUDEPATH . 'getid3.functions.php';
    // Function library
    $ThisFileInfo['getID3version'] = GETID3VERSION;
    $ThisFileInfo['fileformat'] = '';
    // filled in later
    $ThisFileInfo['audio']['dataformat'] = '';
    // filled in later, unset if not used
    $ThisFileInfo['video']['dataformat'] = '';
    // filled in later, unset if not used
    $ThisFileInfo['tags'] = array();
    // filled in later, unset if not used
    $ThisFileInfo['error'] = '';
    // filled in later, unset if not used
    $ThisFileInfo['warning'] = '';
    // filled in later, unset if not used
    $ThisFileInfo['exist'] = false;
    if (eregi('^(ht|f)tp://', $filename)) {
        // remote file
        $ThisFileInfo['filename'] = $filename;
        $ThisFileInfo['error'] .= "\n" . 'Remote files are not supported in this version of getID3() - please copy the file locally first';
    } else {
        // local file
        $ThisFileInfo['filename'] = basename($filename);
        $ThisFileInfo['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
        $ThisFileInfo['filenamepath'] = $ThisFileInfo['filepath'] . '/' . $ThisFileInfo['filename'];
        ob_start();
        if ($localfilepointer = fopen($filename, 'rb')) {
            $ThisFileInfo['exist'] = true;
            //$ThisFileInfo['filesize'] = filesize($ThisFileInfo['filenamepath']);
            // PHP doesn't support integers larger than 31-bit (~2GB)
            // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
            // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
            fseek($localfilepointer, 0, SEEK_END);
            $ThisFileInfo['filesize'] = ftell($localfilepointer);
            ob_end_clean();
            if ($ThisFileInfo['filesize'] == 0) {
                if (filesize($ThisFileInfo['filenamepath']) != 0) {
                    unset($ThisFileInfo['filesize']);
                    $ThisFileInfo['error'] .= "\n" . 'File is most likely larger than 2GB and is not supported by PHP';
                }
                // remove unneeded/meaningless keys
                CleanUpGetAllMP3info($ThisFileInfo);
                // close & remove local filepointer
                CloseRemoveFilepointer($localfilepointer);
                return $ThisFileInfo;
            }
        } else {
            $ThisFileInfo['error'] .= "\n" . 'Error opening file: ' . trim(strip_tags(ob_get_contents()));
            ob_end_clean();
            // remove unneeded/meaningless keys
            CleanUpGetAllMP3info($ThisFileInfo);
            // close & remove local filepointer
            CloseRemoveFilepointer($localfilepointer);
            return $ThisFileInfo;
        }
    }
    // Initialize avdataoffset and avdataend
    $ThisFileInfo['avdataoffset'] = 0;
    $ThisFileInfo['avdataend'] = $ThisFileInfo['filesize'];
    // Handle APE tags
    HandleAPETag($localfilepointer, $ThisFileInfo);
    rewind($localfilepointer);
    //$formattest = fread($localfilepointer, 16);  // 16 bytes is sufficient for any format except ISO CD-image
    $formattest = fread($localfilepointer, 32774);
    // (ISO needs at least 32774 bytes)
    // Handle ID3v2 tag
    if (substr($formattest, 0, 3) == 'ID3') {
        HandleID3v2Tag($localfilepointer, $ThisFileInfo);
        rewind($localfilepointer);
        fseek($localfilepointer, $ThisFileInfo['avdataoffset'], SEEK_SET);
        $formattest = fread($localfilepointer, 32774);
        // (ISO9660 needs at least 32774 bytes)
    }
    // Handle ID3v1 tags
    HandleID3v1Tag($localfilepointer, $ThisFileInfo);
    // Nothing-but-tags check
    if ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'] > 0) {
        if ($DeterminedFormat = GetFileFormat($formattest)) {
            // break if ID3/APE tags found on illegal format
            if (!$DeterminedFormat['allowtags'] && $ThisFileInfo['avdataoffset'] > 0 && $ThisFileInfo['avdataend'] != $ThisFileInfo['filesize']) {
                $ThisFileInfo['error'] .= "\n" . 'Illegal ID3 and/or APE tag found on non multimedia file.';
                break;
            }
            // set mime type
            $ThisFileInfo['mime_type'] = $DeterminedFormat['mimetype'];
            // supported format signature pattern detected
            require_once GETID3_INCLUDEPATH . $DeterminedFormat['include'];
            switch ($DeterminedFormat['format']) {
                //case 'midi':
                //    if ($assumedFormat === false) {
                //        // do not parse all MIDI tracks - much faster
                //        getMIDIHeaderFilepointer($localfilepointer, $ThisFileInfo, false);
                //    } else {
                //        getMIDIHeaderFilepointer($localfilepointer, $ThisFileInfo);
                //    }
                //    break;
                //case 'aac':
                //    if (!getAACADIFheaderFilepointer($localfilepointer, $ThisFileInfo)) {
                //        $dummy = $ThisFileInfo;
                //        unset($dummy['error']);
                //        if (getAACADTSheaderFilepointer($localfilepointer, $dummy)) {
                //            $ThisFileInfo = $dummy;
                //        }
                //    }
                //    break;
                default:
                    $VariableFunctionName = $DeterminedFormat['function'];
                    $VariableFunctionName($localfilepointer, $ThisFileInfo);
                    break;
            }
        } elseif ($assumedFormat == 'mp3' || $assumedFormat == '' && (substr($formattest, 0, 3) == 'ID3' || substr(BigEndian2Bin(substr($formattest, 0, 2)), 0, 11) == '11111111111')) {
            // assume AAC-ADTS format
            require_once GETID3_INCLUDEPATH . 'getid3.aac.php';
            $dummy = $ThisFileInfo;
            if (getAACADTSheaderFilepointer($localfilepointer, $dummy)) {
                $ThisFileInfo = $dummy;
            } else {
                // it's not AAC-ADTS format, probably MP3
                require_once GETID3_INCLUDEPATH . 'getid3.mp3.php';
                getMP3headerFilepointer($localfilepointer, $ThisFileInfo);
            }
        } else {
            // unknown format, do nothing
        }
    }
    if (isset($ThisFileInfo['fileformat'])) {
        // Calculate combined bitrate - audio + video
        $CombinedBitrate = 0;
        $CombinedBitrate += isset($ThisFileInfo['audio']['bitrate']) ? $ThisFileInfo['audio']['bitrate'] : 0;
        $CombinedBitrate += isset($ThisFileInfo['video']['bitrate']) ? $ThisFileInfo['video']['bitrate'] : 0;
        if ($CombinedBitrate > 0 && !isset($ThisFileInfo['bitrate'])) {
            $ThisFileInfo['bitrate'] = $CombinedBitrate;
        }
        // Set playtime string
        if (!empty($ThisFileInfo['playtime_seconds']) && empty($ThisFileInfo['playtime_string'])) {
            $ThisFileInfo['playtime_string'] = PlaytimeString($ThisFileInfo['playtime_seconds']);
        }
        if (!empty($ThisFileInfo['audio']['channels'])) {
            switch ($ThisFileInfo['audio']['channels']) {
                case 1:
                    $ThisFileInfo['audio']['channelmode'] = 'mono';
                    break;
                case 2:
                    $ThisFileInfo['audio']['channelmode'] = 'stereo';
                    break;
                default:
                    // unknown?
                    break;
            }
        }
    }
    // Get the MD5 hash of the entire file
    if ($MD5file && empty($ThisFileInfo['md5_file'])) {
        $ThisFileInfo['md5_file'] = md5_file($filename);
    }
    // Get the MD5 hash of the audio/video portion of the file
    // (without ID3/APE/Lyrics3/etc header/footer tags
    if ($MD5data && empty($ThisFileInfo['md5_data'])) {
        getMD5data($ThisFileInfo);
    }
    // return tags data in alphabetical order, without duplicates
    $ThisFileInfo['tags'] = array_unique($ThisFileInfo['tags']);
    sort($ThisFileInfo['tags']);
    // remove unneeded/meaningless keys
    CleanUpGetAllMP3info($ThisFileInfo);
    // close & remove local filepointer
    CloseRemoveFilepointer($localfilepointer);
    return $ThisFileInfo;
}