-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0043c9
commit 218235c
Showing
7 changed files
with
141 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
core/src/main/kotlin/io/github/shaksternano/borgar/core/io/task/FFmpegTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.github.shaksternano.borgar.core.io.task | ||
|
||
import io.github.shaksternano.borgar.core.collect.indicesOf | ||
import io.github.shaksternano.borgar.core.exception.ErrorResponseException | ||
import io.github.shaksternano.borgar.core.io.* | ||
import io.github.shaksternano.borgar.core.util.splitWords | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.bytedeco.ffmpeg.ffmpeg | ||
import org.bytedeco.javacpp.Loader | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.absolutePathString | ||
|
||
private val FFMPEG_PATH: String = Loader.load(ffmpeg::class.java) | ||
|
||
class FFmpegTask( | ||
private val arguments: String, | ||
) : MappedFileTask() { | ||
|
||
override suspend fun process(input: DataSource): DataSource = input.useFile { fileInput -> | ||
val inputPath = fileInput.path | ||
val splitArguments = arguments.splitWords() | ||
val inputIndices = splitArguments.indicesOf("-i") | ||
val ffmpegArguments = splitArguments.filterIndexed { index, _ -> | ||
(index !in inputIndices && index - 1 !in inputIndices) | ||
}.ifEmpty { | ||
listOf(inputPath.toString()) | ||
} | ||
val output = ffmpegArguments.last() | ||
if (fileExtension(output).isBlank()) { | ||
throw ErrorResponseException("Output filename must have an extension!") | ||
} | ||
val outputFilename = Path(output).filename | ||
val outputPath = getTemporaryFile(outputFilename) | ||
outputPath.toFile().deleteOnExit() | ||
val ffmpegCommand = listOf(FFMPEG_PATH, "-i", inputPath.absolutePathString()) + | ||
ffmpegArguments.subList(0, ffmpegArguments.lastIndex) + | ||
outputPath.absolutePathString() | ||
val processBuilder = ProcessBuilder(ffmpegCommand) | ||
.redirectOutput(ProcessBuilder.Redirect.DISCARD) | ||
.redirectError(ProcessBuilder.Redirect.DISCARD) | ||
withContext(Dispatchers.IO) { | ||
val process = processBuilder.start() | ||
val exitCode = process.waitFor() | ||
if (exitCode != 0) { | ||
throw ErrorResponseException("FFmpeg command failed!") | ||
} | ||
} | ||
DataSource.fromFile(outputPath, outputFilename) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
messaging/src/main/kotlin/io/github/shaksternano/borgar/messaging/command/FFmpegCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.github.shaksternano.borgar.messaging.command | ||
|
||
import io.github.shaksternano.borgar.core.io.task.FFmpegTask | ||
import io.github.shaksternano.borgar.core.io.task.FileTask | ||
import io.github.shaksternano.borgar.messaging.event.CommandEvent | ||
|
||
object FFmpegCommand : FileCommand( | ||
CommandArgumentInfo( | ||
key = "arguments", | ||
description = "The FFmpeg arguments.", | ||
type = CommandArgumentType.String, | ||
required = false, | ||
), | ||
) { | ||
|
||
override val name: String = "ffmpeg" | ||
override val description: String = "Runs an FFmpeg command. The input is specified automatically." | ||
|
||
override suspend fun createTask(arguments: CommandArguments, event: CommandEvent, maxFileSize: Long): FileTask { | ||
val ffmpegArguments = arguments.getDefaultStringOrEmpty() | ||
return FFmpegTask(ffmpegArguments) | ||
} | ||
} |