feat: transcode streams with ffmpeg
- FfmpegTranscoder with maxBitRate, format fallback to MP3 and timeOffset - direct play when no transcode is required, preserving HTTP Range support - transcoded responses are chunked audio/mpeg with process cleanup - unit tests for policy and real ffmpeg output - smoke test covers maxBitRate, timeOffset and format fallback
This commit is contained in:
1 parent
722f486c3a
commit
18a1d7cd19
9 files changed
+254
-4
No files matched your search
@@ -53,6 +53,7 @@ import win.taptap.musicmanager.db.SongsTable
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.subsonic.routes.subsonicRoutes
|
||||
import win.taptap.musicmanager.transcoding.FfmpegTranscoder
|
||||
import win.taptap.musicmanager.util.IdGenerator
|
||||
|
||||
class BrowsingRoutesTest {
|
||||
@@ -420,6 +421,7 @@ class BrowsingRoutesTest {
|
||||
users = seed.users,
|
||||
library = seed.library,
|
||||
coverArtService = seed.coverArt,
|
||||
transcoder = FfmpegTranscoder("ffmpeg"),
|
||||
startScan = { false },
|
||||
scanStatus = { ScanStatus(false, 0) },
|
||||
)
|
||||
|
||||
@@ -28,6 +28,7 @@ import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.subsonic.routes.subsonicRoutes
|
||||
import win.taptap.musicmanager.transcoding.FfmpegTranscoder
|
||||
|
||||
class SystemRoutesTest {
|
||||
|
||||
@@ -178,6 +179,7 @@ class SystemRoutesTest {
|
||||
listOf(MusicFolder(1, "Music", FolderType.MUSIC, tempDir)),
|
||||
),
|
||||
coverArtService = CoverArtService(tempDir.resolve("data")),
|
||||
transcoder = FfmpegTranscoder("ffmpeg"),
|
||||
startScan = startScan,
|
||||
scanStatus = scanStatus,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package win.taptap.musicmanager.transcoding
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import org.junit.jupiter.api.Assumptions.assumeTrue
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
|
||||
class FfmpegTranscoderTest {
|
||||
|
||||
@TempDir
|
||||
lateinit var tempDir: Path
|
||||
|
||||
private val transcoder = FfmpegTranscoder("ffmpeg")
|
||||
|
||||
@Test
|
||||
fun `direct play is used when no transcode is requested`() {
|
||||
assertFalse(transcoder.shouldTranscode(320, null, null, 0))
|
||||
assertFalse(transcoder.shouldTranscode(128, 0, "mp3", 0))
|
||||
assertFalse(transcoder.shouldTranscode(128, 128, "mp3", 0))
|
||||
assertFalse(transcoder.shouldTranscode(128, 320, "mp3", 0))
|
||||
assertFalse(transcoder.shouldTranscode(128, null, "raw", 0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `transcodes for lower bitrate time offsets and foreign formats`() {
|
||||
assertTrue(transcoder.shouldTranscode(320, 128, null, 0))
|
||||
assertTrue(transcoder.shouldTranscode(128, null, null, 30))
|
||||
assertTrue(transcoder.shouldTranscode(128, null, "flac", 0))
|
||||
assertTrue(transcoder.shouldTranscode(128, 96, "mp3", 10))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ffmpeg produces mp3 bytes`() {
|
||||
assumeTrue(ffmpegAvailable(), "ffmpeg is not installed")
|
||||
val source = createMp3()
|
||||
|
||||
val process = transcoder.start(source, maxBitRate = 64, timeOffset = 0)
|
||||
val bytes = process.inputStream.readBytes()
|
||||
assertTrue(process.waitFor(10, TimeUnit.SECONDS))
|
||||
assertTrue(bytes.isNotEmpty(), "transcoded output is empty")
|
||||
// MP3 frames start with an 0xFF sync byte (or an ID3 header when metadata is kept).
|
||||
assertTrue(bytes[0] == 0xFF.toByte() || bytes[0] == 'I'.code.toByte())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `time offset produces a shorter stream`() {
|
||||
assumeTrue(ffmpegAvailable(), "ffmpeg is not installed")
|
||||
val source = createMp3()
|
||||
|
||||
val full = transcoder.start(source, maxBitRate = 64, timeOffset = 0).let { process ->
|
||||
val bytes = process.inputStream.readBytes()
|
||||
process.waitFor(10, TimeUnit.SECONDS)
|
||||
bytes
|
||||
}
|
||||
val offset = transcoder.start(source, maxBitRate = 64, timeOffset = 1).let { process ->
|
||||
val bytes = process.inputStream.readBytes()
|
||||
process.waitFor(10, TimeUnit.SECONDS)
|
||||
bytes
|
||||
}
|
||||
|
||||
assertTrue(full.isNotEmpty())
|
||||
assertTrue(offset.isNotEmpty())
|
||||
assertTrue(offset.size < full.size, "offset stream should be shorter")
|
||||
}
|
||||
|
||||
private fun createMp3(): Path {
|
||||
val target = tempDir.resolve("source.mp3")
|
||||
val process = ProcessBuilder(
|
||||
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
|
||||
"-f", "lavfi", "-i", "sine=frequency=440:duration=3",
|
||||
"-metadata", "title=Transcode Test",
|
||||
"-q:a", "9",
|
||||
target.toString(),
|
||||
).redirectError(ProcessBuilder.Redirect.DISCARD).start()
|
||||
|
||||
assertTrue(process.waitFor(20, TimeUnit.SECONDS))
|
||||
assertTrue(Files.size(target) > 0)
|
||||
return target
|
||||
}
|
||||
|
||||
private fun ffmpegAvailable(): Boolean = runCatching {
|
||||
val process = ProcessBuilder("ffmpeg", "-version")
|
||||
.redirectErrorStream(true)
|
||||
.start()
|
||||
process.inputStream.readBytes()
|
||||
process.waitFor(5, TimeUnit.SECONDS)
|
||||
true
|
||||
}.getOrDefault(false)
|
||||
}
|
||||
Reference in new issue
Block a user