Esempio n. 1
0
function getMPEGHeaderFilepointer(&$fd, &$ThisFileInfo)
{
    $ThisFileInfo['fileformat'] = 'mpeg';
    // Start code                       32 bits
    // horizontal frame size            12 bits
    // vertical frame size              12 bits
    // pixel aspect ratio                4 bits
    // frame rate                        4 bits
    // bitrate                          18 bits
    // marker bit                        1 bit
    // VBV buffer size                  10 bits
    // constrained parameter flag        1 bit
    // intra quant. matrix flag          1 bit
    // intra quant. matrix values      512 bits (present if matrix flag == 1)
    // non-intra quant. matrix flag      1 bit
    // non-intra quant. matrix values  512 bits (present if matrix flag == 1)
    fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
    $MPEGvideoHeader = fread($fd, FREAD_BUFFER_SIZE);
    $offset = 0;
    // MPEG video information is found as $00 $00 $01 $B3
    $matching_pattern = chr(0x0) . chr(0x0) . chr(0x1) . chr(0xb3);
    while (substr($MPEGvideoHeader, $offset++, 4) !== $matching_pattern) {
        if ($offset >= strlen($MPEGvideoHeader) - 12) {
            $MPEGvideoHeader .= fread($fd, FREAD_BUFFER_SIZE);
            $MPEGvideoHeader = substr($MPEGvideoHeader, $offset);
            $offset = 0;
            if (strlen($MPEGvideoHeader) < 12) {
                $ThisFileInfo['error'] .= "\n" . 'Could not find start of video block before end of file';
                unset($ThisFileInfo['fileformat']);
                return false;
            } elseif (ftell($fd) >= 100000) {
                $ThisFileInfo['error'] .= "\n" . 'Could not find start of video block in the first 100,000 bytes (this might not be an MPEG-video file?)';
                unset($ThisFileInfo['fileformat']);
                return false;
            }
        }
    }
    $ThisFileInfo['video']['dataformat'] = 'mpeg';
    $offset += strlen($matching_pattern) - 1;
    $FrameSizeAspectRatioFrameRateDWORD = BigEndian2Int(substr($MPEGvideoHeader, $offset, 4));
    $offset += 4;
    $assortedinformation = BigEndian2Int(substr($MPEGvideoHeader, $offset, 4));
    $offset += 4;
    $ThisFileInfo['mpeg']['video']['raw']['framesize_horizontal'] = ($FrameSizeAspectRatioFrameRateDWORD & 4293918720.0) >> 20;
    // 12 bits for horizontal frame size
    $ThisFileInfo['mpeg']['video']['raw']['framesize_vertical'] = ($FrameSizeAspectRatioFrameRateDWORD & 0xfff00) >> 8;
    // 12 bits for vertical frame size
    $ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio'] = ($FrameSizeAspectRatioFrameRateDWORD & 0xf0) >> 4;
    $ThisFileInfo['mpeg']['video']['raw']['frame_rate'] = $FrameSizeAspectRatioFrameRateDWORD & 0xf;
    $ThisFileInfo['mpeg']['video']['framesize_horizontal'] = $ThisFileInfo['mpeg']['video']['raw']['framesize_horizontal'];
    $ThisFileInfo['mpeg']['video']['framesize_vertical'] = $ThisFileInfo['mpeg']['video']['raw']['framesize_vertical'];
    $ThisFileInfo['video']['resolution_x'] = $ThisFileInfo['mpeg']['video']['framesize_horizontal'];
    $ThisFileInfo['video']['resolution_y'] = $ThisFileInfo['mpeg']['video']['framesize_vertical'];
    $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio'] = MPEGvideoAspectRatioLookup($ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
    $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio_text'] = MPEGvideoAspectRatioTextLookup($ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
    $ThisFileInfo['mpeg']['video']['frame_rate'] = MPEGvideoFramerateLookup($ThisFileInfo['mpeg']['video']['raw']['frame_rate']);
    $ThisFileInfo['video']['frame_rate'] = $ThisFileInfo['mpeg']['video']['frame_rate'];
    $ThisFileInfo['mpeg']['video']['raw']['bitrate'] = ($assortedinformation & 4294950912.0) >> 14;
    $ThisFileInfo['mpeg']['video']['raw']['marker_bit'] = ($assortedinformation & 0x2000) >> 13;
    $ThisFileInfo['mpeg']['video']['raw']['vbv_buffer_size'] = ($assortedinformation & 0x1ff8) >> 3;
    $ThisFileInfo['mpeg']['video']['raw']['constrained_param_flag'] = ($assortedinformation & 0x4) >> 2;
    $ThisFileInfo['mpeg']['video']['raw']['intra_quant_flag'] = ($assortedinformation & 0x2) >> 1;
    if ($ThisFileInfo['mpeg']['video']['raw']['bitrate'] == 0x3ffff) {
        // 18 set bits
        $ThisFileInfo['mpeg']['video']['bitrate_type'] = 'variable';
        $ThisFileInfo['bitrate_mode'] = 'vbr';
    } else {
        $ThisFileInfo['mpeg']['video']['bitrate'] = $ThisFileInfo['mpeg']['video']['raw']['bitrate'] * 400;
        $ThisFileInfo['mpeg']['video']['bitrate_mode'] = 'cbr';
        $ThisFileInfo['video']['bitrate_mode'] = $ThisFileInfo['mpeg']['video']['bitrate_mode'];
        $ThisFileInfo['video']['bitrate'] = $ThisFileInfo['mpeg']['video']['bitrate'];
    }
    $ThisFileInfo['video']['bitrate_mode'] = $ThisFileInfo['mpeg']['video']['bitrate_mode'];
    $ThisFileInfo['video']['bitrate'] = $ThisFileInfo['mpeg']['video']['bitrate'];
    return true;
}
function getMPEGHeaderFilepointer(&$fd, &$MP3fileInfo)
{
    // Start code                       32 bits
    // horizontal frame size            12 bits
    // vertical frame size              12 bits
    // pixel aspect ratio                4 bits
    // frame rate                        4 bits
    // bitrate                          18 bits
    // marker bit                        1 bit
    // VBV buffer size                  10 bits
    // constrained parameter flag        1 bit
    // intra quant. matrix flag          1 bit
    // intra quant. matrix values      512 bits (present if matrix flag == 1)
    // non-intra quant. matrix flag      1 bit
    // non-intra quant. matrix values  512 bits (present if matrix flag == 1)
    if (!$fd) {
        $MP3fileInfo['error'] .= "\n" . 'Could not open file';
        return FALSE;
    } else {
        // MPEG video information is found as $00 $00 $01 $B3
        $matching_pattern = chr(0x0) . chr(0x0) . chr(0x1) . chr(0xb3);
        rewind($fd);
        $MPEGvideoHeader = fread($fd, FREAD_BUFFER_SIZE);
        $offset = 0;
        while (substr($MPEGvideoHeader, $offset++, 4) !== $matching_pattern) {
            if ($offset >= strlen($MPEGvideoHeader) - 12) {
                $MPEGvideoHeader .= fread($fd, FREAD_BUFFER_SIZE);
                $MPEGvideoHeader = substr($MPEGvideoHeader, $offset);
                $offset = 0;
                if (strlen($MPEGvideoHeader) < 12) {
                    $MP3fileInfo['error'] = "\n" . 'Could not find start of video block before end of file';
                    return FALSE;
                    // return array('error'=>'Could not find start of video block before end of file');
                } else {
                    if (ftell($fd) >= 100000) {
                        $MP3fileInfo['error'] = "\n" . 'Could not find start of video block in the first 100,000 bytes (this might not be an MPEG-video file?)';
                        unset($MP3fileInfo['fileformat']);
                        return FALSE;
                        // return array('error'=>"\n".'Could not find start of video block in the first 100,000 bytes (this might not be an MPEG-video file?)', 'fileformat'=>'');
                    }
                }
            }
        }
        $offset += strlen($matching_pattern) - 1;
        $framesizes = BigEndian2Bin(substr($MPEGvideoHeader, $offset, 3));
        $offset += 3;
        $aspect_framerate = BigEndian2Bin(substr($MPEGvideoHeader, $offset, 1));
        $offset += 1;
        $assortedinformation = BigEndian2Bin(substr($MPEGvideoHeader, $offset, 4));
        $offset += 4;
        $MP3fileInfo['mpeg']['video']['raw']['framesize_horizontal'] = bindec(substr($framesizes, 0, 12));
        // 12 bits for horizontal frame size
        $MP3fileInfo['mpeg']['video']['raw']['framesize_vertical'] = bindec(substr($framesizes, 12, 12));
        // 12 bits for vertical frame size
        $MP3fileInfo['mpeg']['video']['framesize_horizontal'] = $MP3fileInfo['mpeg']['video']['raw']['framesize_horizontal'];
        $MP3fileInfo['mpeg']['video']['framesize_vertical'] = $MP3fileInfo['mpeg']['video']['raw']['framesize_vertical'];
        $MP3fileInfo['mpeg']['video']['raw']['pixel_aspect_ratio'] = bindec(substr($aspect_framerate, 0, 4));
        $MP3fileInfo['mpeg']['video']['raw']['frame_rate'] = bindec(substr($aspect_framerate, 4, 4));
        $MP3fileInfo['mpeg']['video']['pixel_aspect_ratio'] = MPEGvideoAspectRatioLookup($MP3fileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
        $MP3fileInfo['mpeg']['video']['pixel_aspect_ratio_text'] = MPEGvideoAspectRatioTextLookup($MP3fileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
        $MP3fileInfo['mpeg']['video']['frame_rate'] = MPEGvideoFramerateLookup($MP3fileInfo['mpeg']['video']['raw']['frame_rate']);
        $MP3fileInfo['mpeg']['video']['raw']['bitrate'] = bindec(substr($assortedinformation, 0, 18));
        if ($MP3fileInfo['mpeg']['video']['raw']['bitrate'] == 0x3ffff) {
            // 18 set bits
            $MP3fileInfo['mpeg']['video']['bitrate_type'] = 'variable';
        } else {
            $MP3fileInfo['mpeg']['video']['bitrate_type'] = 'constant';
            $MP3fileInfo['mpeg']['video']['bitrate_bps'] = $MP3fileInfo['mpeg']['video']['raw']['bitrate'] * 400;
            //$MP3fileInfo['mpeg']['video']['bitrate_kbytes'] = $MP3fileInfo['mpeg']['video']['bitrate_bps'] / 8192;
        }
        $MP3fileInfo['mpeg']['video']['raw']['marker_bit'] = bindec(substr($assortedinformation, 18, 1));
        $MP3fileInfo['mpeg']['video']['raw']['vbv_buffer_size'] = bindec(substr($assortedinformation, 19, 10));
        $MP3fileInfo['mpeg']['video']['raw']['constrained_param_flag'] = bindec(substr($assortedinformation, 29, 1));
        $MP3fileInfo['mpeg']['video']['raw']['intra_quant_flag'] = bindec(substr($assortedinformation, 30, 1));
        return TRUE;
    }
}
Esempio n. 3
0
function getMPEGHeaderFilepointer(&$fd, &$ThisFileInfo)
{
    $ThisFileInfo['fileformat'] = 'mpeg';
    fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
    $MPEGstreamData = fread($fd, min(100000, $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']));
    $MPEGstreamDataLength = strlen($MPEGstreamData);
    $foundVideo = true;
    $VideoChunkOffset = 0;
    while (substr($MPEGstreamData, $VideoChunkOffset++, 4) !== MPEG_VIDEO_SEQUENCE_HEADER) {
        if ($VideoChunkOffset >= $MPEGstreamDataLength) {
            $foundVideo = false;
            break 2;
        }
    }
    if ($foundVideo) {
        // Start code                       32 bits
        // horizontal frame size            12 bits
        // vertical frame size              12 bits
        // pixel aspect ratio                4 bits
        // frame rate                        4 bits
        // bitrate                          18 bits
        // marker bit                        1 bit
        // VBV buffer size                  10 bits
        // constrained parameter flag        1 bit
        // intra quant. matrix flag          1 bit
        // intra quant. matrix values      512 bits (present if matrix flag == 1)
        // non-intra quant. matrix flag      1 bit
        // non-intra quant. matrix values  512 bits (present if matrix flag == 1)
        $ThisFileInfo['video']['dataformat'] = 'mpeg';
        // I don't know how to differentiate between MPEG-1 and MPEG-2 video stream
        // Any information appreciated: info@getid3.org
        //$ThisFileInfo['video']['codec']      = 'MPEG-1';
        //$ThisFileInfo['video']['codec']      = 'MPEG-2';
        $ThisFileInfo['video']['codec'] = 'MPEG';
        $VideoChunkOffset += strlen(MPEG_VIDEO_SEQUENCE_HEADER) - 1;
        $FrameSizeDWORD = BigEndian2Int(substr($MPEGstreamData, $VideoChunkOffset, 3));
        $VideoChunkOffset += 3;
        $AspectRatioFrameRateDWORD = BigEndian2Int(substr($MPEGstreamData, $VideoChunkOffset, 1));
        $VideoChunkOffset += 1;
        $assortedinformation = BigEndian2Bin(substr($MPEGstreamData, $VideoChunkOffset, 4));
        $VideoChunkOffset += 4;
        $ThisFileInfo['mpeg']['video']['raw']['framesize_horizontal'] = ($FrameSizeDWORD & 0xfff000) >> 12;
        // 12 bits for horizontal frame size
        $ThisFileInfo['mpeg']['video']['raw']['framesize_vertical'] = $FrameSizeDWORD & 0xfff;
        // 12 bits for vertical frame size
        $ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio'] = ($AspectRatioFrameRateDWORD & 0xf0) >> 4;
        $ThisFileInfo['mpeg']['video']['raw']['frame_rate'] = $AspectRatioFrameRateDWORD & 0xf;
        $ThisFileInfo['mpeg']['video']['framesize_horizontal'] = $ThisFileInfo['mpeg']['video']['raw']['framesize_horizontal'];
        $ThisFileInfo['mpeg']['video']['framesize_vertical'] = $ThisFileInfo['mpeg']['video']['raw']['framesize_vertical'];
        $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio'] = MPEGvideoAspectRatioLookup($ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
        $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio_text'] = MPEGvideoAspectRatioTextLookup($ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
        $ThisFileInfo['mpeg']['video']['frame_rate'] = MPEGvideoFramerateLookup($ThisFileInfo['mpeg']['video']['raw']['frame_rate']);
        $ThisFileInfo['mpeg']['video']['raw']['bitrate'] = Bin2Dec(substr($assortedinformation, 0, 18));
        $ThisFileInfo['mpeg']['video']['raw']['marker_bit'] = Bin2Dec(substr($assortedinformation, 18, 1));
        $ThisFileInfo['mpeg']['video']['raw']['vbv_buffer_size'] = Bin2Dec(substr($assortedinformation, 19, 10));
        $ThisFileInfo['mpeg']['video']['raw']['constrained_param_flag'] = Bin2Dec(substr($assortedinformation, 29, 1));
        $ThisFileInfo['mpeg']['video']['raw']['intra_quant_flag'] = Bin2Dec(substr($assortedinformation, 30, 1));
        if ($ThisFileInfo['mpeg']['video']['raw']['bitrate'] == 0x3ffff) {
            // 18 set bits
            $ThisFileInfo['warning'] .= "\n" . 'This version of getID3() [' . GETID3VERSION . '] cannot determine average bitrate of VBR MPEG video files';
            $ThisFileInfo['mpeg']['video']['bitrate_mode'] = 'vbr';
        } else {
            $ThisFileInfo['mpeg']['video']['bitrate'] = $ThisFileInfo['mpeg']['video']['raw']['bitrate'] * 400;
            $ThisFileInfo['mpeg']['video']['bitrate_mode'] = 'cbr';
            $ThisFileInfo['video']['bitrate'] = $ThisFileInfo['mpeg']['video']['bitrate'];
        }
        $ThisFileInfo['video']['resolution_x'] = $ThisFileInfo['mpeg']['video']['framesize_horizontal'];
        $ThisFileInfo['video']['resolution_y'] = $ThisFileInfo['mpeg']['video']['framesize_vertical'];
        $ThisFileInfo['video']['frame_rate'] = $ThisFileInfo['mpeg']['video']['frame_rate'];
        $ThisFileInfo['video']['bitrate_mode'] = $ThisFileInfo['mpeg']['video']['bitrate_mode'];
        $ThisFileInfo['video']['lossless'] = false;
        $ThisFileInfo['video']['bits_per_sample'] = 24;
    } else {
        $ThisFileInfo['error'] .= "\n" . 'Could not find start of video block in the first 100,000 bytes (or before end of file) - this might not be an MPEG-video file?';
    }
    $AudioChunkOffset = 0;
    while (true) {
        while (substr($MPEGstreamData, $AudioChunkOffset++, 4) !== MPEG_AUDIO_START) {
            if ($AudioChunkOffset >= $MPEGstreamDataLength) {
                break 2;
            }
        }
        require_once GETID3_INCLUDEPATH . 'getid3.mp3.php';
        for ($i = 0; $i <= 2; $i++) {
            // some files have the MPEG-audio header 8 bytes after the end of the $00 $00 $01 $C0 signature, some have it 9 bytes, some 10 bytes after
            // I have no idea why or what the difference is, so this is a stupid hack.
            // If anybody has any better idea of what's going on, please let me know - info@getid3.org
            $dummy = $ThisFileInfo;
            if (decodeMPEGaudioHeader($fd, $AudioChunkOffset + 3 + 8 + $i, $dummy, false)) {
                $ThisFileInfo = $dummy;
                $ThisFileInfo['audio']['bits_per_sample'] = 16;
                $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
                $ThisFileInfo['audio']['lossless'] = false;
                break 2;
            }
        }
    }
    return true;
}