Last active 1 month ago

Revision 8588fddf0c099e8b02845b716324c79d358f484b

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)));
30print_r($items);
31
32foreach ($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
50file_put_contents(preg_replace('/\.[a-zA-Z0-9]$/', '', $argv[1]).'.json', json_encode($json));