-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotag.php
156 lines (118 loc) · 3.72 KB
/
autotag.php
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
<?PHP
if (count($argv)!=2) { echo ("Use <path>\n"); exit(0); }
$path = $argv[1];
echo($path."\n");
$song = shazam($path);
$track = $song['track'];
$title = $track['title'];
if (strlen($title)<1) { echo("unknown song\n"); exit(0); }
$artist = $track['subtitle'];
if (substr($artist, 0, 4)=="The ") $artist = substr($artist, 4);
$album = $track['sections'][0]['metadata'][0]['text'];
echo $title."\n";
echo $artist."\n";
echo $album."\n";
$filename = $artist." - ".$title;
$filename = formatFilename($filename);
if (strlen($filename)<5) { echo("unknown song 2\n"); exit(0); }
$filenameMp3 = $filename.".mp3";
$filenameJpgA = $filename."a.jpg";
$filenameJpgB = $filename."b.jpg";
$urlCover = $track['share']['image'];
audioToMp3($path, $filenameMp3, getMaxVol($path));
$cmd = "eyeD3 ".escapeshellarg($filenameMp3)." -a ".escapeshellarg($artist)." -t ".escapeshellarg($title)." -A ".escapeshellarg($album)." --to-v2.3";
if (strlen($urlCover)>6) {
curlc($urlCover, $filenameJpgA);
resize($filenameJpgA, $filenameJpgB, 600, 600);
$cmd .= " --add-image ".escapeshellarg($filenameJpgB).":FRONT_COVER";
}
exec($cmd);
if (strlen($urlCover)>6) {
unlink($filenameJpgA);
unlink($filenameJpgB);
}
echo "Created ".$filenameMp3."\n";
function getMaxVol($path) {
$res = cmd("ffmpeg -i \"".$path."\" -hide_banner -af volumedetect -vn -sn -dn -f null /dev/null 2>&1");
$lines = explode("\n", $res);
foreach($lines as $line) {
if (strpos($line, "max_volume")===FALSE) continue;
$maxVol = getStrBtn($line, "max_volume: ", " dB");
return floatval($maxVol);
}
return 0;
}
function audioToMp3($src, $dst, $vol) {
if ($vol>=0) {
$cmd = "ffmpeg -i \"".$src."\" -hide_banner -codec:a libmp3lame -b:a 320k \"".$dst."\"";
} else {
$vol = $vol * (-1.0);
$cmd = "ffmpeg -i \"".$src."\" -hide_banner -af \"volume=".$vol."dB\" -codec:a libmp3lame -b:a 320k \"".$dst."\"";
}
echo("cmd ".$cmd."\n");
exec($cmd);
}
function getStrBtn($str_content, $str_start, $str_end)
{
$pos1 = strpos($str_content, $str_start);
if ($pos1===FALSE) return "";
$pos1 = $pos1 + strlen($str_start);
$pos2 = strpos($str_content, $str_end, $pos1);
if ($pos2===FALSE) return "";
$res_len = $pos2 - $pos1;
$res = substr($str_content, $pos1, $res_len);
return $res;
}
function shazam($path) {
$res = cmd("songrec audio-file-to-recognized-song \"".$path."\"");
//echo($res."\n");
return json_decode($res, true);
}
function resize($src, $dst, $width, $height) {
exec("convert \"".$src."\" -resize ".$width."x".$height." -quality 100 \"".$dst."\"");
}
function curlc($url, $path) {
exec('curl -q -4 -s --user-agent "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36" --header "Accept-Language: en-US,en;q=0.5" --insecure --output "'.$path.'" "'.$url.'"');
}
function formatFilename($cad) {
$cad = str_replace("/", "-", $cad);
$cad = str_replace("_", "-", $cad);
$imax = strlen($cad);
$char_val = " ";
$char_num = 0;
$char_valid = false;
$res = "";
for ($i=0; $i<$imax; $i++) {
$char_val = substr($cad, $i, 1);
$char_num = ord($char_val);
$char_valid = false;
if ($char_num >= ord("0") && $char_num <= ord("9")) {
$char_valid = true;
}
if ($char_num >= ord("a") && $char_num <= ord("z")) {
$char_valid = true;
}
if ($char_num >= ord("A") && $char_num <= ord("Z")) {
$char_valid = true;
}
if ($char_val=="-") $char_valid = true;
if ($char_val==" ") $char_valid = true;
if ($char_val=="(") $char_valid = true;
if ($char_val==")") $char_valid = true;
if ($char_valid==true) {
$res .= $char_val;
}
}
return $res;
}
function cmd($ruta)
{
$fp = popen($ruta, "r");
$res = "";
while(!feof($fp)) {
$res .= fread($fp, 1024);
}
fclose($fp);
return $res;
}
?>