简体   繁体   English

命令适用于终端,但不适用于 Kotlin

[英]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.

相关问题 AppleScript 命令在终端中有效,但在 Java 中无效 - AppleScript Command Works in Terminal but not in Java 命令在终端中工作,但不在Runtime.exec中 - Command works in terminal, but not with Runtime.exec 命令从exec()失败,但可在终端上使用 - command fails from exec() but works on terminal 该命令在终端上有效,但在Java程序中不起作用 - The command works on terminal but doesn't work in java program Java线程程序可在Eclipse中运行,但不能在终端/命令提示符下运行 - Java thread program works in Eclipse, but not in terminal / command prompt Java应用程序仅在通过终端/命令提示符启动时才有效 - Java application only works when launched via a terminal/command prompt 除了这个命令(合并)之外,ffmpeg大部分时间在java中工作正常,这个命令在终端中工作正常 - ffmpeg works fine most of the time in java except for this command (merging), and this command works fine directly in terminal 从Java执行时,MYSQL命令给出错误。 相同的命令在Linux Terminal上可以正常工作。 可能是什么原因? - MYSQL command gives error while being executed from java. Same command works fine on Linux Terminal. What can be the reason? 打开 Safari 的终端命令 - Terminal command to open Safari Java命令终端 - Java command terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM