Skip to content

Instantly share code, notes, and snippets.

@knivey
Last active June 22, 2021 05:30
Show Gist options
  • Save knivey/97bee62b4d6ddc11a2cef328131f54de to your computer and use it in GitHub Desktop.
Save knivey/97bee62b4d6ddc11a2cef328131f54de to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
# downloads done with:
# youtube-dl --proxy socks5://127.0.0.1:8080 --write-info-json -i -o "%(title)s (%(id)s).%(ext)s" -f bestaudio --add-metadata --extract-audio --download-archive dl.txt
//$files = [];
$files = glob("*.m4a");
$files = array_merge($files, glob("*.opus"));
foreach ($files as $f) {
$ext = pathinfo($f, PATHINFO_EXTENSION);
if(!preg_match("@((.+) \(.+\))\.". preg_quote($ext, '@') . "@", $f, $m)) {
echo "$f failed\n";
continue;
}
//print_r($m);
$newDir = "out/$m[2]";
$doneDir = "done";
$jfile = "$m[1].info.json";
$j = json_decode(file_get_contents($jfile));
if(isset($j->chapters)) {
@mkdir($newDir, 0700, true);
foreach($j->chapters as $t) {
$s = $t->start_time;
$d = $t->end_time - $s;
$out = escapeshellarg("$newDir/" . str_replace('/', '&', $t->title));
$ff = escapeshellarg($f);
if(!preg_match("@(\d+) +- +(.+)@", $t->title, $mm)) {
$meta = "-metadata title=" . escapeshellarg($t->title);
} else {
$meta = "-metadata title=" . escapeshellarg($mm[2]);
$meta .= " -metadata track=" . escapeshellarg($mm[1]);
}
$meta .= " -metadata album=" . escapeshellarg($m[2]);
$cmd = "ffmpeg -i $ff -acodec copy -map_chapters -1 -map_metadata -1 $meta -ss $s -t $d $out.$ext";
echo "$cmd\n";
system($cmd, $rc);
if($rc != 0) {
die("error?\n");
}
}
@mkdir($doneDir);
rename($jfile, "$doneDir/$jfile");
rename($f, "$doneDir/$f");
}
//break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment