From bb70d36d2d058fe67038a85b098dfcaad8a8f5a7 Mon Sep 17 00:00:00 2001 From: wapmorgan Date: Wed, 1 Mar 2017 14:37:35 +0300 Subject: [PATCH] Add track and TRCK tags handler --- README.md | 4 ++-- bin/scan | 6 +++--- src/Mp3Info.php | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 60dbe48..67d10af 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ This class extracts information from mpeg/mp3 audio: | sampleRate | album | TALB | | channel | year | TYER | | framesCount | comment | COMM | -| codecVersion | genre | TCON | -| layerVersion | | | +| codecVersion | track | TRCK | +| layerVersion | genre | TCON | 1. Usage 2. Performance diff --git a/bin/scan b/bin/scan index 25f85e7..60f10c6 100755 --- a/bin/scan +++ b/bin/scan @@ -27,7 +27,7 @@ function analyze($filename, &$total_duration, &$total_parse_time, $id3v2 = false } catch (Exception $e) { return null; } - echo sprintf('%15s | %4s | %7s | %0.1fkHz | %-11s | %-10s | %.5f', substrIfLonger(basename($filename), 15), formatTime($audio->duration), $audio->isVbr ? 'vbr' : ($audio->bitRate / 1000).'kbps', ($audio->sampleRate / 1000), isset($audio->tags1['song']) ? substrIfLonger($audio->tags1['song'], 11) : null, isset($audio->tags1['artist']) ? substrIfLonger($audio->tags1['artist'], 10) : null, $audio->_parsingTime).PHP_EOL; + echo sprintf('%15s | %4s | %7s | %0.1fkHz | %-11s | %-10s | %5d | %.5f', substrIfLonger(basename($filename), 15), formatTime($audio->duration), $audio->isVbr ? 'vbr' : ($audio->bitRate / 1000).'kbps', ($audio->sampleRate / 1000), isset($audio->tags1['song']) ? substrIfLonger($audio->tags1['song'], 11) : null, isset($audio->tags1['artist']) ? substrIfLonger($audio->tags1['artist'], 10) : null, isset($audio->tags1['track']) ? substrIfLonger($audio->tags1['track'], 5) : null, $audio->_parsingTime).PHP_EOL; if ($id3v2 && !empty($audio->tags2)) { foreach ($audio->tags2 as $tag=>$value) { echo ' '.$tag.': '; @@ -51,12 +51,12 @@ function analyzeId3($filename, &$total_parse_time) { $t = microtime(true); $info = $ID3->analyze($filename); $parse_time = microtime(true) - $t; - echo sprintf('%15s | %4s | %7s | %0.1fkHz | %-11s | %-10s | %.5f', substrIfLonger(basename($filename), 15), $info['playtime_string'], $info['audio']['bitrate_mode'] == 'vbr' ? 'vbr' : floor($info['audio']['bitrate'] / 1000).'kbps', ($info['audio']['sample_rate'] / 1000), isset($info['tags']['title']) ? substrIfLonger($info['tags']['title'], 11) : null, isset($info['tags']['artist']) ? substrIfLonger($info['tags']['artist'], 10) : null, $parse_time).PHP_EOL; + echo sprintf('%15s | %4s | %7s | %0.1fkHz | %-11s | %-10s | %.5f | %5d', substrIfLonger(basename($filename), 15), $info['playtime_string'], $info['audio']['bitrate_mode'] == 'vbr' ? 'vbr' : floor($info['audio']['bitrate'] / 1000).'kbps', ($info['audio']['sample_rate'] / 1000), isset($info['tags']['title']) ? substrIfLonger($info['tags']['title'], 11) : null, isset($info['tags']['artist']) ? substrIfLonger($info['tags']['artist'], 10) : null, null, $parse_time).PHP_EOL; $total_parse_time += $parse_time; } array_shift($argv); -echo sprintf('%15s | %4s | %7s | %7s | %11s | %10s | %4s', 'File name', 'dur.', 'bitrate', 'sample', 'song', 'artist', +echo sprintf('%15s | %4s | %7s | %7s | %11s | %10s | %5s | %4s', 'File name', 'dur.', 'bitrate', 'sample', 'song', 'artist', 'track', 'time').PHP_EOL; $total_duration = $total_parse_time = $id3_parse_time = 0; foreach ($argv as $arg) { diff --git a/src/Mp3Info.php b/src/Mp3Info.php index 4907ab8..dc2f395 100644 --- a/src/Mp3Info.php +++ b/src/Mp3Info.php @@ -297,8 +297,10 @@ class Mp3Info { $this->tags1['artist'] = trim(fread($fp, 30)); $this->tags1['album'] = trim(fread($fp, 30)); $this->tags1['year'] = trim(fread($fp, 4)); - $this->tags1['comment'] = trim(fread($fp, 30)); - $this->tags1['genre'] = hexdec(fread($fp, 1)); + $this->tags1['comment'] = trim(fread($fp, 28)); + fseek($fp, 1, SEEK_CUR); + $this->tags1['track'] = ord(fread($fp, 1)); + $this->tags1['genre'] = ord(fread($fp, 1)); return 128; } @@ -483,7 +485,14 @@ class Mp3Info { // case 'TPE4': # Interpreted, remixed, or otherwise modified by // case 'TPOS': # Part of a set // case 'TPUB': # Publisher - // case 'TRCK': # Track number/Position in set + case 'TRCK': # Track number/Position in set + $raw = fread($fp, $frame_size); + $data = unpack("C1encoding/A".($frame_size - 1)."information", $raw); + if ((bool)($data['encoding'] == 0x00)) # ISO-8859-1 + $this->tags2[$frame_id] = mb_convert_encoding($data['information'], 'utf-8', 'iso-8859-1'); + else # utf-16 + $this->tags2[$frame_id] = mb_convert_encoding($data['information'], 'utf-8', 'utf-16'); + break; // case 'TRDA': # Recording dates // case 'TRSN': # Internet radio station name // case 'TRSO': # Internet radio station owner