package win.taptap.musicmanager.download import java.nio.file.Files import java.nio.file.Path import java.util.concurrent.TimeUnit import kotlin.concurrent.thread /** * Downloads a single track as an MP3 with embedded metadata, so the indexer can * derive artist, title, album and cover art from the file itself. */ fun interface AudioDownloader { /** Downloads [videoId] into [targetDir] and returns the resulting MP3 file. */ fun download(videoId: String, targetDir: Path): Path } /** * Downloads from the YouTube Music player first, which is what a search hit * points at: yt-dlp then exposes the song's artist, album and release year and * `--embed-metadata` writes them into the MP3. Some videos only exist on the * regular YouTube host, so that URL is tried once as a fallback. */ class YtDlpDownloader( private val executable: String, private val timeoutSeconds: Long = 600, ) : AudioDownloader { override fun download(videoId: String, targetDir: Path): Path { Files.createDirectories(targetDir) return try { runDownload("$MUSIC_WATCH_URL$videoId", videoId, targetDir) } catch (e: YtDlpTimeoutException) { throw e } catch (e: IllegalStateException) { clearPartials(videoId, targetDir) runDownload("$YOUTUBE_WATCH_URL$videoId", videoId, targetDir) } } private fun runDownload(url: String, videoId: String, targetDir: Path): Path { val process = ProcessBuilder( executable, "-x", "--audio-format", "mp3", "--audio-quality", "0", "--embed-metadata", "--embed-thumbnail", "--convert-thumbnails", "jpg", "--no-playlist", "--ignore-config", "--no-cache-dir", "--no-progress", "--print", "after_move:filepath", "-o", targetDir.resolve("%(id)s.%(ext)s").toString(), url, ) .redirectErrorStream(false) .start() val output = StringBuilder() val errors = StringBuilder() val reader = thread(isDaemon = true, name = "yt-dlp-download-stdout") { process.inputStream.bufferedReader().useLines { lines -> lines.forEach { output.appendLine(it) } } } val errorReader = thread(isDaemon = true, name = "yt-dlp-download-stderr") { process.errorStream.bufferedReader().useLines { lines -> lines.forEach { errors.appendLine(it) } } } if (!process.waitFor(timeoutSeconds, TimeUnit.SECONDS)) { process.destroyForcibly() reader.join(2_000) errorReader.join(2_000) throw YtDlpTimeoutException("yt-dlp download timed out after ${timeoutSeconds}s") } reader.join(2_000) errorReader.join(2_000) if (process.exitValue() != 0) { val detail = (errors.toString().ifBlank { output.toString() }).take(400).trim() throw IllegalStateException("yt-dlp exited with ${process.exitValue()}: $detail") } val printed = output.lines() .map { it.trim() } .lastOrNull { it.endsWith(".mp3", ignoreCase = true) } val file = printed?.let { Path.of(it) } ?: targetDir.resolve("$videoId.mp3") if (!Files.isRegularFile(file)) { throw IllegalStateException("yt-dlp did not produce an MP3 file") } return file } /** A failed first attempt may leave `.part`/`.webm` files behind. */ private fun clearPartials(videoId: String, targetDir: Path) { runCatching { Files.list(targetDir).use { stream -> stream .filter { it.fileName.toString().startsWith(videoId) } .forEach { runCatching { Files.deleteIfExists(it) } } } } } private companion object { const val MUSIC_WATCH_URL = "https://music.youtube.com/watch?v=" const val YOUTUBE_WATCH_URL = "https://www.youtube.com/watch?v=" } } /** A timeout is not retried against the other host: the network is the problem. */ class YtDlpTimeoutException(message: String) : IllegalStateException(message)