playlist-exporter.php
· 1.3 KiB · PHP
Raw
<?php
require 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create();
function normalize_string($str) {
$out = str_replace(' ', '_', preg_replace('/[\'\"\(\)\.,\[\]]+/', '', strtolower(str_replace('Ke$ha', 'kesha', $str))));
return $out;
}
$file = trim(file_get_contents($argv[1]));
$items = array_map(function ($l) {
return explode("\t", $l);
}, explode("\n", $file));
$header = array_slice($items, 0, 1)[0];
$items = array_map(function ($l) use ($argv) {
return [
'title' => $l['Name'],
'artist' => $l['Artist'],
'album' => $l['Album'],
'mime' => 'audio/mp3',
'file' => str_replace("Macintosh HD", "", $l['Location']),
'note' => ''
];
}, array_map(function ($l) use ($header) {
print_r($header);
print_r($l);
return array_combine($header, $l);
}, array_slice($items, 1)));
print_r($items);
foreach ($items as $item) {
$out = $argv[2].'/'.normalize_string($item['artist']).'-'.normalize_string($item['title']).'.mp3';
if (!file_exists($out) && file_exists($item['file'])) {
$audio = $ffmpeg->open($item['file']);
$audio->save(new FFMpeg\Format\Audio\MP3(), $out);
}
$item['file'] = $out;
}
$json = [
'title' => '',
'description' => '',
'cover' => '',
'tags' => [],
'pubDate' => '',
'tracklist' => $items
];
file_put_contents(preg_replace('/\.[a-zA-Z0-9]$/', '', $argv[1]).'.json', json_encode($json));
| 1 | <?php |
| 2 | |
| 3 | require 'vendor/autoload.php'; |
| 4 | $ffmpeg = FFMpeg\FFMpeg::create(); |
| 5 | |
| 6 | function normalize_string($str) { |
| 7 | $out = str_replace(' ', '_', preg_replace('/[\'\"\(\)\.,\[\]]+/', '', strtolower(str_replace('Ke$ha', 'kesha', $str)))); |
| 8 | return $out; |
| 9 | } |
| 10 | |
| 11 | $file = trim(file_get_contents($argv[1])); |
| 12 | $items = array_map(function ($l) { |
| 13 | return explode("\t", $l); |
| 14 | }, explode("\n", $file)); |
| 15 | $header = array_slice($items, 0, 1)[0]; |
| 16 | $items = array_map(function ($l) use ($argv) { |
| 17 | return [ |
| 18 | 'title' => $l['Name'], |
| 19 | 'artist' => $l['Artist'], |
| 20 | 'album' => $l['Album'], |
| 21 | 'mime' => 'audio/mp3', |
| 22 | 'file' => str_replace("Macintosh HD", "", $l['Location']), |
| 23 | 'note' => '' |
| 24 | ]; |
| 25 | }, array_map(function ($l) use ($header) { |
| 26 | print_r($header); |
| 27 | print_r($l); |
| 28 | return array_combine($header, $l); |
| 29 | }, array_slice($items, 1))); |
| 30 | print_r($items); |
| 31 | |
| 32 | foreach ($items as $item) { |
| 33 | $out = $argv[2].'/'.normalize_string($item['artist']).'-'.normalize_string($item['title']).'.mp3'; |
| 34 | if (!file_exists($out) && file_exists($item['file'])) { |
| 35 | $audio = $ffmpeg->open($item['file']); |
| 36 | $audio->save(new FFMpeg\Format\Audio\MP3(), $out); |
| 37 | } |
| 38 | $item['file'] = $out; |
| 39 | } |
| 40 | |
| 41 | $json = [ |
| 42 | 'title' => '', |
| 43 | 'description' => '', |
| 44 | 'cover' => '', |
| 45 | 'tags' => [], |
| 46 | 'pubDate' => '', |
| 47 | 'tracklist' => $items |
| 48 | ]; |
| 49 | |
| 50 | file_put_contents(preg_replace('/\.[a-zA-Z0-9]$/', '', $argv[1]).'.json', json_encode($json)); |