feat: download accepted recommendations and track longform progress
Accepted recommendations without a library match now enter a download queue. A worker downloads the MP3 with yt-dlp into "<music>/AudioManager Imports", runs a scan, matches the new song, links it to the recommendation and adds it as a seed. Jobs retry three times and expose queued/downloading/done/failed state (plus the error) on the recommendation DTO, so clients can show progress and retry. Downloads can be disabled and the target folder chosen. Long-form gets resume points: GET/PUT /longform/progress, scoped to longform folders and to the owner, with automatic completion detection. Longform stays excluded from moods and radio. The chart mounts the music hostPath writable for imports and passes the new DOWNLOAD_* settings; version 0.2.3.
This commit is contained in:
1 parent
51d1b20bf6
commit
a5af1ee83f
18 files changed
+996
-9
No files matched your search
@@ -0,0 +1,67 @@
|
||||
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 YouTube video as an MP3 with embedded metadata, so the
|
||||
* scanner can derive artist and title from the file itself.
|
||||
*/
|
||||
class YtDlpDownloader(
|
||||
private val executable: String,
|
||||
private val timeoutSeconds: Long = 600,
|
||||
) : AudioDownloader {
|
||||
|
||||
override fun download(videoId: String, title: String, artist: String, targetDir: Path): Path {
|
||||
Files.createDirectories(targetDir)
|
||||
val process = ProcessBuilder(
|
||||
executable,
|
||||
"-x",
|
||||
"--audio-format",
|
||||
"mp3",
|
||||
"--audio-quality",
|
||||
"0",
|
||||
"--embed-metadata",
|
||||
"--no-playlist",
|
||||
"--no-cache-dir",
|
||||
"--no-progress",
|
||||
"--print",
|
||||
"after_move:filepath",
|
||||
"-o",
|
||||
targetDir.resolve("%(id)s.%(ext)s").toString(),
|
||||
"https://www.youtube.com/watch?v=$videoId",
|
||||
)
|
||||
.redirectErrorStream(true)
|
||||
.start()
|
||||
|
||||
val output = StringBuilder()
|
||||
val reader = thread(isDaemon = true, name = "yt-dlp-download") {
|
||||
process.inputStream.bufferedReader().useLines { lines ->
|
||||
lines.forEach { output.appendLine(it) }
|
||||
}
|
||||
}
|
||||
|
||||
if (!process.waitFor(timeoutSeconds, TimeUnit.SECONDS)) {
|
||||
process.destroyForcibly()
|
||||
reader.join(2_000)
|
||||
throw IllegalStateException("yt-dlp download timed out after ${timeoutSeconds}s")
|
||||
}
|
||||
reader.join(2_000)
|
||||
if (process.exitValue() != 0) {
|
||||
throw IllegalStateException(
|
||||
"yt-dlp exited with ${process.exitValue()}: ${output.toString().take(400).trim()}",
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user