-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathphpmp3.php
447 lines (423 loc) · 12.2 KB
/
phpmp3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/**
* Class PHPMP3
*
* @license LGPL
*/
class PHPMP3
{
/**
* The full mp3 file
*
* @var string
*/
private $str;
/**
* The time length of the current file
*
* @var
*/
private $time;
/**
* The amount of frames in the current file
*
* @var
*/
private $frames;
/**
* Translate ascii characters to binary
*
* @var array
*/
private $binaryTable;
/**
* Construct a new instance
*
* @param string $path Path to an mp3 file
*/
public function __construct($path = '')
{
$this->binaryTable = array();
for ($i = 0; $i < 256; $i ++) {
$this->binaryTable[chr($i)] = sprintf('%08b', $i);
}
if ($path != '') {
$this->str = file_get_contents($path);
}
}
/**
* Set the mp3 data
*
* @param string $str Mp3 file
* @return void
*/
private function setStr($str)
{
$this->str = $str;
}
/**
* Get the start of audio data
*
* @return bool|int|void
*/
public function getStart()
{
$currentStrPos = - 1;
while (true) {
$currentStrPos = strpos($this->str, chr(255), $currentStrPos + 1);
if ($currentStrPos === false) {
return 0;
}
$str = substr($this->str, $currentStrPos, 4);
$strlen = strlen($str);
$parts = array();
for ($i = 0; $i < $strlen; $i ++) {
$parts[] = $this->binaryTable[$str[$i]];
}
if ($this->doFrameStuff($parts) === false) {
continue;
}
return $currentStrPos;
}
}
/**
* Manually count the frames and time of the file
*
* @return bool
*/
public function setFileInfoExact()
{
$maxStrLen = strlen($this->str);
$currentStrPos = $this->getStart();
$framesCount = 0;
$time = 0;
while ($currentStrPos < $maxStrLen) {
$str = substr($this->str, $currentStrPos, 4);
$strlen = strlen($str);
$parts = array();
for ($i = 0; $i < $strlen; $i ++) {
$parts[] = $this->binaryTable[$str[$i]];
}
if ($parts[0] != '11111111') {
if (($maxStrLen - 128) > $currentStrPos) {
return false;
} else {
$this->time = $time;
$this->frames = $framesCount;
return true;
}
}
$a = $this->doFrameStuff($parts);
$currentStrPos += $a[0];
$time += $a[1];
$framesCount ++;
}
$this->time = $time;
$this->frames = $framesCount;
return true;
}
/**
* Extract a portion of an mp3
*
* @param int $start Time in seconds to extract from
* @param int $length Time in seconds to extract
* @return static
*/
public function extract($start, $length)
{
$maxStrLen = strlen($this->str);
$currentStrPos = $this->getStart();
$framesCount = 0;
$time = 0;
$startCount = - 1;
$endCount = - 1;
while ($currentStrPos < $maxStrLen) {
if ($startCount == - 1 && $time >= $start) {
$startCount = $currentStrPos;
}
if ($endCount == - 1 && $time >= ($start + $length)) {
$endCount = $currentStrPos - $startCount;
}
$str = substr($this->str, $currentStrPos, 4);
$strlen = strlen($str);
$parts = array();
for ($i = 0; $i < $strlen; $i ++) {
$parts[] = $this->binaryTable[$str[$i]];
}
if ($parts[0] == '11111111') {
$a = $this->doFrameStuff($parts);
$currentStrPos += $a[0];
$time += $a[1];
$framesCount ++;
} else {
break;
}
}
$mp3 = new static();
if ($endCount == - 1) {
$endCount = $maxStrLen - $startCount;
}
if ($startCount != - 1 && $endCount != - 1) {
$mp3->setStr(substr($this->str, $startCount, $endCount));
}
return $mp3;
}
/**
* Get the length of a frame in bytes and seconds
*
* @param string[] $parts A frame with bytes converted to binary
* @return array|bool
*/
private function doFrameStuff($parts)
{
//Get Audio Version
$seconds = 0;
$errors = array();
switch (substr($parts[1], 3, 2)) {
case '01':
$errors[] = 'Reserved audio version';
break;
case '00':
$audio = 2.5;
break;
case '10':
$audio = 2;
break;
case '11':
$audio = 1;
break;
}
//Get Layer
switch (substr($parts[1], 5, 2)) {
case '01':
$layer = 3;
break;
case '00':
$errors[] = 'Reserved layer';
break;
case '10':
$layer = 2;
break;
case '11':
$layer = 1;
break;
}
//Get Bitrate
$bitFlag = substr($parts[2], 0, 4);
$bitArray = array(
'0000' => array(0, 0, 0, 0, 0),
'0001' => array(32, 32, 32, 32, 8),
'0010' => array(64, 48, 40, 48, 16),
'0011' => array(96, 56, 48, 56, 24),
'0100' => array(128, 64, 56, 64, 32),
'0101' => array(160, 80, 64, 80, 40),
'0110' => array(192, 96, 80, 96, 48),
'0111' => array(224, 112, 96, 112, 56),
'1000' => array(256, 128, 112, 128, 64),
'1001' => array(288, 160, 128, 144, 80),
'1010' => array(320, 192, 160, 160, 96),
'1011' => array(352, 224, 192, 176, 112),
'1100' => array(384, 256, 224, 192, 128),
'1101' => array(416, 320, 256, 224, 144),
'1110' => array(448, 384, 320, 256, 160),
'1111' => array(- 1, - 1, - 1, - 1, - 1)
);
$bitPart = $bitArray[$bitFlag];
$bitArrayNumber = null;
if ($audio == 1) {
switch ($layer) {
case 1:
$bitArrayNumber = 0;
break;
case 2:
$bitArrayNumber = 1;
break;
case 3:
$bitArrayNumber = 2;
break;
}
} else {
switch ($layer) {
case 1:
$bitArrayNumber = 3;
break;
case 2:
$bitArrayNumber = 4;
break;
case 3:
$bitArrayNumber = 4;
break;
}
}
$bitRate = $bitPart[$bitArrayNumber];
if ($bitRate <= 0) {
return false;
}
//Get Frequency
$frequencies = array(
1 => array(
'00' => 44100,
'01' => 48000,
'10' => 32000,
'11' => 'reserved'
),
2 => array(
'00' => 44100,
'01' => 48000,
'10' => 32000,
'11' => 'reserved'
),
2.5 => array(
'00' => 44100,
'01' => 48000,
'10' => 32000,
'11' => 'reserved'
)
);
$freq = $frequencies[$audio][substr($parts[2], 4, 2)];
$frameLength = 0;
//IsPadded?
$padding = substr($parts[2], 6, 1);
if ($layer == 3 || $layer == 2) {
$frameLength = 144 * $bitRate * 1000 / $freq + $padding;
}
$frameLength = floor($frameLength);
if ($frameLength == 0) {
return false;
}
$seconds += $frameLength * 8 / ($bitRate * 1000);
return array($frameLength, $seconds);
}
/**
* Set ID3 data
*
* @param string $track
* @param string $title
* @param string $artist
* @param string $album
* @param string $year
* @param string $genre
* @param string $comments
* @param string $composer
* @param string $origArtist
* @param string $copyright
* @param string $url
* @param string $encodedBy
* @return void
*/
public function setIdv3_2(
$track,
$title,
$artist,
$album,
$year,
$genre,
$comments,
$composer,
$origArtist,
$copyright,
$url,
$encodedBy
) {
$urlLength = (int) (strlen($url) + 2);
$copyrightLength = (int) (strlen($copyright) + 1);
$origArtistLength = (int) (strlen($origArtist) + 1);
$composerLength = (int) (strlen($composer) + 1);
$commentsLength = (int) (strlen($comments) + 5);
$titleLength = (int) (strlen($title) + 1);
$artistLength = (int) (strlen($artist) + 1);
$albumLength = (int) (strlen($album) + 1);
$genreLength = (int) (strlen($genre) + 1);
$encodedByLength = (int) (strlen($encodedBy) + 1);
$trackLength = (int) (strlen($track) + 1);
$yearLength = (int) (strlen($year) + 1);
$str = "ID3\x03\0\0\0\0\x085TRCK\0\0\0{$trackLength}\0\0\0{$track}TENC\0\0\0{$encodedByLength}@\0\0{$encodedBy}WXXX\0\0\0{$urlLength}\0\0\0\0{$url}TCOP\0\0\0{$copyrightLength}\0\0\0{$copyright}TOPE\0\0\0{$origArtistLength}\0\0\0{$origArtist}TCOM\0\0\0{$composerLength}\0\0\0{$composer}COMM\0\0\0{$commentsLength}\0\0\0\0\x09\0\0{$comments}TCON\0\0\0{$genreLength}\0\0\0{$genre}TYER\0\0\0{$yearLength}\0\0\0{$year}TALB\0\0\0{$albumLength}\0\0\0{$album}TPE1\0\0\0{$artistLength}\0\0\0{$artist}TIT2\0\0\0{$titleLength}\0\0\0{$title}";
$this->str = $str . $this->str;
}
/**
* Append another mp3 file to the end of this file
*
* @param self $mp3
* @return void
*/
public function mergeBehind(self $mp3)
{
$this->str .= $mp3->str;
}
/**
* Prepend another mp3 to the start of this file
*
* @param self $mp3
* @return void
*/
public function mergeInfront(self $mp3)
{
$this->str = $mp3->str . $this->str;
}
/**
* Get the end string of the ID3 data
*
* @return bool|string
*/
private function getIdvEnd()
{
$strlen = strlen($this->str);
$str = substr($this->str, ($strlen - 128));
$str1 = substr($str, 0, 3);
if (strtolower($str1) == strtolower('TAG')) {
return $str;
} else {
return false;
}
}
/**
* Remove ID3 data from the file
*
* @return bool
*/
public function striptags()
{
//Remove start stuff...
$s = $start = $this->getStart();
if ($s === false) {
return false;
} else {
$this->str = substr($this->str, $start);
}
//Remove end tag stuff
$end = $this->getIdvEnd();
if ($end !== false) {
$this->str = substr($this->str, 0, (strlen($this->str) - 129));
}
}
/**
* Write an mp3 file
*
* @param string $path Path to write file to
* @return bool
*/
public function save($path)
{
$fp = fopen($path, 'w');
$bytesWritten = fwrite($fp, $this->str);
fclose($fp);
return $bytesWritten == strlen($this->str);
}
/**
* Join multiple mp3 files into one file
*
* @param string $newpath
* @param array $array
* @return void
*/
public function multiJoin($newpath, $array)
{
foreach ($array as $path) {
$mp3 = new static($path);
$mp3->striptags();
$mp3_1 = new static($newpath);
$mp3->mergeBehind($mp3_1);
$mp3->save($newpath);
}
}
}