Last active 1 month ago

playlist-exporter.php Raw
1<?php
2
3require 'vendor/autoload.php';
4$ffmpeg = FFMpeg\FFMpeg::create();
5
6function 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
31foreach ($items as $k => $item) {
32 $out = $argv[2].'/'.normalize_string($item['artist']).'-'.normalize_string($item['title']).'.mp3';
33 if (!file_exists($out) && file_exists($item['file'])) {
34 $audio = $ffmpeg->open($item['file']);
35 $audio->save(new FFMpeg\Format\Audio\MP3(), $out);
36 }
37 $items[$k]['file'] = $out;
38}
39
40$json = [
41 'title' => '',
42 'description' => '',
43 'cover' => '',
44 'tags' => [],
45 'pubDate' => '',
46 'tracklist' => $items
47];
48
49file_put_contents(preg_replace('/\.[a-zA-Z0-9]$/', '', $argv[1]).'.json', json_encode($json, JSON_PRETTY_PRINT));