Skip to content

Commit 534c305

Browse files
committed
#13 #24 Fix invalid duration due to difference in first N frames. Solution: read few frames and compute duration based on the last frame data.
1 parent cbbd6b3 commit 534c305

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ php bin/scan ./
115115
- `Mp3Info::isValidAudio($filename)`
116116
Static method that checks if file `$filename` looks like a mp3-file. Returns `true` if file looks like a mp3, otherwise false.
117117

118+
### Settings
119+
You can adjust some variables to reconfigure before instantiating of object:
120+
121+
- `Mp3Info::$headerSeekLimit` - count of bytes to search for the first mpeg header in audio. Default: `2048` (bytes).
122+
- `Mp3Info::$framesCountRead` - count of mpeg frames to read before compute audio duration. Default: `2` (frames).
123+
118124
## Technical information
119125
Supporting features:
120126
* id3v1

bin/mp3scan

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Mp3InfoConsoleRunner {
9797
.':'.str_pad($time % 60, 2, 0, STR_PAD_LEFT);
9898
else
9999
return floor($time / 60)
100-
.':'.str_pad($time % 60, 2, 0, STR_PAD_LEFT);
100+
.':'.str_pad((int)$time % 60, 2, 0, STR_PAD_LEFT);
101101
}
102102

103103
/**

src/Mp3Info.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ class Mp3Info {
5656
/**
5757
* @var array
5858
*/
59-
static private $_bitRateTable;
59+
private static $_bitRateTable;
6060

6161
/**
6262
* @var array
6363
*/
64-
static private $_sampleRateTable;
64+
private static $_sampleRateTable;
6565

6666
/**
6767
* @var array
6868
*/
69-
static private $_vbrOffsets = [
69+
private static $_vbrOffsets = [
7070
self::MPEG_1 => [21, 36],
7171
self::MPEG_2 => [13, 21],
7272
self::MPEG_25 => [13, 21],
@@ -77,6 +77,8 @@ class Mp3Info {
7777
*/
7878
public static $headerSeekLimit = 2048;
7979

80+
public static $framesCountRead = 2;
81+
8082
/**
8183
* @var int MPEG codec version (1 or 2 or 2.5 or undefined)
8284
*/
@@ -307,8 +309,11 @@ private function parseAudio($filename, $fileSize, $mode) {
307309
/**
308310
* First frame can lie. Need to fix in the future.
309311
* @link https://github.com/wapmorgan/Mp3Info/issues/13#issuecomment-447470813
312+
* Read first N frames
310313
*/
311-
$framesCount = $this->readMpegFrame($fp);
314+
for ($i = 0; $i < self::$framesCountRead; $i++) {
315+
$framesCount = $this->readMpegFrame($fp);
316+
}
312317

313318
$this->_framesCount = $framesCount !== null
314319
? $framesCount

0 commit comments

Comments
 (0)