feat: search and download from YouTube Music
- search music.youtube.com with the Songs filter and resolve each hit so results carry canonical artist, album and duration - fall back to plain ytsearch when the Music search fails - download the Music watch URL first and the regular YouTube URL second - parse explicit artist fields and drop channel/album/playlist entries - update the smoke stub, docs and tests
This commit is contained in:
1 parent
61d8b58223
commit
515b9bbe1b
6 files changed
+215
-29
No files matched your search
@@ -6,14 +6,20 @@ import java.util.concurrent.TimeUnit
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
/**
|
||||
* Downloads a single YouTube video as an MP3 with embedded metadata, so the
|
||||
* indexer can derive artist and title from the file itself.
|
||||
* 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,
|
||||
@@ -21,6 +27,17 @@ class YtDlpDownloader(
|
||||
|
||||
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",
|
||||
@@ -33,13 +50,14 @@ class YtDlpDownloader(
|
||||
"--convert-thumbnails",
|
||||
"jpg",
|
||||
"--no-playlist",
|
||||
"--ignore-config",
|
||||
"--no-cache-dir",
|
||||
"--no-progress",
|
||||
"--print",
|
||||
"after_move:filepath",
|
||||
"-o",
|
||||
targetDir.resolve("%(id)s.%(ext)s").toString(),
|
||||
"https://www.youtube.com/watch?v=$videoId",
|
||||
url,
|
||||
)
|
||||
.redirectErrorStream(false)
|
||||
.start()
|
||||
@@ -61,7 +79,7 @@ class YtDlpDownloader(
|
||||
process.destroyForcibly()
|
||||
reader.join(2_000)
|
||||
errorReader.join(2_000)
|
||||
throw IllegalStateException("yt-dlp download timed out after ${timeoutSeconds}s")
|
||||
throw YtDlpTimeoutException("yt-dlp download timed out after ${timeoutSeconds}s")
|
||||
}
|
||||
reader.join(2_000)
|
||||
errorReader.join(2_000)
|
||||
@@ -79,4 +97,23 @@ class YtDlpDownloader(
|
||||
}
|
||||
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)
|
||||
Reference in new issue
Block a user