author | ymh <ymh.work@gmail.com> |
Wed, 16 Oct 2019 11:23:38 +0200 | |
changeset 14 | 00ac8f60d73f |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
///////////////////////////////////////////////////////////////// |
|
3 |
/// getID3() by James Heinrich <info@getid3.org> // |
|
4 |
// available at http://getid3.sourceforge.net // |
|
5 |
// or http://www.getid3.org // |
|
5 | 6 |
// also https://github.com/JamesHeinrich/getID3 // |
0 | 7 |
///////////////////////////////////////////////////////////////// |
8 |
// See readme.txt for more details // |
|
9 |
///////////////////////////////////////////////////////////////// |
|
10 |
/// // |
|
11 |
// module.tag.lyrics3.php // |
|
12 |
// module for analyzing Lyrics3 tags // |
|
13 |
// dependencies: module.tag.apetag.php (optional) // |
|
14 |
// /// |
|
15 |
///////////////////////////////////////////////////////////////// |
|
16 |
||
17 |
||
18 |
class getid3_lyrics3 extends getid3_handler |
|
19 |
{ |
|
20 |
||
21 |
public function Analyze() { |
|
22 |
$info = &$this->getid3->info; |
|
23 |
||
24 |
// http://www.volweb.cz/str/tags.htm |
|
25 |
||
26 |
if (!getid3_lib::intValueSupported($info['filesize'])) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
$this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); |
0 | 28 |
return false; |
29 |
} |
|
30 |
||
5 | 31 |
$this->fseek((0 - 128 - 9 - 6), SEEK_END); // end - ID3v1 - "LYRICSEND" - [Lyrics3size] |
32 |
$lyrics3_id3v1 = $this->fread(128 + 9 + 6); |
|
0 | 33 |
$lyrics3lsz = substr($lyrics3_id3v1, 0, 6); // Lyrics3size |
34 |
$lyrics3end = substr($lyrics3_id3v1, 6, 9); // LYRICSEND or LYRICS200 |
|
35 |
$id3v1tag = substr($lyrics3_id3v1, 15, 128); // ID3v1 |
|
36 |
||
37 |
if ($lyrics3end == 'LYRICSEND') { |
|
38 |
// Lyrics3v1, ID3v1, no APE |
|
39 |
||
40 |
$lyrics3size = 5100; |
|
41 |
$lyrics3offset = $info['filesize'] - 128 - $lyrics3size; |
|
42 |
$lyrics3version = 1; |
|
43 |
||
44 |
} elseif ($lyrics3end == 'LYRICS200') { |
|
45 |
// Lyrics3v2, ID3v1, no APE |
|
46 |
||
47 |
// LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' |
|
48 |
$lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200'); |
|
49 |
$lyrics3offset = $info['filesize'] - 128 - $lyrics3size; |
|
50 |
$lyrics3version = 2; |
|
51 |
||
52 |
} elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICSEND')) { |
|
53 |
// Lyrics3v1, no ID3v1, no APE |
|
54 |
||
55 |
$lyrics3size = 5100; |
|
56 |
$lyrics3offset = $info['filesize'] - $lyrics3size; |
|
57 |
$lyrics3version = 1; |
|
58 |
$lyrics3offset = $info['filesize'] - $lyrics3size; |
|
59 |
||
60 |
} elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICS200')) { |
|
61 |
||
62 |
// Lyrics3v2, no ID3v1, no APE |
|
63 |
||
64 |
$lyrics3size = strrev(substr(strrev($lyrics3_id3v1), 9, 6)) + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' |
|
65 |
$lyrics3offset = $info['filesize'] - $lyrics3size; |
|
66 |
$lyrics3version = 2; |
|
67 |
||
68 |
} else { |
|
69 |
||
70 |
if (isset($info['ape']['tag_offset_start']) && ($info['ape']['tag_offset_start'] > 15)) { |
|
71 |
||
5 | 72 |
$this->fseek($info['ape']['tag_offset_start'] - 15); |
73 |
$lyrics3lsz = $this->fread(6); |
|
74 |
$lyrics3end = $this->fread(9); |
|
0 | 75 |
|
76 |
if ($lyrics3end == 'LYRICSEND') { |
|
77 |
// Lyrics3v1, APE, maybe ID3v1 |
|
78 |
||
79 |
$lyrics3size = 5100; |
|
80 |
$lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size; |
|
81 |
$info['avdataend'] = $lyrics3offset; |
|
82 |
$lyrics3version = 1; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
$this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability'); |
0 | 84 |
|
85 |
} elseif ($lyrics3end == 'LYRICS200') { |
|
86 |
// Lyrics3v2, APE, maybe ID3v1 |
|
87 |
||
88 |
$lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' |
|
89 |
$lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size; |
|
90 |
$lyrics3version = 2; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
$this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability'); |
0 | 92 |
|
93 |
} |
|
94 |
||
95 |
} |
|
96 |
||
97 |
} |
|
98 |
||
99 |
if (isset($lyrics3offset)) { |
|
100 |
$info['avdataend'] = $lyrics3offset; |
|
101 |
$this->getLyrics3Data($lyrics3offset, $lyrics3version, $lyrics3size); |
|
102 |
||
103 |
if (!isset($info['ape'])) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
if (isset($info['lyrics3']['tag_offset_start'])) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
$GETID3_ERRORARRAY = &$info['warning']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.apetag.php', __FILE__, true); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
$getid3_temp = new getID3(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
$getid3_temp->openfile($this->getid3->filename); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
$getid3_apetag = new getid3_apetag($getid3_temp); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
$getid3_apetag->overrideendoffset = $info['lyrics3']['tag_offset_start']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
$getid3_apetag->Analyze(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
if (!empty($getid3_temp->info['ape'])) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
$info['ape'] = $getid3_temp->info['ape']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
if (!empty($getid3_temp->info['replay_gain'])) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
$info['replay_gain'] = $getid3_temp->info['replay_gain']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
unset($getid3_temp, $getid3_apetag); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
$this->warning('Lyrics3 and APE tags appear to have become entangled (most likely due to updating the APE tags with a non-Lyrics3-aware tagger)'); |
0 | 121 |
} |
122 |
} |
|
123 |
||
124 |
} |
|
125 |
||
126 |
return true; |
|
127 |
} |
|
128 |
||
129 |
public function getLyrics3Data($endoffset, $version, $length) { |
|
130 |
// http://www.volweb.cz/str/tags.htm |
|
131 |
||
132 |
$info = &$this->getid3->info; |
|
133 |
||
134 |
if (!getid3_lib::intValueSupported($endoffset)) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
$this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); |
0 | 136 |
return false; |
137 |
} |
|
138 |
||
5 | 139 |
$this->fseek($endoffset); |
0 | 140 |
if ($length <= 0) { |
141 |
return false; |
|
142 |
} |
|
5 | 143 |
$rawdata = $this->fread($length); |
0 | 144 |
|
145 |
$ParsedLyrics3['raw']['lyrics3version'] = $version; |
|
146 |
$ParsedLyrics3['raw']['lyrics3tagsize'] = $length; |
|
147 |
$ParsedLyrics3['tag_offset_start'] = $endoffset; |
|
148 |
$ParsedLyrics3['tag_offset_end'] = $endoffset + $length - 1; |
|
149 |
||
150 |
if (substr($rawdata, 0, 11) != 'LYRICSBEGIN') { |
|
151 |
if (strpos($rawdata, 'LYRICSBEGIN') !== false) { |
|
152 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
$this->warning('"LYRICSBEGIN" expected at '.$endoffset.' but actually found at '.($endoffset + strpos($rawdata, 'LYRICSBEGIN')).' - this is invalid for Lyrics3 v'.$version); |
0 | 154 |
$info['avdataend'] = $endoffset + strpos($rawdata, 'LYRICSBEGIN'); |
155 |
$rawdata = substr($rawdata, strpos($rawdata, 'LYRICSBEGIN')); |
|
156 |
$length = strlen($rawdata); |
|
157 |
$ParsedLyrics3['tag_offset_start'] = $info['avdataend']; |
|
158 |
$ParsedLyrics3['raw']['lyrics3tagsize'] = $length; |
|
159 |
||
160 |
} else { |
|
161 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
$this->error('"LYRICSBEGIN" expected at '.$endoffset.' but found "'.substr($rawdata, 0, 11).'" instead'); |
0 | 163 |
return false; |
164 |
||
165 |
} |
|
166 |
||
167 |
} |
|
168 |
||
169 |
switch ($version) { |
|
170 |
||
171 |
case 1: |
|
172 |
if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICSEND') { |
|
173 |
$ParsedLyrics3['raw']['LYR'] = trim(substr($rawdata, 11, strlen($rawdata) - 11 - 9)); |
|
174 |
$this->Lyrics3LyricsTimestampParse($ParsedLyrics3); |
|
175 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
$this->error('"LYRICSEND" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead'); |
0 | 177 |
return false; |
178 |
} |
|
179 |
break; |
|
180 |
||
181 |
case 2: |
|
182 |
if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICS200') { |
|
183 |
$ParsedLyrics3['raw']['unparsed'] = substr($rawdata, 11, strlen($rawdata) - 11 - 9 - 6); // LYRICSBEGIN + LYRICS200 + LSZ |
|
184 |
$rawdata = $ParsedLyrics3['raw']['unparsed']; |
|
185 |
while (strlen($rawdata) > 0) { |
|
186 |
$fieldname = substr($rawdata, 0, 3); |
|
187 |
$fieldsize = (int) substr($rawdata, 3, 5); |
|
188 |
$ParsedLyrics3['raw'][$fieldname] = substr($rawdata, 8, $fieldsize); |
|
189 |
$rawdata = substr($rawdata, 3 + 5 + $fieldsize); |
|
190 |
} |
|
191 |
||
192 |
if (isset($ParsedLyrics3['raw']['IND'])) { |
|
193 |
$i = 0; |
|
194 |
$flagnames = array('lyrics', 'timestamps', 'inhibitrandom'); |
|
195 |
foreach ($flagnames as $flagname) { |
|
196 |
if (strlen($ParsedLyrics3['raw']['IND']) > $i++) { |
|
197 |
$ParsedLyrics3['flags'][$flagname] = $this->IntString2Bool(substr($ParsedLyrics3['raw']['IND'], $i, 1 - 1)); |
|
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
$fieldnametranslation = array('ETT'=>'title', 'EAR'=>'artist', 'EAL'=>'album', 'INF'=>'comment', 'AUT'=>'author'); |
|
203 |
foreach ($fieldnametranslation as $key => $value) { |
|
204 |
if (isset($ParsedLyrics3['raw'][$key])) { |
|
205 |
$ParsedLyrics3['comments'][$value][] = trim($ParsedLyrics3['raw'][$key]); |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
if (isset($ParsedLyrics3['raw']['IMG'])) { |
|
210 |
$imagestrings = explode("\r\n", $ParsedLyrics3['raw']['IMG']); |
|
211 |
foreach ($imagestrings as $key => $imagestring) { |
|
212 |
if (strpos($imagestring, '||') !== false) { |
|
213 |
$imagearray = explode('||', $imagestring); |
|
214 |
$ParsedLyrics3['images'][$key]['filename'] = (isset($imagearray[0]) ? $imagearray[0] : ''); |
|
215 |
$ParsedLyrics3['images'][$key]['description'] = (isset($imagearray[1]) ? $imagearray[1] : ''); |
|
216 |
$ParsedLyrics3['images'][$key]['timestamp'] = $this->Lyrics3Timestamp2Seconds(isset($imagearray[2]) ? $imagearray[2] : ''); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
if (isset($ParsedLyrics3['raw']['LYR'])) { |
|
221 |
$this->Lyrics3LyricsTimestampParse($ParsedLyrics3); |
|
222 |
} |
|
223 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
$this->error('"LYRICS200" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead'); |
0 | 225 |
return false; |
226 |
} |
|
227 |
break; |
|
228 |
||
229 |
default: |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
$this->error('Cannot process Lyrics3 version '.$version.' (only v1 and v2)'); |
0 | 231 |
return false; |
232 |
break; |
|
233 |
} |
|
234 |
||
235 |
||
236 |
if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] <= $ParsedLyrics3['tag_offset_end'])) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
$this->warning('ID3v1 tag information ignored since it appears to be a false synch in Lyrics3 tag data'); |
0 | 238 |
unset($info['id3v1']); |
239 |
foreach ($info['warning'] as $key => $value) { |
|
240 |
if ($value == 'Some ID3v1 fields do not use NULL characters for padding') { |
|
241 |
unset($info['warning'][$key]); |
|
242 |
sort($info['warning']); |
|
243 |
break; |
|
244 |
} |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
$info['lyrics3'] = $ParsedLyrics3; |
|
249 |
||
250 |
return true; |
|
251 |
} |
|
252 |
||
253 |
public function Lyrics3Timestamp2Seconds($rawtimestamp) { |
|
254 |
if (preg_match('#^\\[([0-9]{2}):([0-9]{2})\\]$#', $rawtimestamp, $regs)) { |
|
255 |
return (int) (($regs[1] * 60) + $regs[2]); |
|
256 |
} |
|
257 |
return false; |
|
258 |
} |
|
259 |
||
260 |
public function Lyrics3LyricsTimestampParse(&$Lyrics3data) { |
|
261 |
$lyricsarray = explode("\r\n", $Lyrics3data['raw']['LYR']); |
|
262 |
foreach ($lyricsarray as $key => $lyricline) { |
|
263 |
$regs = array(); |
|
264 |
unset($thislinetimestamps); |
|
265 |
while (preg_match('#^(\\[[0-9]{2}:[0-9]{2}\\])#', $lyricline, $regs)) { |
|
266 |
$thislinetimestamps[] = $this->Lyrics3Timestamp2Seconds($regs[0]); |
|
267 |
$lyricline = str_replace($regs[0], '', $lyricline); |
|
268 |
} |
|
269 |
$notimestamplyricsarray[$key] = $lyricline; |
|
270 |
if (isset($thislinetimestamps) && is_array($thislinetimestamps)) { |
|
271 |
sort($thislinetimestamps); |
|
272 |
foreach ($thislinetimestamps as $timestampkey => $timestamp) { |
|
273 |
if (isset($Lyrics3data['synchedlyrics'][$timestamp])) { |
|
274 |
// timestamps only have a 1-second resolution, it's possible that multiple lines |
|
275 |
// could have the same timestamp, if so, append |
|
276 |
$Lyrics3data['synchedlyrics'][$timestamp] .= "\r\n".$lyricline; |
|
277 |
} else { |
|
278 |
$Lyrics3data['synchedlyrics'][$timestamp] = $lyricline; |
|
279 |
} |
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
$Lyrics3data['unsynchedlyrics'] = implode("\r\n", $notimestamplyricsarray); |
|
284 |
if (isset($Lyrics3data['synchedlyrics']) && is_array($Lyrics3data['synchedlyrics'])) { |
|
285 |
ksort($Lyrics3data['synchedlyrics']); |
|
286 |
} |
|
287 |
return true; |
|
288 |
} |
|
289 |
||
290 |
public function IntString2Bool($char) { |
|
291 |
if ($char == '1') { |
|
292 |
return true; |
|
293 |
} elseif ($char == '0') { |
|
294 |
return false; |
|
295 |
} |
|
296 |
return null; |
|
297 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
} |