[英]Command works on terminal but not with Kotlin
I'm trying to use an FFMPEG command for concatenating different videos.我正在尝试使用 FFMPEG 命令来连接不同的视频。 The command is命令是
ffmpeg -i video.mp4 -i video-2.mp4 -filter_complex "[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4
This command works good when I run on Windows PowerShell.当我在 Windows PowerShell 上运行时,此命令运行良好。 But when I try to run with Kotlin code it doesn't work.但是当我尝试使用 Kotlin 代码运行时,它不起作用。
val firstVideo = "D:\\Videos\\ffmpeg\\video.mp4"
val secondVideo = "D:\\Videos\\ffmpeg\\video-2.mp4"
val resultPath = "D:\\Videos\\ffmpeg\\result-2.mp4"
val cmd = "ffmpeg -i $firstVideo -i $secondVideo -filter_complex \"[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]\" -map \"[v]\" -map \"[a]\" $resultPath"
.split(" ").toTypedArray()
Runtime.getRuntime().exec(cmd)
I'm not taking any error messages since I'm using FFMPEG cli.因为我使用的是 FFMPEG cli,所以我没有收到任何错误消息。
Also, this piece of Kotlin works perfect when I'm trying to run other FFMPEG operations.此外,当我尝试运行其他 FFMPEG 操作时,这段 Kotlin 工作得很好。
Turns out the actual issue was within the Runtime.getRuntime().exec() function.事实证明,实际问题出在Runtime.getRuntime().exec()函数中。 I tried to call waitFor() method but it got stuck.我试图调用waitFor()方法,但它卡住了。 I was waiting forever.我一直在等待。 I did some research and read the docs.我做了一些研究并阅读了文档。
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.由于一些原生平台只为标准输入输出流提供有限的缓冲区大小,如果不能及时写入输入流或读取子进程的输出流,可能会导致子进程阻塞,甚至死锁。
This code was works just fine:这段代码工作得很好:
fun main(){
val path1 = "video-1.mp4"
val path2 = "video-2.mp4"
val resultPath = "output.mp4"
val cmd = arrayOf("ffmpeg",
"-i", path1, "-i", path2,
"-filter_complex", "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[v][a]",
"-map", "[v]", "-map", "[a]", resultPath)
executeCommand(cmd)
}
fun executeCommand(cmd: Array<String>){
try {
val runtime = Runtime.getRuntime()
val process = runtime.exec(cmd)
val errorStream = process.errorStream
val input = InputStreamReader(errorStream)
val reader = BufferedReader(input)
reader.forEachLine {
println(it);
}
process.waitFor()
} catch (e: Exception) {
e.printStackTrace();
}
}
https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.