java 如何从java在后台运行ffmpeg命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3740935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:13:51  来源:igfitidea点击:

How to run ffmpeg command in background from java

java

提问by Hyman

I am running a ffmpeg command from java Runtime.getRuntime().exec. ffmpeg command basically cut the images from live stream. Actually when i run this command without & then it works fine for five minutes after that it stops cutting images.

我正在从 java Runtime.getRuntime().exec 运行 ffmpeg 命令。ffmpeg 命令基本上是从实时流中剪切图像。实际上,当我在没有 & 的情况下运行此命令时,它可以正常工作五分钟,然后停止剪切图像。

but when i use "&" in ffmpeg command it does not work at all.

但是当我在 ffmpeg 命令中使用“&”时,它根本不起作用。

there is no problem in live stream as when i ran this ffmpeg command from linux its working fine.

直播中没有问题,因为当我从 linux 运行这个 ffmpeg 命令时,它工作正常。

My main question is how to run a ffmpeg command in background from java.

我的主要问题是如何从 java 在后台运行 ffmpeg 命令。

回答by Brian Agnew

The '&' is a shelldirective to drop the task into the background. Running from Process.exec()doesn't involve the shell.

'&' 是一个shell指令,用于将任务放入后台。运行 fromProcess.exec()不涉及 shell。

If your process is stalling (i.e. running but just not working) then I suspect that it's blocked waiting for you to consume stdout/stderr. You have to do this using threads to prevent the process blocking waiting for you to consume its output. See this SO answerfor more details.

如果您的进程停滞(即正在运行但不工作),那么我怀疑它被阻止等待您使用 stdout/stderr。您必须使用线程来执行此操作,以防止进程阻塞等待您使用其输出。有关更多详细信息,请参阅此 SO 答案

To run it in the background (i.e. whilst your Java process does other stuff) you need to:

要在后台运行它(即当您的 Java 进程执行其他操作时),您需要:

  1. spawn a new thread
  2. invoke the process via Process.execin this thread
  3. consume stdout/stderr (both in separate threads) and finally get the exit code
  1. 产生一个新线程
  2. 通过Process.exec在此线程中调用进程
  3. 使用 stdout/stderr(都在单独的线程中)并最终获得退出代码

回答by nitin

String livestream = "/Users/videos/video_10/video_1.mp4";

String folderpth = "/Users/videos/video_10/photos";

String cmd="/opt/local/bin/ffmpeg -i "+ livestream +" -r 10 "+folderpth+"/image%d.jpeg"; 

Process p = Runtime.getRuntime().exec(cmd);

The video was 10 sec. long and it created 100 jpeg's in photos folder. You have to provide the absolute path of the folder.

视频是10秒。很长,它在照片文件夹中创建了 100 个 jpeg。您必须提供文件夹的绝对路径。

Also to find the path for ffmpeg binary use which ffmpegin terminal. Mine was /opt/local/bin/ffmpeg.

还要找到which ffmpeg在终端中使用 ffmpeg 二进制文件的路径。我的是/opt/local/bin/ffmpeg

回答by Martin Magakian

I know you ask for help for your command line problem in Javabut you might be interest in an other solution.

我知道您就Java 中的命令行问题寻求帮助,但您可能对其他解决方案感兴趣。

Maybe you can give a try to this library: http://code.google.com/p/jjmpeg/

也许你可以试试这个库:http: //code.google.com/p/jjmpeg/

It's a wrapper for ffmpeg. Maybe you can try to create a java thread and run your command.

它是 ffmpeg 的包装器。也许您可以尝试创建一个 java 线程并运行您的命令。

回答by dekz

Are you passing the arguments in an array? If it works for a while are you giving it all the expected arguments? (print it out and check). Here is an example of my ffmpeg that I dug out for you.

您是否在数组中传递参数?如果它工作了一段时间,你是否给它所有预期的参数?(打印出来检查)。这是我为您挖掘的我的 ffmpeg 示例。

String[] ffmpeg = new String[] {"ffmpeg", "-ss", Integer.toString(seconds),"-i", in, "-f", "image2", "-vframes", "1", out};
Process p = Runtime.getRuntime().exec(ffmpeg);

回答by Thomas Mueller

You wrote it stops working after minutes. Did you check the exit code (Process.exitValue()). Also, did you check output stream and error stream from the external process?

你写的它在几分钟后停止工作。您是否检查了退出代码 ( Process.exitValue())。另外,您是否检查了外部进程的输出流和错误流?