Use Mp3Info class for metadata parsing

This commit is contained in:
2025-06-15 01:25:38 +01:00
parent 0da2a3a223
commit 4c15690a23
5 changed files with 43 additions and 31 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
filesize.cache
filemeta.cache
/feed_*.rss2

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "Mp3Info"]
path = Mp3Info
url = https://git.mbirth.uk/mbirth/php-mp3info.git

1
Mp3Info Submodule

Submodule Mp3Info added at 7a8dede10b

View File

@ -56,7 +56,7 @@ if ($show_conf === false) {
}
// PARSE BASSDRIVE ARCHIVE
$show_data = new BDAParser($show_dir, $show_conf['archive']['url'], $show_conf['archive']['mp3_bitrate']);
$show_data = new BDAParser($show_dir, $show_conf['archive']['url']);
$file_list = $show_data->fetchFiles();
// GENERATE RSS XML

View File

@ -2,6 +2,10 @@
namespace BassDrive;
require_once __DIR__ . '/../Mp3Info/src/Mp3Info.php';
use wapmorgan\Mp3Info\Mp3Info;
/**
* BassDrive Archive Parser class
* @author Markus Birth <markus@birth-online.de>
@ -15,7 +19,7 @@ class BDAParser
$this->show_dir = $show_dir;
$this->archive_url = $archive_url;
$this->mp3_bitrate = $mp3_bitrate;
$this->cachefile = $show_dir . 'filesize.cache';
$this->cachefile = $show_dir . 'filemeta.cache';
$this->initCache();
}
@ -24,7 +28,7 @@ class BDAParser
$this->len_cache = array();
$this->cache_dirty = false;
if (file_exists($this->cachefile)) {
$this->len_cache = json_decode(file_get_contents($this->cachefile), true);
$this->meta_cache = json_decode(file_get_contents($this->cachefile), true);
}
}
@ -32,7 +36,7 @@ class BDAParser
{
// Flush filesize cache if needed
if ($this->cache_dirty) {
file_put_contents($this->cachefile, json_encode($this->len_cache, JSON_PRETTY_PRINT), LOCK_EX);
file_put_contents($this->cachefile, json_encode($this->meta_cache, JSON_PRETTY_PRINT), LOCK_EX);
}
}
@ -56,45 +60,49 @@ class BDAParser
$title = urldecode(substr($href, 0, -4)); // strip off ".mp3" file extension
$url = $this->archive_url . $href;
$size = $this->getSize($url);
array_push($file_list, array(
'url' => $url,
'title' => trim($title),
'size' => $size,
'duration' => $this->getDuration($size),
'date' => $this->getDateFromTitle(trim($title)),
));
try {
$meta = $this->getMetadata($url);
array_push($file_list, array(
'url' => $url,
'title' => trim($title),
'size' => $meta['_fileSize'],
'duration' => $this->getDurationFromSeconds($meta['duration']),
'date' => $this->getDateFromTitle(trim($title)),
));
} catch (\Exception $e) {
// Whoopsie! Just skip this one...
}
}
return $file_list;
}
public function getSize($url)
public function getMetadata($url)
{
if (isset($this->len_cache[$url])) {
return $this->len_cache[$url];
if (isset($this->meta_cache[$url])) {
return $this->meta_cache[$url];
}
$context = stream_context_set_default(array(
'http' => array(
'method' => 'HEAD',
),
));
$headers = get_headers($url, 1);
$length = intval($headers['Content-Length']);
$this->len_cache[$url] = $length;
#var_dump($url);
Mp3Info::$headerSeekLimit = 512000;
$meta = new Mp3Info($url);
$this->meta_cache[$url] = array(
'audioSize' => $meta->audioSize,
'duration' => $meta->duration,
'bitRate' => $meta->bitRate,
'sampleRate' => $meta->sampleRate,
'_fileSize' => $meta->_fileSize,
'_framesCount' => $meta->_framesCount,
);
$this->cache_dirty = true;
return $length;
return $this->meta_cache[$url];
}
public function getDuration($size)
public function getDurationFromSeconds($seconds)
{
// 70445111 Bytes = 01:13:22.80 = 73:22.80 = 4402.80 seconds (128 kb/s)
// 147530737 Bytes = 02:33:40.64 = 153:40.64 = 9220.64 seconds (128 kb/s)
$bitrate = intval($this->mp3_bitrate) * 1000;
$seconds = $size / ($bitrate / 8);
$seconds = ceil($seconds);
$hours = intdiv($seconds, 3600);
$seconds = $seconds % 3600;
$minutes = intdiv($seconds, 60);