sclass DownloadAudio { S url; File dir = downloadedAudioDir(); new LS options; S extension = ".mp3"; // used to have .opus but got bad quality sometimes int maxFileSizeInMB = 20; S cmd; *(WithURL url) { this(url?.url()); } *(S *url) { options.add("-U --no-playlist --no-mtime --write-description --write-info-json"); options.add("--write-annotations --write-thumbnail --print-json --restrict-filenames"; options.add("-x"); // audio only options.add("--audio-format " + dropDotPrefix(extension)); // demand our format } DownloadedMedia get() { if (empty(url)) null; installFFMPEGIfMissing(); options.add("--max-filesize " + maxFileSizeInMB + "m"); S fn = md5FileName(url); File infoFile = newFile(dir, fn + ".info"); S json = loadTextFile(infoFile); if (isPossibleJSONMap(json)) { DownloadedMedia dm = new(null, json, extension); if (fileExists(dm!)) ret dm; } saveTextFile(newFile(dir, fn + ".url"), url); cmd = pqO(installYouTubeDL()) + " " + joinWithSpace(options) + " -o " + pqO(newFile(dir, youTubeDLDefaultFileNameTemplate())) + " " + safePlatformQuote(url); S output = backtick_verbose(cmd); saveTextFile(infoFile, output); ret new DownloadedMedia(cmd, output, extension); } }