/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// see readme.txt for more details //
/////////////////////////////////////////////////////////////////
// module.audio-video.quicktime.php //
// module for analyzing Quicktime and MP3-in-MP4 files //
// dependencies: module.audio.mp3.php //
// dependencies: module.tag.id3v2.php //
/////////////////////////////////////////////////////////////////
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.mp3.php', __FILE__, true);
getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true); // needed for ISO 639-2 language code lookup
class getid3_quicktime extends getid3_handler
public $ReturnAtomData = true;
public $ParseAllPossibleAtoms = false;
public function Analyze() {
$info = &$this->getid3->info;
$info['fileformat'] = 'quicktime';
$info['quicktime']['hinting'] = false;
$info['quicktime']['controller'] = 'standard'; // may be overridden if 'ctyp' atom is present
$this->fseek($info['avdataoffset']);
$atom_data_read_buffer_size = $info['php_memory_limit'] ? round($info['php_memory_limit'] / 4) : $this->getid3->option_fread_buffer_size * 1024; // set read buffer to 25% of PHP memory limit (if one is specified), otherwise use option_fread_buffer_size [default: 32MB]
while ($offset < $info['avdataend']) {
if (!getid3_lib::intValueSupported($offset)) {
$this->error('Unable to parse atom at offset '.$offset.' because beyond '.round(PHP_INT_MAX / 1073741824).'GB limit of PHP filesystem functions');
$AtomHeader = $this->fread(8);
$atomsize = getid3_lib::BigEndian2Int(substr($AtomHeader, 0, 4));
$atomname = substr($AtomHeader, 4, 4);
// 64-bit MOV patch by jlegateØktnc*com
$atomsize = getid3_lib::BigEndian2Int($this->fread(8));
if (($offset + $atomsize) > $info['avdataend']) {
$info['quicktime'][$atomname]['name'] = $atomname;
$info['quicktime'][$atomname]['size'] = $atomsize;
$info['quicktime'][$atomname]['offset'] = $offset;
$this->error('Atom at offset '.$offset.' claims to go beyond end-of-file (length: '.$atomsize.' bytes)');
// Furthermore, for historical reasons the list of atoms is optionally
// terminated by a 32-bit integer set to 0. If you are writing a program
// to read user data atoms, you should allow for the terminating 0.
$info['quicktime'][$atomname]['name'] = $atomname;
$info['quicktime'][$atomname]['size'] = $atomsize;
$info['quicktime'][$atomname]['offset'] = $offset;
$atomHierarchy = array();
$parsedAtomData = $this->QuicktimeParseAtom($atomname, $atomsize, $this->fread(min($atomsize, $atom_data_read_buffer_size)), $offset, $atomHierarchy, $this->ParseAllPossibleAtoms);
$parsedAtomData['name'] = $atomname;
$parsedAtomData['size'] = $atomsize;
$parsedAtomData['offset'] = $offset;
if (in_array($atomname, array('uuid'))) {
@$info['quicktime'][$atomname][] = $parsedAtomData;
$info['quicktime'][$atomname] = $parsedAtomData;
if (!empty($info['avdataend_tmp'])) {
// this value is assigned to a temp value and then erased because
// otherwise any atoms beyond the 'mdat' atom would not get parsed
$info['avdataend'] = $info['avdataend_tmp'];
unset($info['avdataend_tmp']);
if (!empty($info['quicktime']['comments']['chapters']) && is_array($info['quicktime']['comments']['chapters']) && (count($info['quicktime']['comments']['chapters']) > 0)) {
$durations = $this->quicktime_time_to_sample_table($info);
for ($i = 0; $i < count($info['quicktime']['comments']['chapters']); $i++) {
$bookmark['title'] = $info['quicktime']['comments']['chapters'][$i];
if (isset($durations[$i])) {
$bookmark['duration_sample'] = $durations[$i]['sample_duration'];
$bookmark['start_sample'] = $info['quicktime']['bookmarks'][($i - 1)]['start_sample'] + $info['quicktime']['bookmarks'][($i - 1)]['duration_sample'];
$bookmark['start_sample'] = 0;
if ($time_scale = $this->quicktime_bookmark_time_scale($info)) {
$bookmark['duration_seconds'] = $bookmark['duration_sample'] / $time_scale;
$bookmark['start_seconds'] = $bookmark['start_sample'] / $time_scale;
$info['quicktime']['bookmarks'][] = $bookmark;
if (isset($info['quicktime']['temp_meta_key_names'])) {
unset($info['quicktime']['temp_meta_key_names']);
if (!empty($info['quicktime']['comments']['location.ISO6709'])) {
// https://en.wikipedia.org/wiki/ISO_6709
foreach ($info['quicktime']['comments']['location.ISO6709'] as $ISO6709string) {
$ISO6709parsed = array('latitude'=>false, 'longitude'=>false, 'altitude'=>false);
if (preg_match('#^([\\+\\-])([0-9]{2}|[0-9]{4}|[0-9]{6})(\\.[0-9]+)?([\\+\\-])([0-9]{3}|[0-9]{5}|[0-9]{7})(\\.[0-9]+)?(([\\+\\-])([0-9]{3}|[0-9]{5}|[0-9]{7})(\\.[0-9]+)?)?/$#', $ISO6709string, $matches)) {
// phpcs:ignore PHPCompatibility.Lists.AssignmentOrder.Affected
@list($dummy, $lat_sign, $lat_deg, $lat_deg_dec, $lon_sign, $lon_deg, $lon_deg_dec, $dummy, $alt_sign, $alt_deg, $alt_deg_dec) = $matches;
if (strlen($lat_deg) == 2) { // [+-]DD.D
$ISO6709parsed['latitude'] = (($lat_sign == '-') ? -1 : 1) * floatval(ltrim($lat_deg, '0').$lat_deg_dec);
} elseif (strlen($lat_deg) == 4) { // [+-]DDMM.M
$ISO6709parsed['latitude'] = (($lat_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lat_deg, 0, 2), '0')) + floatval(ltrim(substr($lat_deg, 2, 2), '0').$lat_deg_dec / 60);
} elseif (strlen($lat_deg) == 6) { // [+-]DDMMSS.S
$ISO6709parsed['latitude'] = (($lat_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lat_deg, 0, 2), '0')) + floatval(ltrim(substr($lat_deg, 2, 2), '0') / 60) + floatval(ltrim(substr($lat_deg, 4, 2), '0').$lat_deg_dec / 3600);
if (strlen($lon_deg) == 3) { // [+-]DDD.D
$ISO6709parsed['longitude'] = (($lon_sign == '-') ? -1 : 1) * floatval(ltrim($lon_deg, '0').$lon_deg_dec);
} elseif (strlen($lon_deg) == 5) { // [+-]DDDMM.M
$ISO6709parsed['longitude'] = (($lon_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lon_deg, 0, 2), '0')) + floatval(ltrim(substr($lon_deg, 2, 2), '0').$lon_deg_dec / 60);
} elseif (strlen($lon_deg) == 7) { // [+-]DDDMMSS.S
$ISO6709parsed['longitude'] = (($lon_sign == '-') ? -1 : 1) * floatval(ltrim(substr($lon_deg, 0, 2), '0')) + floatval(ltrim(substr($lon_deg, 2, 2), '0') / 60) + floatval(ltrim(substr($lon_deg, 4, 2), '0').$lon_deg_dec / 3600);
if (strlen($alt_deg) == 3) { // [+-]DDD.D
$ISO6709parsed['altitude'] = (($alt_sign == '-') ? -1 : 1) * floatval(ltrim($alt_deg, '0').$alt_deg_dec);
} elseif (strlen($alt_deg) == 5) { // [+-]DDDMM.M
$ISO6709parsed['altitude'] = (($alt_sign == '-') ? -1 : 1) * floatval(ltrim(substr($alt_deg, 0, 2), '0')) + floatval(ltrim(substr($alt_deg, 2, 2), '0').$alt_deg_dec / 60);
} elseif (strlen($alt_deg) == 7) { // [+-]DDDMMSS.S
$ISO6709parsed['altitude'] = (($alt_sign == '-') ? -1 : 1) * floatval(ltrim(substr($alt_deg, 0, 2), '0')) + floatval(ltrim(substr($alt_deg, 2, 2), '0') / 60) + floatval(ltrim(substr($alt_deg, 4, 2), '0').$alt_deg_dec / 3600);
foreach (array('latitude', 'longitude', 'altitude') as $key) {
if ($ISO6709parsed[$key] !== false) {
$value = (($lat_sign == '-') ? -1 : 1) * floatval($ISO6709parsed[$key]);
if (!isset($info['quicktime']['comments']['gps_'.$key]) || !in_array($value, $info['quicktime']['comments']['gps_'.$key])) {
@$info['quicktime']['comments']['gps_'.$key][] = (($lat_sign == '-') ? -1 : 1) * floatval($ISO6709parsed[$key]);
if ($ISO6709parsed['latitude'] === false) {
$this->warning('location.ISO6709 string not parsed correctly: "'.$ISO6709string.'", please submit as a bug');
if (!isset($info['bitrate']) && isset($info['playtime_seconds'])) {
$info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
if (isset($info['bitrate']) && !isset($info['audio']['bitrate']) && !isset($info['quicktime']['video'])) {
$info['audio']['bitrate'] = $info['bitrate'];
if (!empty($info['bitrate']) && !empty($info['audio']['bitrate']) && empty($info['video']['bitrate']) && !empty($info['video']['frame_rate']) && !empty($info['video']['resolution_x']) && ($info['bitrate'] > $info['audio']['bitrate'])) {
$info['video']['bitrate'] = $info['bitrate'] - $info['audio']['bitrate'];
if (!empty($info['playtime_seconds']) && !isset($info['video']['frame_rate']) && !empty($info['quicktime']['stts_framecount'])) {
foreach ($info['quicktime']['stts_framecount'] as $key => $samples_count) {
$samples_per_second = $samples_count / $info['playtime_seconds'];
if ($samples_per_second > 240) {
// has to be audio samples
$info['video']['frame_rate'] = $samples_per_second;
if ($info['audio']['dataformat'] == 'mp4') {
$info['fileformat'] = 'mp4';
if (empty($info['video']['resolution_x'])) {
$info['mime_type'] = 'audio/mp4';
unset($info['video']['dataformat']);
$info['mime_type'] = 'video/mp4';
if (!$this->ReturnAtomData) {
unset($info['quicktime']['moov']);
if (empty($info['audio']['dataformat']) && !empty($info['quicktime']['audio'])) {
$info['audio']['dataformat'] = 'quicktime';
if (empty($info['video']['dataformat']) && !empty($info['quicktime']['video'])) {
$info['video']['dataformat'] = 'quicktime';
if (isset($info['video']) && ($info['mime_type'] == 'audio/mp4') && empty($info['video']['resolution_x']) && empty($info['video']['resolution_y'])) {
* @param string $atomname
* @param string $atom_data
* @param array $atomHierarchy
* @param bool $ParseAllPossibleAtoms
public function QuicktimeParseAtom($atomname, $atomsize, $atom_data, $baseoffset, &$atomHierarchy, $ParseAllPossibleAtoms) {
// http://developer.apple.com/techpubs/quicktime/qtdevdocs/APIREF/INDEX/atomalphaindex.htm
// https://code.google.com/p/mp4v2/wiki/iTunesMetadata
$info = &$this->getid3->info;
$atom_parent = end($atomHierarchy); // not array_pop($atomHierarchy); see https://www.getid3.org/phpBB3/viewtopic.php?t=1717
array_push($atomHierarchy, $atomname);
$atom_structure = array();
$atom_structure['hierarchy'] = implode(' ', $atomHierarchy);
$atom_structure['name'] = $atomname;
$atom_structure['size'] = $atomsize;
$atom_structure['offset'] = $baseoffset;
if (substr($atomname, 0, 3) == "\x00\x00\x00") {
// https://github.com/JamesHeinrich/getID3/issues/139
$atomname = getid3_lib::BigEndian2Int($atomname);
$atom_structure['name'] = $atomname;
$atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms);
case 'moov': // MOVie container atom
case 'trak': // TRAcK container atom
case 'clip': // CLIPping container atom
case 'matt': // track MATTe container atom
case 'edts': // EDiTS container atom
case 'tref': // Track REFerence container atom
case 'mdia': // MeDIA container atom
case 'minf': // Media INFormation container atom
case 'dinf': // Data INFormation container atom
case 'nmhd': // Null Media HeaDer container atom
case 'udta': // User DaTA container atom
case 'cmov': // Compressed MOVie container atom
case 'rmra': // Reference Movie Record Atom
case 'rmda': // Reference Movie Descriptor Atom
case 'gmhd': // Generic Media info HeaDer atom (seen on QTVR)
$atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms);
case 'ilst': // Item LiST container atom
if ($atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms)) {
// some "ilst" atoms contain data atoms that have a numeric name, and the data is far more accessible if the returned array is compacted
foreach ($atom_structure['subatoms'] as $subatomarray) {
if (!is_integer($subatomarray['name']) || (count($subatomarray['subatoms']) != 1)) {
$allnumericnames = false;
foreach ($atom_structure['subatoms'] as $subatomarray) {
foreach ($subatomarray['subatoms'] as $newData_subatomarray) {
unset($newData_subatomarray['hierarchy'], $newData_subatomarray['name']);
$newData[$subatomarray['name']] = $newData_subatomarray;
$atom_structure['data'] = $newData;
unset($atom_structure['subatoms']);
case 'stbl': // Sample TaBLe container atom
$atom_structure['subatoms'] = $this->QuicktimeParseContainerAtom($atom_data, $baseoffset + 8, $atomHierarchy, $ParseAllPossibleAtoms);
foreach ($atom_structure['subatoms'] as $key => $value_array) {
if (isset($value_array['sample_description_table'])) {
foreach ($value_array['sample_description_table'] as $key2 => $value_array2) {
if (isset($value_array2['data_format'])) {
switch ($value_array2['data_format']) {
} elseif (isset($value_array['time_to_sample_table'])) {
foreach ($value_array['time_to_sample_table'] as $key2 => $value_array2) {
if (isset($value_array2['sample_count']) && isset($value_array2['sample_duration']) && ($value_array2['sample_duration'] > 0)) {
$framerate = round($info['quicktime']['time_scale'] / $value_array2['sample_duration'], 3);
$framecount = $value_array2['sample_count'];
if ($isVideo && $framerate) {
$info['quicktime']['video']['frame_rate'] = $framerate;
$info['video']['frame_rate'] = $info['quicktime']['video']['frame_rate'];
if ($isVideo && $framecount) {
$info['quicktime']['video']['frame_count'] = $framecount;
case "\xA9".'alb': // ALBum
case "\xA9".'art': // ARTist
case "\xA9".'cmt': // CoMmenT
case "\xA9".'com': // COMposer
case "\xA9".'day': // content created year
case "\xA9".'gen': // GENre
case "\xA9".'grp': // GRouPing
case "\xA9".'lyr': // LYRics
case "\xA9".'nam': // full NAMe
case "\xA9".'too': // encoder
case "\xA9".'trk': // TRacK
case "\xA9".'wrt': // WRiTer
case '----': // itunes specific
case 'aART': // Album ARTist
case 'akID': // iTunes store account type
case 'apID': // Purchase Account
case 'covr': // COVeR artwork
case 'cpil': // ComPILation
case 'cprt': // CoPyRighT
case 'desc': // DESCription
case 'disk': // DISK number
case 'egid': // Episode Global ID
case 'ldes': // Long DEScription
case 'pgap': // GAPless Playback
case 'purd': // PURchase Date
case 'purl': // Podcast URL
case 'sfID': // iTunes store country
case 'soaa': // SOrt Album Artist
case 'soal': // SOrt ALbum
case 'soar': // SOrt ARtist
case 'soco': // SOrt COmposer
case 'sonm': // SOrt NaMe
case 'sosn': // SOrt Show Name
case 'tmpo': // TeMPO (BPM)
case 'trkn': // TRacK Number
case 'tven': // tvEpisodeID
case 'tves': // TV EpiSode
case 'tvnn': // TV Network Name
case 'tvsh': // TV SHow Name
case 'tvsn': // TV SeasoN
if ($atom_parent == 'udta') {
// User data atom handler
$atom_structure['data_length'] = getid3_lib::BigEndian2Int(substr($atom_data, 0, 2));
$atom_structure['language_id'] = getid3_lib::BigEndian2Int(substr($atom_data, 2, 2));
$atom_structure['data'] = substr($atom_data, 4);
$atom_structure['language'] = $this->QuicktimeLanguageLookup($atom_structure['language_id']);
if (empty($info['comments']['language']) || (!in_array($atom_structure['language'], $info['comments']['language']))) {
$info['comments']['language'][] = $atom_structure['language'];
// Apple item list box atom handler
if (substr($atom_data, 2, 2) == "\x10\xB5") {
// not sure what it means, but observed on iPhone4 data.
// Each $atom_data has 2 bytes of datasize, plus 0x10B5, then data
while ($atomoffset < strlen($atom_data)) {
$boxsmallsize = getid3_lib::BigEndian2Int(substr($atom_data, $atomoffset, 2));
$boxsmalltype = substr($atom_data, $atomoffset + 2, 2);
$boxsmalldata = substr($atom_data, $atomoffset + 4, $boxsmallsize);
if ($boxsmallsize <= 1) {
$this->warning('Invalid QuickTime atom smallbox size "'.$boxsmallsize.'" in atom "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $atomname).'" at offset: '.($atom_structure['offset'] + $atomoffset));
$atom_structure['data'] = null;
$atomoffset = strlen($atom_data);
$atom_structure['data'] = $boxsmalldata;
$this->warning('Unknown QuickTime smallbox type: "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $boxsmalltype).'" ('.trim(getid3_lib::PrintHexBytes($boxsmalltype)).') at offset '.$baseoffset);
$atom_structure['data'] = $atom_data;
$atomoffset += (4 + $boxsmallsize);
while ($atomoffset < strlen($atom_data)) {
$boxsize = getid3_lib::BigEndian2Int(substr($atom_data, $atomoffset, 4));
$boxtype = substr($atom_data, $atomoffset + 4, 4);
$boxdata = substr($atom_data, $atomoffset + 8, $boxsize - 8);
$this->warning('Invalid QuickTime atom box size "'.$boxsize.'" in atom "'.preg_replace('#[^a-zA-Z0-9 _\\-]#', '?', $atomname).'" at offset: '.($atom_structure['offset'] + $atomoffset));
$atom_structure['data'] = null;
$atomoffset = strlen($atom_data);
$atom_structure[$boxtype] = substr($boxdata, 4);
$atom_structure['version'] = getid3_lib::BigEndian2Int(substr($boxdata, 0, 1));
$atom_structure['flags_raw'] = getid3_lib::BigEndian2Int(substr($boxdata, 1, 3));
switch ($atom_structure['flags_raw']) {
case 21: // tmpo/cpil flag
// 8-bit integer (boolean)
$atom_structure['data'] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 1));
$atom_structure['data'] = getid3_lib::BigEndian2Int(substr($boxdata, 8, 2));
$num = getid3_lib::BigEndian2Int(substr($boxdata, 10, 2));
$num_total = getid3_lib::BigEndian2Int(substr($boxdata, 12, 2));
$atom_structure['data'] = empty($num) ? '' : $num;
$atom_structure['data'] .= empty($num_total) ? '' : '/'.$num_total;