public function WriteMetaFLAC() { // Create file with new comments $tempcommentsfilename = tempnam(Utils::getTempDirectory(), 'getID3'); if (is_writable($tempcommentsfilename) && is_file($tempcommentsfilename) && ($fpcomments = fopen($tempcommentsfilename, 'wb'))) { foreach ($this->tag_data as $key => $value) { foreach ($value as $commentdata) { fwrite($fpcomments, $this->CleanmetaflacName($key) . '=' . $commentdata . "\n"); } } fclose($fpcomments); } else { $this->errors[] = 'failed to open temporary tags file, tags not written - fopen("' . $tempcommentsfilename . '", "wb")'; return false; } $oldignoreuserabort = ignore_user_abort(true); if (Utils::isWindows()) { if (file_exists(Utils::getHelperAppDirectory() . 'metaflac.exe')) { //$commandline = '"' . Utils::getHelperAppDirectory() . 'metaflac.exe" --no-utf8-convert --remove-all-tags --import-tags-from="' . $tempcommentsfilename . '" "' . str_replace('/', '\\', $this->filename) . '"'; // metaflac works fine if you copy-paste the above commandline into a command prompt, // but refuses to work with `backtick` if there are "doublequotes" present around BOTH // the metaflac pathname and the target filename. For whatever reason...?? // The solution is simply ensure that the metaflac pathname has no spaces, // and therefore does not need to be quoted // On top of that, if error messages are not always captured properly under Windows // To at least see if there was a problem, compare file modification timestamps before and after writing clearstatcache(); $timestampbeforewriting = filemtime($this->filename); $commandline = Utils::getHelperAppDirectory() . 'metaflac.exe --no-utf8-convert --remove-all-tags --import-tags-from=' . escapeshellarg($tempcommentsfilename) . ' ' . escapeshellarg($this->filename) . ' 2>&1'; $metaflacError = `{$commandline}`; if (empty($metaflacError)) { clearstatcache(); if ($timestampbeforewriting == filemtime($this->filename)) { $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not written'; } } } else { $metaflacError = 'metaflac.exe not found in ' . Utils::getHelperAppDirectory(); } } else { // It's simpler on *nix $commandline = 'metaflac --no-utf8-convert --remove-all-tags --import-tags-from=' . escapeshellarg($tempcommentsfilename) . ' ' . escapeshellarg($this->filename) . ' 2>&1'; $metaflacError = `{$commandline}`; } // Remove temporary comments file unlink($tempcommentsfilename); ignore_user_abort($oldignoreuserabort); if (!empty($metaflacError)) { $this->errors[] = 'System call to metaflac failed with this message returned: ' . "\n\n" . $metaflacError; return false; } return true; }
public function ParseRIFFdata(&$RIFFdata) { $info =& $this->getid3->info; if ($RIFFdata) { $tempfile = tempnam(Utils::getTempDirectory(), 'getID3'); $fp_temp = fopen($tempfile, 'wb'); $RIFFdataLength = strlen($RIFFdata); $NewLengthString = Utils::LittleEndian2String($RIFFdataLength, 4); for ($i = 0; $i < 4; $i++) { $RIFFdata[$i + 4] = $NewLengthString[$i]; } fwrite($fp_temp, $RIFFdata); fclose($fp_temp); $getid3_temp = new GetID3(); $getid3_temp->openfile($tempfile); $getid3_temp->info['filesize'] = $RIFFdataLength; $getid3_temp->info['filenamepath'] = $info['filenamepath']; $getid3_temp->info['tags'] = $info['tags']; $getid3_temp->info['warning'] = $info['warning']; $getid3_temp->info['error'] = $info['error']; $getid3_temp->info['comments'] = $info['comments']; $getid3_temp->info['audio'] = isset($info['audio']) ? $info['audio'] : array(); $getid3_temp->info['video'] = isset($info['video']) ? $info['video'] : array(); $getid3_riff = new Riff($getid3_temp); $getid3_riff->Analyze(); $info['riff'] = $getid3_temp->info['riff']; $info['warning'] = $getid3_temp->info['warning']; $info['error'] = $getid3_temp->info['error']; $info['tags'] = $getid3_temp->info['tags']; $info['comments'] = $getid3_temp->info['comments']; unset($getid3_riff, $getid3_temp); unlink($tempfile); } return false; }
public function RemoveReal() { // File MUST be writeable - CHMOD(646) at least if (is_writeable($this->filename) && is_file($this->filename) && ($fp_source = fopen($this->filename, 'r+b'))) { // Initialize getID3 engine $getID3 = new GetID3(); $OldThisFileInfo = $getID3->analyze($this->filename); if (empty($OldThisFileInfo['real']['chunks']) && !empty($OldThisFileInfo['real']['old_ra_header'])) { $this->errors[] = 'Cannot remove Real tags from old-style file format'; fclose($fp_source); return false; } if (empty($OldThisFileInfo['real']['chunks'])) { $this->errors[] = 'Cannot remove Real tags because cannot find DATA chunk in file'; fclose($fp_source); return false; } foreach ($OldThisFileInfo['real']['chunks'] as $chunknumber => $chunkarray) { $oldChunkInfo[$chunkarray['name']] = $chunkarray; } if (empty($oldChunkInfo['CONT'])) { // no existing CONT chunk fclose($fp_source); return true; } $BeforeOffset = $oldChunkInfo['CONT']['offset']; $AfterOffset = $oldChunkInfo['CONT']['offset'] + $oldChunkInfo['CONT']['length']; if ($tempfilename = tempnam(Utils::getTempDirectory(), 'getID3')) { if (is_writable($tempfilename) && is_file($tempfilename) && ($fp_temp = fopen($tempfilename, 'wb'))) { rewind($fp_source); fwrite($fp_temp, fread($fp_source, $BeforeOffset)); fseek($fp_source, $AfterOffset); while ($buffer = fread($fp_source, $this->fread_buffer_size)) { fwrite($fp_temp, $buffer, strlen($buffer)); } fclose($fp_temp); if (copy($tempfilename, $this->filename)) { unlink($tempfilename); fclose($fp_source); return true; } unlink($tempfilename); $this->errors[] = 'FAILED: copy(' . $tempfilename . ', ' . $this->filename . ')'; } else { $this->errors[] = 'Could not fopen("' . $tempfilename . '", "wb")'; } } fclose($fp_source); return false; } $this->errors[] = 'Could not fopen("' . $this->filename . '", "r+b")'; return false; }
public function Analyze() { $info =& $this->getid3->info; $offset = 0; $this->fseek($info['avdataoffset']); $rawdata = $this->fread($this->getid3->fread_buffer_size()); switch (substr($rawdata, $offset, 4)) { case 'LA02': case 'LA03': case 'LA04': $info['fileformat'] = 'la'; $info['audio']['dataformat'] = 'la'; $info['audio']['lossless'] = true; $info['la']['version_major'] = (int) substr($rawdata, $offset + 2, 1); $info['la']['version_minor'] = (int) substr($rawdata, $offset + 3, 1); $info['la']['version'] = (double) $info['la']['version_major'] + $info['la']['version_minor'] / 10; $offset += 4; $info['la']['uncompressed_size'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; if ($info['la']['uncompressed_size'] == 0) { $info['error'][] = 'Corrupt LA file: uncompressed_size == zero'; return false; } $WAVEchunk = substr($rawdata, $offset, 4); if ($WAVEchunk !== 'WAVE') { $info['error'][] = 'Expected "WAVE" (' . Utils::PrintHexBytes('WAVE') . ') at offset ' . $offset . ', found "' . $WAVEchunk . '" (' . Utils::PrintHexBytes($WAVEchunk) . ') instead.'; return false; } $offset += 4; $info['la']['fmt_size'] = 24; if ($info['la']['version'] >= 0.3) { $info['la']['fmt_size'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $info['la']['header_size'] = 49 + $info['la']['fmt_size'] - 24; $offset += 4; } else { // version 0.2 didn't support additional data blocks $info['la']['header_size'] = 41; } $fmt_chunk = substr($rawdata, $offset, 4); if ($fmt_chunk !== 'fmt ') { $info['error'][] = 'Expected "fmt " (' . Utils::PrintHexBytes('fmt ') . ') at offset ' . $offset . ', found "' . $fmt_chunk . '" (' . Utils::PrintHexBytes($fmt_chunk) . ') instead.'; return false; } $offset += 4; $fmt_size = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; $info['la']['raw']['format'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 2)); $offset += 2; $info['la']['channels'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 2)); $offset += 2; if ($info['la']['channels'] == 0) { $info['error'][] = 'Corrupt LA file: channels == zero'; return false; } $info['la']['sample_rate'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; if ($info['la']['sample_rate'] == 0) { $info['error'][] = 'Corrupt LA file: sample_rate == zero'; return false; } $info['la']['bytes_per_second'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; $info['la']['bytes_per_sample'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 2)); $offset += 2; $info['la']['bits_per_sample'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 2)); $offset += 2; $info['la']['samples'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; $info['la']['raw']['flags'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 1)); $offset += 1; $info['la']['flags']['seekable'] = (bool) ($info['la']['raw']['flags'] & 0x1); if ($info['la']['version'] >= 0.4) { $info['la']['flags']['high_compression'] = (bool) ($info['la']['raw']['flags'] & 0x2); } $info['la']['original_crc'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; // mikeØbevin*de // Basically, the blocksize/seekevery are 61440/19 in La0.4 and 73728/16 // in earlier versions. A seekpoint is added every blocksize * seekevery // samples, so 4 * int(totalSamples / (blockSize * seekEvery)) should // give the number of bytes used for the seekpoints. Of course, if seeking // is disabled, there are no seekpoints stored. if ($info['la']['version'] >= 0.4) { $info['la']['blocksize'] = 61440; $info['la']['seekevery'] = 19; } else { $info['la']['blocksize'] = 73728; $info['la']['seekevery'] = 16; } $info['la']['seekpoint_count'] = 0; if ($info['la']['flags']['seekable']) { $info['la']['seekpoint_count'] = floor($info['la']['samples'] / ($info['la']['blocksize'] * $info['la']['seekevery'])); for ($i = 0; $i < $info['la']['seekpoint_count']; $i++) { $info['la']['seekpoints'][] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; } } if ($info['la']['version'] >= 0.3) { // Following the main header information, the program outputs all of the // seekpoints. Following these is what I called the 'footer start', // i.e. the position immediately after the La audio data is finished. $info['la']['footerstart'] = Utils::LittleEndian2Int(substr($rawdata, $offset, 4)); $offset += 4; if ($info['la']['footerstart'] > $info['filesize']) { $info['warning'][] = 'FooterStart value points to offset ' . $info['la']['footerstart'] . ' which is beyond end-of-file (' . $info['filesize'] . ')'; $info['la']['footerstart'] = $info['filesize']; } } else { // La v0.2 didn't have FooterStart value $info['la']['footerstart'] = $info['avdataend']; } if ($info['la']['footerstart'] < $info['avdataend']) { if ($RIFFtempfilename = tempnam(Utils::getTempDirectory(), 'id3')) { if ($RIFF_fp = fopen($RIFFtempfilename, 'w+b')) { $RIFFdata = 'WAVE'; if ($info['la']['version'] == 0.2) { $RIFFdata .= substr($rawdata, 12, 24); } else { $RIFFdata .= substr($rawdata, 16, 24); } if ($info['la']['footerstart'] < $info['avdataend']) { $this->fseek($info['la']['footerstart']); $RIFFdata .= $this->fread($info['avdataend'] - $info['la']['footerstart']); } $RIFFdata = 'RIFF' . Utils::LittleEndian2String(strlen($RIFFdata), 4, false) . $RIFFdata; fwrite($RIFF_fp, $RIFFdata, strlen($RIFFdata)); fclose($RIFF_fp); $getid3_temp = new GetID3(); $getid3_temp->openfile($RIFFtempfilename); $getid3_riff = new Riff($getid3_temp); $getid3_riff->Analyze(); if (empty($getid3_temp->info['error'])) { $info['riff'] = $getid3_temp->info['riff']; } else { $info['warning'][] = 'Error parsing RIFF portion of La file: ' . implode($getid3_temp->info['error']); } unset($getid3_temp, $getid3_riff); } unlink($RIFFtempfilename); } } // $info['avdataoffset'] should be zero to begin with, but just in case it's not, include the addition anyway $info['avdataend'] = $info['avdataoffset'] + $info['la']['footerstart']; $info['avdataoffset'] = $info['avdataoffset'] + $offset; $info['la']['compression_ratio'] = (double) (($info['avdataend'] - $info['avdataoffset']) / $info['la']['uncompressed_size']); $info['playtime_seconds'] = (double) ($info['la']['samples'] / $info['la']['sample_rate']) / $info['la']['channels']; if ($info['playtime_seconds'] == 0) { $info['error'][] = 'Corrupt LA file: playtime_seconds == zero'; return false; } $info['audio']['bitrate'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['playtime_seconds']; //$info['audio']['codec'] = $info['la']['codec']; $info['audio']['bits_per_sample'] = $info['la']['bits_per_sample']; break; default: if (substr($rawdata, $offset, 2) == 'LA') { $info['error'][] = 'This version of getID3() [' . $this->getid3->version() . '] does not support LA version ' . substr($rawdata, $offset + 2, 1) . '.' . substr($rawdata, $offset + 3, 1) . ' which this appears to be - check http://getid3.sourceforge.net for updates.'; } else { $info['error'][] = 'Not a LA (Lossless-Audio) file'; } return false; break; } $info['audio']['channels'] = $info['la']['channels']; $info['audio']['sample_rate'] = (int) $info['la']['sample_rate']; $info['audio']['encoder'] = 'LA v' . $info['la']['version']; return true; }