Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved PSR-4 code compliance and fixed formatting #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ This class extracts information from mpeg/mp3 audio:
After creating an instance of `Mp3Info` with passing filename as the first argument to the constructor, you can retrieve data from object properties (listed below).

```php
use wapmorgan\Mp3Info\Mp3Info;
use Wapmorgan\Mp3Info\Mp3Info;
// To get basic audio information
$audio = new Mp3Info('./audio.mp3');

Expand All @@ -66,7 +66,7 @@ echo 'Song '.$audio->tags1['song'].' from '.$audio->tags1['artist'].PHP_EOL;
// specific id3v2 tags
echo 'Song '.$audio->tags2['TIT2'].' from '.$audio->tags2['TPE1'].PHP_EOL;

// combined tags (simplies way to get as more information as possible)
// combined tags (simplest way to get as more information as possible)
echo 'Song '.$audio->tags['song'].' from '.$audio->tags['artist'].PHP_EOL;
```

Expand Down
78 changes: 47 additions & 31 deletions bin/mp3scan
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#!/usr/bin/php
<?php
use wapmorgan\Mp3Info\Mp3Info;

use Wapmorgan\Mp3Info\Mp3Info;

$paths = [
// as a root package or phar
__DIR__.'/../vendor/autoload.php',
__DIR__ . '/../vendor/autoload.php',
// as a dependency from bin
__DIR__.'/../autoload.php',
__DIR__ . '/../autoload.php',
// as a dependency from package folder
__DIR__.'/../../../autoload.php',
__DIR__ . '/../../../autoload.php',
];
function init_composer(array $paths) {

/**
* @param array $paths
* @return bool
*/
function init_composer(array $paths)
{
foreach ($paths as $path) {
if (file_exists($path)) {
require_once $path;
Expand All @@ -19,11 +26,13 @@ function init_composer(array $paths) {
}
return false;
}
if (!init_composer($paths)) die('Run `composer install` firstly.'.PHP_EOL);

if (!init_composer($paths)) die('Run `composer install` firstly.' . PHP_EOL);
if ($argc == 1)
die('Specify file names to scan');

class Mp3InfoConsoleRunner {
class Mp3InfoConsoleRunner
{

/** @var array */
protected $widths = array(
Expand All @@ -49,23 +58,23 @@ class Mp3InfoConsoleRunner {

/**
* @param array $fileNames
* @param bool $verbose
* @param bool $verbose
*/
public function run(array $fileNames, $verbose = false)
{
$this->adjustOutputSize();
$this->songRowTempalte = '%'.$this->widths['filename'].'s | %'.$this->widths['duration'].'s | %'.$this->widths['bitRate'].'s | %'.$this->widths['sampleRate'].'s | %'
.$this->widths['song'].'s | %'.$this->widths['artist'].'s | %'.$this->widths['track'].'s | %'.$this->widths['parseTime'].'s';
$this->songRowTempalte = '%' . $this->widths['filename'] . 's | %' . $this->widths['duration'] . 's | %' . $this->widths['bitRate'] . 's | %' . $this->widths['sampleRate'] . 's | %'
. $this->widths['song'] . 's | %' . $this->widths['artist'] . 's | %' . $this->widths['track'] . 's | %' . $this->widths['parseTime'] . 's';
$this->compareWithId3 = class_exists('getID3');

echo sprintf($this->songRowTempalte, 'File name', 'dur.', 'bitrate', 'sample', 'song', 'artist', 'track',
'time').PHP_EOL;
'time') . PHP_EOL;

foreach ($fileNames as $file) {
$file = realpath($file);
if (is_dir($file)) {
echo $file.':'.PHP_EOL;
foreach (glob(rtrim($file, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'*.mp3') as $f) {
echo $file . ':' . PHP_EOL;
foreach (glob(rtrim($file, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*.mp3') as $f) {
if (is_file($f)) {
$this->analyze($f, false, $verbose);
if ($this->compareWithId3) $this->analyzeId3($f);
Expand All @@ -78,24 +87,25 @@ class Mp3InfoConsoleRunner {
}


echo sprintf('%42s | %34s', 'Total duration: '.self::formatTime($this->totalDuration), 'Total parsing time: '.round($this->totalParseTime, 5)).PHP_EOL;
echo sprintf('%42s | %34s', 'Total duration: ' . self::formatTime($this->totalDuration), 'Total parsing time: ' . round($this->totalParseTime, 5)) . PHP_EOL;
if ($this->compareWithId3)
echo sprintf('%79s', 'Total getId3 parsing time: '.round($this->totalId3ParseTime, 5)).PHP_EOL;
echo sprintf('%79s', 'Total getId3 parsing time: ' . round($this->totalId3ParseTime, 5)) . PHP_EOL;
}

/**
* @param $time
*
* @return string
*/
public static function formatTime($time) {
public static function formatTime($time)
{
if ($time > 3600)
return floor($time / 3600)
.':'.str_pad(floor($time % 3600 / 60), 2, 0, STR_PAD_LEFT)
.':'.str_pad($time % 60, 2, 0, STR_PAD_LEFT);
. ':' . str_pad(floor($time % 3600 / 60), 2, 0, STR_PAD_LEFT)
. ':' . str_pad($time % 60, 2, 0, STR_PAD_LEFT);
else
return floor($time / 60)
.':'.str_pad($time % 60, 2, 0, STR_PAD_LEFT);
. ':' . str_pad($time % 60, 2, 0, STR_PAD_LEFT);
}

/**
Expand All @@ -104,9 +114,10 @@ class Mp3InfoConsoleRunner {
*
* @return string
*/
public static function substrIfLonger($string, $maxLength) {
public static function substrIfLonger($string, $maxLength)
{
if (mb_strlen($string) > $maxLength) {
return mb_substr($string, 0, $maxLength-3).'...';
return mb_substr($string, 0, $maxLength - 3) . '...';
}
return $string;
}
Expand Down Expand Up @@ -134,30 +145,31 @@ class Mp3InfoConsoleRunner {
*
* @return null|void
*/
protected function analyze($filename, $id3v2 = false, $verbose = false) {
protected function analyze($filename, $id3v2 = false, $verbose = false)
{
if (!is_readable($filename)) return;
try {
$audio = new Mp3Info($filename, true);
} catch (Exception $e) {
var_dump("Exception when parsing ".$filename.': '.$e->getMessage());
var_dump("Exception when parsing " . $filename . ': ' . $e->getMessage());
return null;
}

echo sprintf($this->songRowTempalte,
self::convertToNativeEncoding(self::substrIfLonger(basename($filename), $this->widths['filename'])),
self::formatTime($audio->duration),
$audio->isVbr ? 'vbr' : ($audio->bitRate / 1000).'kbps',
$audio->isVbr ? 'vbr' : ($audio->bitRate / 1000) . 'kbps',
($audio->sampleRate / 1000),
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;
. PHP_EOL;

if ($id3v2 && !empty($audio->tags2)) {
foreach ($audio->tags as $tag => $value) {
echo ' '.$tag.': ';
echo self::convertToNativeEncoding($value).PHP_EOL;
echo ' ' . $tag . ': ';
echo self::convertToNativeEncoding($value) . PHP_EOL;
}
}

Expand All @@ -172,7 +184,8 @@ class Mp3InfoConsoleRunner {
/**
* @param $filename
*/
protected function analyzeId3($filename) {
protected function analyzeId3($filename)
{
static $ID3;
if ($ID3 === null) $ID3 = new getID3();

Expand All @@ -182,18 +195,22 @@ class Mp3InfoConsoleRunner {
echo sprintf($this->songRowTempalte,
self::substrIfLonger(basename($filename), $this->widths['filename']),
$info['playtime_string'],
$info['audio']['bitrate_mode'] == 'vbr' ? 'vbr' : floor($info['audio']['bitrate'] / 1000).'kbps',
$info['audio']['bitrate_mode'] == 'vbr' ? 'vbr' : floor($info['audio']['bitrate'] / 1000) . 'kbps',
($info['audio']['sample_rate'] / 1000),
isset($info['tags']['title']) ? self::substrIfLonger($info['tags']['title'], 11) : null,
isset($info['tags']['artist']) ? self::substrIfLonger($info['tags']['artist'], 10) :
null,
null,
$parse_time)
.PHP_EOL;
. PHP_EOL;

$this->totalId3ParseTime += $parse_time;
}

/**
* @param $string
* @return mixed
*/
protected static function convertToNativeEncoding($string)
{
// if (strncasecmp(PHP_OS, 'win', 3) === 0)
Expand All @@ -211,4 +228,3 @@ if (in_array('-v', $argv, true)) {

$runner = new Mp3InfoConsoleRunner();
$runner->run($argv, $verbose);

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "The fastest php library to extract mp3 tags & meta information.",
"autoload": {
"psr-4": {
"wapmorgan\\Mp3Info\\": "src/"
"Wapmorgan\\Mp3Info\\": "src/"
}
},
"require": {
Expand Down
17 changes: 9 additions & 8 deletions data/bitRateTable.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
use wapmorgan\Mp3Info\Mp3Info;

use Wapmorgan\Mp3Info\Mp3Info;

$data = [
Mp3Info::MPEG_1 => [
1 => [null, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000, false], // MPEG 1 layer 1
2 => [null, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000, false], // MPEG 1 layer 2
3 => [null, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, false], // MPEG 1 layer 3
1 => [null, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000, false], // MPEG 1 layer 1
2 => [null, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000, false], // MPEG 1 layer 2
3 => [null, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, false], // MPEG 1 layer 3
],
Mp3Info::MPEG_2 => [
1 => [null, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000, false], // MPEG 2 layer 1
2 => [null, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, false], // MPEG 2 layer 2
3 => [null, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, false], // MPEG 2 layer 3
Mp3Info::MPEG_2 => [
1 => [null, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000, false], // MPEG 2 layer 1
2 => [null, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, false], // MPEG 2 layer 2
3 => [null, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, false], // MPEG 2 layer 3
],
];

Expand Down
3 changes: 2 additions & 1 deletion data/sampleRateTable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
use wapmorgan\Mp3Info\Mp3Info;

use Wapmorgan\Mp3Info\Mp3Info;

return [
Mp3Info::MPEG_1 => [44100, 48000, 32000, false], // MPEG 1
Expand Down
Loading