From b5c58e301b96b2bd067d07fc0e203a2db502a216 Mon Sep 17 00:00:00 2001 From: Arumugam J Date: Sat, 17 Aug 2024 17:38:17 -0700 Subject: [PATCH] Copy missing specs --- .../kotlin/viewmodels/VideoListViewModel.kt | 2 +- gradle/libs.versions.toml | 38 ++++++++++++++----- .../jsixface/codexvert/api/ConversionApi.kt | 19 +++++----- .../jsixface/codexvert/plugins/Watchers.kt | 5 ++- .../jsixface/codexvert/route/VideoRoutes.kt | 3 +- .../io/github/jsixface/common/MediaModels.kt | 2 + 6 files changed, 45 insertions(+), 24 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/viewmodels/VideoListViewModel.kt b/composeApp/src/commonMain/kotlin/viewmodels/VideoListViewModel.kt index c687248..05b81db 100644 --- a/composeApp/src/commonMain/kotlin/viewmodels/VideoListViewModel.kt +++ b/composeApp/src/commonMain/kotlin/viewmodels/VideoListViewModel.kt @@ -39,7 +39,7 @@ class VideoListViewModel(private val client: HttpClient) { suspend fun submitJob(videoFile: VideoFile, conversions: Map) { client.post(Api.Videos.Video(path = videoFile.fileName)) { - setBody(conversions.entries.map { it.toPair() }) + setBody(conversions) contentType(ContentType.Application.Cbor) } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 99406da..fc4237d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -42,15 +42,35 @@ logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } napier = { module = "io.github.aakira:napier", version.ref = "napier" } [bundles] -koin-server = ["koin-core", "koin-ktor", "koin-log"] -koin-client = ["koin-core", "koin-compose"] -koin-test = ["koin-test", "koin-test-junit4"] - -ktor-client = ["ktor-client-core", "ktor-client-logging", "ktor-client-content-negotiation", - "ktor-serialization-kotlinx-cbor", "ktor-client-resources"] -ktor-server = ["ktor-server-core", "ktor-server-netty", "ktor-server-resources", - "ktor-server-config-yaml", "ktor-serialization-kotlinx-json", "ktor-serialization-kotlinx-cbor", - "ktor-server-content-negotiation"] +koin-server = [ + "koin-core", + "koin-ktor", + "koin-log" +] +koin-client = [ + "koin-core", + "koin-compose" +] +koin-test = [ + "koin-test", + "koin-test-junit4" +] +ktor-client = [ + "ktor-client-core", + "ktor-client-logging", + "ktor-client-content-negotiation", + "ktor-serialization-kotlinx-cbor", + "ktor-client-resources" +] +ktor-server = [ + "ktor-server-core", + "ktor-server-netty", + "ktor-server-resources", + "ktor-server-config-yaml", + "ktor-serialization-kotlinx-json", + "ktor-serialization-kotlinx-cbor", + "ktor-server-content-negotiation" +] [plugins] diff --git a/server/src/main/kotlin/io/github/jsixface/codexvert/api/ConversionApi.kt b/server/src/main/kotlin/io/github/jsixface/codexvert/api/ConversionApi.kt index 74f5c81..c95c1a2 100644 --- a/server/src/main/kotlin/io/github/jsixface/codexvert/api/ConversionApi.kt +++ b/server/src/main/kotlin/io/github/jsixface/codexvert/api/ConversionApi.kt @@ -59,7 +59,7 @@ class ConversionApi { jobs.removeAll { it.progress.value == -1 || it.progress.value == 100 } } - fun startConversion(file: VideoFile, convSpecs: List>): Boolean { + fun startConversion(file: VideoFile, convSpecs: Map): Boolean { val jobId = UUID.randomUUID().toString() val newDir = File(workspace, jobId).apply { mkdirs() } @@ -79,7 +79,7 @@ class ConversionApi { private suspend fun startJob( file: VideoFile, - convSpecs: List>, + convSpecs: Map, outFile: File, updates: MutableStateFlow ) = coroutineScope { @@ -148,7 +148,7 @@ class ConversionApi { private fun buildCommand( file: VideoFile, - convSpecs: List>, + convSpecs: Map, outFile: File ): List = listOf( "ffmpeg", @@ -159,13 +159,12 @@ class ConversionApi { outFile.absolutePath ) - private fun conversionParams(convSpecs: List>, file: VideoFile): List { + private fun conversionParams(convSpecs: Map, file: VideoFile): List { val result = mutableListOf() - val subTitleWithoutSpecs = - file.subtitles.filter { convSpecs.none { c -> c.first == it } }.map { it to Conversion.Copy } - val allConversion = convSpecs + subTitleWithoutSpecs - allConversion.forEachIndexed { i, (track, conv) -> - + val missingTracks = (file.subtitles + file.videos + file.audios).filter { it !in convSpecs.keys } + val copyTracks = missingTracks.map { it to Conversion.Copy } + val allConversion = convSpecs + copyTracks + allConversion.toList().forEachIndexed { i, (track, conv) -> when (conv) { Conversion.Copy -> result += listOf( "-map", @@ -189,7 +188,7 @@ class ConversionApi { data class ConvertingJob( val videoFile: VideoFile, - val convSpecs: List>, + val convSpecs: Map, val outFile: File, var job: Job?, val progress: MutableStateFlow = MutableStateFlow(0), diff --git a/server/src/main/kotlin/io/github/jsixface/codexvert/plugins/Watchers.kt b/server/src/main/kotlin/io/github/jsixface/codexvert/plugins/Watchers.kt index b5755be..620c6fd 100644 --- a/server/src/main/kotlin/io/github/jsixface/codexvert/plugins/Watchers.kt +++ b/server/src/main/kotlin/io/github/jsixface/codexvert/plugins/Watchers.kt @@ -6,6 +6,7 @@ import io.github.jsixface.codexvert.api.VideoApi import io.github.jsixface.codexvert.logger import io.github.jsixface.common.Codec import io.github.jsixface.common.Conversion +import io.github.jsixface.common.isDolby import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -52,10 +53,10 @@ class Watchers(private val videoApi: VideoApi, private val conversionApi: Conver // Make sure those files are not already in the job queue. val files = videoApi.getVideos() files.values.forEach { videoFile -> - val dolbyTracks = videoFile.audios.filter { it.codec.lowercase() in listOf("eac3", "ac3") } + val dolbyTracks = videoFile.audios.filter { it.isDolby() } val job = conversionApi.jobs.find { it.videoFile.fileName == videoFile.fileName } if (dolbyTracks.isNotEmpty() && job == null) { - val conversionSpecs = dolbyTracks.map { Pair(it, Conversion.Convert(Codec.AAC)) } + val conversionSpecs = dolbyTracks.associateWith { Conversion.Convert(Codec.AAC) } logger.info("Auto Converting for ${videoFile.fileName}") conversionApi.startConversion(videoFile, conversionSpecs) } diff --git a/server/src/main/kotlin/io/github/jsixface/codexvert/route/VideoRoutes.kt b/server/src/main/kotlin/io/github/jsixface/codexvert/route/VideoRoutes.kt index f857c0d..e0188b0 100644 --- a/server/src/main/kotlin/io/github/jsixface/codexvert/route/VideoRoutes.kt +++ b/server/src/main/kotlin/io/github/jsixface/codexvert/route/VideoRoutes.kt @@ -56,10 +56,9 @@ fun Route.videoRoutes() { return@post } logger.info("Converting the video: ${video.path}") - val data = call.receive>>() + val data = call.receive>() logger.info("Got the data: $data") conversionApi.startConversion(videoFile, data) call.respond("OK") - } } diff --git a/shared/src/commonMain/kotlin/io/github/jsixface/common/MediaModels.kt b/shared/src/commonMain/kotlin/io/github/jsixface/common/MediaModels.kt index e5e75ac..54da9bd 100644 --- a/shared/src/commonMain/kotlin/io/github/jsixface/common/MediaModels.kt +++ b/shared/src/commonMain/kotlin/io/github/jsixface/common/MediaModels.kt @@ -41,6 +41,8 @@ data class MediaTrack( val codec: String ) +fun MediaTrack.isDolby() = codec.lowercase() in listOf("ac3", "eac3") + @Serializable data class VideoFile( val path: String,