download getid3.mpeg.php
Language: PHP
LOC: 120
Project Info
Tunez
Server: SourceForge
Type: cvs
...tunez\tunez\tunez\html\id3\
   getid3.aac.php
   getid3.ape.php
   getid3.asf.php
   getid3.au.php
   getid3.avr.php
   getid3.bmp.php
   getid3.bonk.php
   getid3.check.php
   getid3.exe.php
   getid3.flac.php
   getid3.frames.php
   getid3.functions.php
   getid3.getimagesize.php
   getid3.gif.php
   getid3.id3v1.php
   getid3.id3v2.php
   getid3.iso.php
   getid3.jpg.php
   getid3.la.php
   getid3.lookup.php
   getid3.lpac.php
   getid3.lyrics3.php
   getid3.matroska.php
   getid3.midi.php
   getid3.mod.php
   getid3.monkey.php
   getid3.mp3.php
   getid3.mpc.php
   getid3.mpeg.php
   getid3.nsv.php
   getid3.ogg.php
   getid3.ogginfo.php
   getid3.optimfrog.php
   getid3.php
   getid3.png.php
   getid3.putid3.php
   getid3.quicktime.php
   getid3.rar.php
   getid3.real.php
   getid3.rgad.php
   getid3.riff.php
   getid3.shorten.php
   getid3.swf.php
   getid3.thumbnail.php
   getid3.voc.php
   getid3.vqf.php
   getid3.write.php
   getid3.zip.php

<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org>               //
//  available at http://getid3.sourceforge.net                ///
//            or http://www.getid3.org                        ///
/////////////////////////////////////////////////////////////////
//                                                             //
// getid3.mpeg.php - part of getID3()                          //
// See getid3.readme.txt for more details                      //
//                                                             //
/////////////////////////////////////////////////////////////////

define('MPEG_VIDEO_PICTURE_START',   "\x00\x00\x01\x00");
//define('MPEG_VIDEO_SLICE_START',   "\x00\x00\x01\x01" .. 00\x00\x01\xAF);
//define('MPEG_VIDEO_RESERVED1',     "\x00\x00\x01\xB0");
//define('MPEG_VIDEO_RESERVED2',     "\x00\x00\x01\xB1");
define('MPEG_VIDEO_USER_DATA_START', "\x00\x00\x01\xB2");
define('MPEG_VIDEO_SEQUENCE_HEADER', "\x00\x00\x01\xB3");
define('MPEG_VIDEO_SEQUENCE_ERROR',  "\x00\x00\x01\xB4");
define('MPEG_VIDEO_EXTENSION_START', "\x00\x00\x01\xB5");
//define('MPEG_VIDEO_RESERVED3',     "\x00\x00\x01\xB6");
define('MPEG_VIDEO_SEQUENCE_END',    "\x00\x00\x01\xB7");
define('MPEG_VIDEO_GROUP_START',     "\x00\x00\x01\xB8");
//define('MPEG_VIDEO_SYSTEM_START',  "\x00\x00\x01\xB9" .. \x00\x00\x01\xFF);
define('MPEG_AUDIO_START',           "\x00\x00\x01\xC0");

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;
		}
	}
	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 & 0x000FFF);       // 12 bits for vertical frame size
		$ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']   = ($AspectRatioFrameRateDWORD & 0xF0) >> 4;
		$ThisFileInfo['mpeg']['video']['raw']['frame_rate']           = ($AspectRatioFrameRateDWORD & 0x0F);

		$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']['pixel_aspect_ratio'] = $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio'];
		$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 <= 7; $i++) {
			// some files have the MPEG-audio header 8 bytes after the end of the $00 $00 $01 $C0 signature, some have it up to 13 bytes (or more?) 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;

			}
		}

	}

	// Temporary hack to account for interleaving overhead:
	if (!empty($ThisFileInfo['video']['bitrate']) && !empty($ThisFileInfo['audio']['bitrate'])) {
		$ThisFileInfo['playtime_seconds'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['video']['bitrate'];
		switch ($ThisFileInfo['video']['bitrate']) {
			case('5000000'):
				$multiplier = 0.93292642112380355828048824319889;
				break;
			case('5500000'):
				$multiplier = 0.93582895375200989965359777343219;
				break;
			case('6000000'):
				$multiplier = 0.93796247714820932532911373859139;
				break;
			case('7000000'):
				$multiplier = 0.9413264083635103463010117778776;
				break;
			default:
				$multiplier = 1;
				break;
		}
		$ThisFileInfo['playtime_seconds'] *= $multiplier;
		$ThisFileInfo['warning'] .= "\n".'Interleaved MPEG audio/video playtime may be inaccurate. With current hack should be within a few seconds of accurate. Report to info@getid3.org if off by more than 10 seconds.';
	}

	return true;
}


function MPEGvideoFramerateLookup($rawframerate) {
	$MPEGvideoFramerateLookup = array(0, 23.976, 24, 25, 29.97, 30, 50, 59.94, 60);
	return (isset($MPEGvideoFramerateLookup[$rawframerate]) ? (float) $MPEGvideoFramerateLookup[$rawframerate] : (float) 0);
}

function MPEGvideoAspectRatioLookup($rawaspectratio) {
	$MPEGvideoAspectRatioLookup = array(0, 1, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157, 0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 0);
	return (isset($MPEGvideoAspectRatioLookup[$rawaspectratio]) ? (float) $MPEGvideoAspectRatioLookup[$rawaspectratio] : (float) 0);
}

function MPEGvideoAspectRatioTextLookup($rawaspectratio) {
	$MPEGvideoAspectRatioTextLookup = array('forbidden', 'square pixels', '0.6735', '16:9, 625 line, PAL', '0.7615', '0.8055', '16:9, 525 line, NTSC', '0.8935', '4:3, 625 line, PAL, CCIR601', '0.9815', '1.0255', '1.0695', '4:3, 525 line, NTSC, CCIR601', '1.1575', '1.2015', 'reserved');
	return (isset($MPEGvideoAspectRatioTextLookup[$rawaspectratio]) ? $MPEGvideoAspectRatioTextLookup[$rawaspectratio] : '');
}

?>

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us