-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlist_sync
executable file
·246 lines (214 loc) · 6.18 KB
/
playlist_sync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/php
<?php
$convert_bitrate = "";
function error($message) {
echo "\n";
echo $message;
echo "\n";
echo "\n";
exit();
}
if (!$argv[1]) {
error("Please specify a m3u playlist file to read from\n\nUsage:\n\n\tplaylist_sync playlist.m3u [options]\n\n OPTIONS:\n\n\t -c bitrate : Bitrate to convert audio to, default no conversion.");
}
$playlist = $argv[1];
if ($argv[2] == "-c") {
$convert_bitrate = $argv[3];
if (!is_numeric($convert_bitrate)) {
error("Invalid conversion bitrate specificied, try -h");
}
}
if ($argv[1] == "-h") {
error("Usage:\n\n\tplaylist_sync playlist.m3u [options]\n\n OPTIONS:\n\n\t -c bitrate : Bitrate to convert audio to, default no conversion.");
}
echo "\n";
echo "Reading first entry from Playlist file";
echo "\n";
$handle = fopen($playlist,"r");
if (!$handle) {
error("Could not open playlist file");
}
$contents = file_get_contents($playlist);
$contents = explode("",$contents);
for($i=0;$i<count($contents);$i++) {
$line = $contents[$i];
if (substr($line,0,strlen("#EXT")) != "#EXT" && strlen($line) > 2) {
$tracks[] = $line;
}
}
$library_path = getLibraryPath($tracks);
if ($library_path == undef) {
error("Could not figure out library path, is this an iTunes music collection!");
}
$library_path .= "/";
echo "\nGot library path of $library_path\n";
$memory_key = getMemoryKey() . "/";
echo "\nDestination $memory_key\n";
$response = "";
while ($response != "y" && $response != "n") {
echo "\nDo you want to auto-remove files in $memory_key that are not in the playlist? y/n \n";
$handle = fopen ("php://stdin","r");
$response = trim(fgets($handle));
fclose($handle);
}
if($response == "y"){
echo "Removing old files\n";
removeOldFiles($tracks,$memory_key,$library_path);
RemoveEmptySubFolders($memory_key);
}
echo "Syncing Playlist\n";
syncPlaylist($tracks,$memory_key,$library_path);
function removeOldFiles($tracks,$memory_key,$library_path) {
for ($i=0;$i<count($tracks);$i++) {
$input_path = $tracks[$i];
$output_path = str_replace($library_path,$memory_key,$input_path);
if (substr($input_path,-4) != ".mp3" && substr($input_path,-4) != ".m4a") {
continue;
}
if (substr($output_path,-4) == ".m4a") {
$output_path = substr($output_path,0,-4) . ".mp3";
}
$sync_files[$output_path] = true;
}
recurseRemove($memory_key,$sync_files);
}
function recurseRemove($main, $sync_files) {
$dirHandle = opendir($main);
while($file = readdir($dirHandle)){
if(is_dir($main.$file."/") && $file != '.' && $file != '..'){
recurseRemove($main.$file."/",$sync_files);
} else {
if (!is_dir($main.$file."/") && substr($file,0,1) != '.') {
$search = $main . $file;
if (!$sync_files[$search]) {
unlink($search);
}
}
}
}
closedir($dirHandle);
}
function RemoveEmptySubFolders($path)
{
$empty=true;
foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file)
{
$empty &= is_dir($file) && RemoveEmptySubFolders($file);
}
return $empty && rmdir($path);
}
function syncPlaylist($tracks,$memory_key,$library_path) {
global $convert_bitrate;
for ($i=0;$i<count($tracks);$i++) {
$input_path = $tracks[$i];
$output_path = str_replace($library_path,$memory_key,$input_path);
if (substr($input_path,-4) != ".mp3" && substr($input_path,-4) != ".m4a") {
continue;
}
if (substr($output_path,-4) == ".m4a") {
$output_path = substr($output_path,0,-4) . ".mp3";
}
if (file_exists($output_path) && filesize($output_path) > 0) {
continue;
}
makeDirectoryStructure($memory_key,$output_path);
if (is_numeric($convert_bitrate)) {
$bitrate = getBitRate($input_path);
if ($bitrate > ($convert_bitrate + 32)) {
echo "Converting $input_path\n";
convertAndCopy($input_path, $output_path);
} else {
echo "Copying $input_path\n";
@copy($input_path,$output_path);
}
} else {
echo "Copying $input_path\n";
@copy($input_path,$output_path);
}
}
}
function convertAndCopy($input_path, $output_path) {
global $convert_bitrate;
$ret = system('./ffmpeg -i "' . $input_path .'" -ab ' . $convert_bitrate .' "' . $output_path . '" 2>/dev/null');
}
function makeDirectoryStructure($memory_key,$output_path) {
$output_path = str_replace($memory_key,"",$output_path);
$memory_key = substr($memory_key,0,strlen($memory_key)-1);
$parts = explode("/",$output_path);
$base_path = $memory_key;
for ($i=0;$i<count($parts)-1;$i++) {
$base_path = $base_path . "/" . $parts[$i];
@mkdir($base_path);
}
}
function getBitRate($path) {
@unlink('out.tmp');
$ret = system('./ffprobe "' . $path . '" 2> out.tmp');
$ret = file_get_contents('out.tmp');
$lines = explode("\n",$ret);
for($i=0;$i<count($lines);$i++) {
$parts = explode("bitrate: ",$lines[$i]);
if ($parts[1]) {
$bit = trim($parts[1]);
$bit = str_replace('kb/s',"",$bit);
return trim($bit);
}
}
}
function getMemoryKey() {
$ret = system('df -h | grep Volumes > out.tmp');
$ret = file_get_contents('out.tmp');
$parts = explode("\n",$ret);
for($i=0;$i<count($parts);$i++) {
$new = explode("/Volumes/",$parts[$i]);
if ($new[1] != "") {
$options[] = $new[1];
}
}
echo "\nPlease select a destination for your music: \n\n";
for ($i=0;$i<count($options);$i++) {
$to_select = $i + 1;
echo " $to_select : $options[$i]\n";
}
$total = count($options);
echo "\n\nEnter an option between 1 and $total:\n";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$selected = $line - 1;
if(!$options[$selected]){
error("Failed to recongise input");
}
return "/Volumes/" . $options[$selected];
}
function getLibraryPath($tracks) {
$path = $tracks[0];
if (!file_exists($path)) {
error("Could not find the first track $path, FATAL ERROR");
}
$parts = explode("/",$path);
$count = count($parts);
for ($i=$count-1;$i>0;$i--) {
$common_path = "";
for ($j=0;$j<$i;$j++) {
if ($parts[$j] != "") {
$common_path .= "/" . $parts[$j];
}
}
$path_count = checkPath($common_path,$tracks);
if ($path_count == count($tracks)) {
return $common_path;
}
}
return undef;
}
function checkPath($common_path,$tracks) {
$total = 0;
for($i=0;$i<count($tracks);$i++) {
if (substr($tracks[$i],0,strlen($common_path)) == $common_path) {
$total++;
}
}
return $total;
}
?>