Skip to content

Commit

Permalink
Copy missing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsixface committed Aug 18, 2024
1 parent 820bf0c commit b5c58e3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class VideoListViewModel(private val client: HttpClient) {

suspend fun submitJob(videoFile: VideoFile, conversions: Map<MediaTrack, Conversion>) {
client.post(Api.Videos.Video(path = videoFile.fileName)) {
setBody(conversions.entries.map { it.toPair() })
setBody(conversions)
contentType(ContentType.Application.Cbor)
}
}
Expand Down
38 changes: 29 additions & 9 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ConversionApi {
jobs.removeAll { it.progress.value == -1 || it.progress.value == 100 }
}

fun startConversion(file: VideoFile, convSpecs: List<Pair<MediaTrack, Conversion>>): Boolean {
fun startConversion(file: VideoFile, convSpecs: Map<MediaTrack, Conversion>): Boolean {
val jobId = UUID.randomUUID().toString()
val newDir = File(workspace, jobId).apply { mkdirs() }

Expand All @@ -79,7 +79,7 @@ class ConversionApi {

private suspend fun startJob(
file: VideoFile,
convSpecs: List<Pair<MediaTrack, Conversion>>,
convSpecs: Map<MediaTrack, Conversion>,
outFile: File,
updates: MutableStateFlow<Int>
) = coroutineScope {
Expand Down Expand Up @@ -148,7 +148,7 @@ class ConversionApi {

private fun buildCommand(
file: VideoFile,
convSpecs: List<Pair<MediaTrack, Conversion>>,
convSpecs: Map<MediaTrack, Conversion>,
outFile: File
): List<String> = listOf(
"ffmpeg",
Expand All @@ -159,13 +159,12 @@ class ConversionApi {
outFile.absolutePath
)

private fun conversionParams(convSpecs: List<Pair<MediaTrack, Conversion>>, file: VideoFile): List<String> {
private fun conversionParams(convSpecs: Map<MediaTrack, Conversion>, file: VideoFile): List<String> {
val result = mutableListOf<String>()
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",
Expand All @@ -189,7 +188,7 @@ class ConversionApi {

data class ConvertingJob(
val videoFile: VideoFile,
val convSpecs: List<Pair<MediaTrack, Conversion>>,
val convSpecs: Map<MediaTrack, Conversion>,
val outFile: File,
var job: Job?,
val progress: MutableStateFlow<Int> = MutableStateFlow(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ fun Route.videoRoutes() {
return@post
}
logger.info("Converting the video: ${video.path}")
val data = call.receive<List<Pair<MediaTrack, Conversion>>>()
val data = call.receive<Map<MediaTrack, Conversion>>()
logger.info("Got the data: $data")
conversionApi.startConversion(videoFile, data)
call.respond("OK")

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b5c58e3

Please sign in to comment.