Example #1
0
 public function write()
 {
     // remove existing apetag
     $this->remove();
     $engine = new getid3();
     $engine->filename = $this->filename;
     $engine->fp = fopen($this->filename, 'rb');
     $engine->include_module('tag.id3v1');
     $engine->include_module('tag.lyrics3');
     $tag = new getid3_id3v1($engine);
     $tag->Analyze();
     $tag = new getid3_lyrics3($engine);
     $tag->Analyze();
     $apetag = $this->generate_tag();
     if (!($fp = @fopen($this->filename, 'a+b'))) {
         throw new getid3_exception('Could not open a+b: ' . $this->filename);
     }
     // init: audio ends at eof
     $post_audio_offset = filesize($this->filename);
     // lyrics3 tag present
     if (@$engine->info['lyrics3']['tag_offset_start']) {
         // audio ends before lyrics3 tag
         $post_audio_offset = @$engine->info['lyrics3']['tag_offset_start'];
     } elseif (@$engine->info['id3v1']['tag_offset_start']) {
         // audio ends before id3v1 tag
         $post_audio_offset = $engine->info['id3v1']['tag_offset_start'];
     }
     // seek to end of audio data
     fseek($fp, $post_audio_offset, SEEK_SET);
     // save data after audio data
     $post_audio_data = '';
     if (filesize($this->filename) > $post_audio_offset) {
         $post_audio_data = fread($fp, filesize($this->filename) - $post_audio_offset);
     }
     // truncate file before start of new apetag
     fseek($fp, $post_audio_offset, SEEK_SET);
     ftruncate($fp, ftell($fp));
     // write new apetag
     fwrite($fp, $apetag, strlen($apetag));
     // rewrite data after audio
     if (!empty($post_audio_data)) {
         fwrite($fp, $post_audio_data, strlen($post_audio_data));
     }
     fclose($fp);
     clearstatcache();
     return true;
 }
 public static function Lyrics3LyricsTimestampParse(&$lyrics3_data)
 {
     $lyrics_array = explode("\r\n", $lyrics3_data['raw']['LYR']);
     foreach ($lyrics_array as $key => $lyric_line) {
         while (ereg('^(\\[[0-9]{2}:[0-9]{2}\\])', $lyric_line, $regs)) {
             $this_line_timestamps[] = getid3_lyrics3::Lyrics3Timestamp2Seconds($regs[0]);
             $lyric_line = str_replace($regs[0], '', $lyric_line);
         }
         $no_timestamp_lyrics_array[$key] = $lyric_line;
         if (@is_array($this_line_timestamps)) {
             sort($this_line_timestamps);
             foreach ($this_line_timestamps as $timestampkey => $timestamp) {
                 if (isset($lyrics3_data['synchedlyrics'][$timestamp])) {
                     // timestamps only have a 1-second resolution, it's possible that multiple lines
                     // could have the same timestamp, if so, append
                     $lyrics3_data['synchedlyrics'][$timestamp] .= "\r\n" . $lyric_line;
                 } else {
                     $lyrics3_data['synchedlyrics'][$timestamp] = $lyric_line;
                 }
             }
         }
         unset($this_line_timestamps);
         $regs = array();
     }
     $lyrics3_data['unsynchedlyrics'] = implode("\r\n", $no_timestamp_lyrics_array);
     if (isset($lyrics3_data['synchedlyrics']) && is_array($lyrics3_data['synchedlyrics'])) {
         ksort($lyrics3_data['synchedlyrics']);
     }
     return true;
 }
 public function remove()
 {
     $engine = new getid3();
     $engine->filename = $this->filename;
     $engine->fp = fopen($this->filename, 'rb');
     $engine->include_module('tag.lyrics3');
     $tag = new getid3_lyrics3($engine);
     $tag->Analyze();
     if (isset($engine->info['lyrics3']['tag_offset_start']) && isset($engine->info['lyrics3']['tag_offset_end'])) {
         if (!($fp = @fopen($this->filename, 'a+b'))) {
             throw new getid3_exception('Could not open a+b: ' . $this->filename);
         }
         // get data after tag
         fseek($fp, $engine->info['lyrics3']['tag_offset_end'], SEEK_SET);
         $data_after_lyrics3 = '';
         if (filesize($this->filename) > $engine->info['lyrics3']['tag_offset_end']) {
             $data_after_lyrics3 = fread($fp, filesize($this->filename) - $engine->info['lyrics3']['tag_offset_end']);
         }
         // truncate file before start of tag and seek to end
         ftruncate($fp, $engine->info['lyrics3']['tag_offset_start']);
         // rewrite data after tag
         if (!empty($data_after_lyrics3)) {
             fseek($fp, $engine->info['lyrics3']['tag_offset_start'], SEEK_SET);
             fwrite($fp, $data_after_lyrics3, strlen($data_after_lyrics3));
         }
         fclose($fp);
         clearstatcache();
     }
     // success when removing non-existant tag
     return true;
 }