From 89fac15d4ff1912bd0754745acb0a712a6510692 Mon Sep 17 00:00:00 2001
From: wapmorgan <wapmorgan@gmail.com>
Date: Tue, 3 Dec 2019 03:35:40 +0300
Subject: [PATCH] update mp3scan & update seeking for mpeg header

---
 bin/mp3scan     |  6 +++---
 src/Mp3Info.php | 16 +++++++++++-----
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/bin/mp3scan b/bin/mp3scan
index 59f84d4..b5da0bb 100644
--- a/bin/mp3scan
+++ b/bin/mp3scan
@@ -140,9 +140,9 @@ class Mp3InfoConsoleRunner {
                 self::formatTime($audio->duration),
                 $audio->isVbr ? 'vbr' : ($audio->bitRate / 1000).'kbps',
                 ($audio->sampleRate / 1000),
-                isset($audio->tags1['song']) ? self::substrIfLonger($audio->tags1['song'], 11) : null,
-                isset($audio->tags1['artist']) ? self::substrIfLonger($audio->tags1['artist'], 10) : null,
-                isset($audio->tags1['track']) ? self::substrIfLonger($audio->tags1['track'], 5) : null,
+                isset($audio->tags['song']) ? self::substrIfLonger($audio->tags['song'], 11) : null,
+                isset($audio->tags['artist']) ? self::substrIfLonger($audio->tags['artist'], 10) : null,
+                isset($audio->tags['track']) ? self::substrIfLonger($audio->tags['track'], 5) : null,
                 $audio->_parsingTime)
             .PHP_EOL;
 
diff --git a/src/Mp3Info.php b/src/Mp3Info.php
index bc76340..c43f879 100644
--- a/src/Mp3Info.php
+++ b/src/Mp3Info.php
@@ -304,16 +304,22 @@ class Mp3Info {
         $header_seek_pos = ftell($fp) + self::$headerSeekLimit;
         do {
             $pos = ftell($fp);
-            $first_header_byte = $this->readBytes($fp, 4);
+            $first_header_byte = $this->readBytes($fp, 1);
             if ($first_header_byte[0] === 0xFF) {
-                fseek($fp, $pos);
-                $header_bytes = $this->readBytes($fp, 4);
-                break;
+                $second_header_byte = $this->readBytes($fp, 1);
+                if ((($second_header_byte[0] >> 5) & 0b111) == 0b111) {
+                    fseek($fp, $pos);
+                    $header_bytes = $this->readBytes($fp, 4);
+                    break;
+                }
             }
             fseek($fp, 1, SEEK_CUR);
         } while (ftell($fp) <= $header_seek_pos);
 
-        if ($header_bytes[0] !== 0xFF || (($header_bytes[1] >> 5) & 0b111) != 0b111) throw new \Exception("At ".$pos."(0x".dechex($pos).") should be a frame header!");
+        if (!isset($header_bytes) || $header_bytes[0] !== 0xFF || (($header_bytes[1] >> 5) & 0b111) != 0b111) {
+            throw new \Exception('At '.$pos
+                .'(0x'.dechex($pos).') should be a frame header!');
+        }
 
         switch ($header_bytes[1] >> 3 & 0b11) {
             case 0b00: $this->codecVersion = self::MPEG_25; break;