How to run ffmpeg command in background from java

I run the ffmpeg command from java Runtime.getRuntime (). exec. The ffmpeg command basically cuts images from a real stream. In fact, when I run this command without it, and then it works fine for five minutes after that, it stops cutting images.

but when I use "&" in the ffmpeg command it doesn't work at all.

there is no problem in the real-time stream, since when I ran this ffmpeg command from linux, it works fine.

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

+4
source share
5 answers

"&" is a shell directive to put a task in the background. Running from Process.exec() does not include the shell.

If your process is stalled (i.e. it works, but just does not work), then I suspect that it is blocked, expecting you to use stdout / stderr. You must do this using threads to prevent the process from locking up, expecting you to consume your result. See this SO answer for more details.

To run it in the background (i.e. while your Java process is doing other things), you need to:

  • create new thread
  • invoke a process through Process.exec in this thread
  • consumes stdout / stderr (both in separate threads) and finally gets the exit code
+3
source

I know that you are asking for help with command line problems in Java , but you might be interested in another solution.

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

This is a shell for ffmpeg. Perhaps you can try creating a java thread and running your command.

+1
source

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

0
source

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

 String[] ffmpeg = new String[] {"ffmpeg", "-ss", Integer.toString(seconds),"-i", in, "-f", "image2", "-vframes", "1", out}; Process p = Runtime.getRuntime().exec(ffmpeg); 
0
source
 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 seconds. long and he created 100 jpegs in the photo folder. You must specify an absolute path to the folder.

Also, to find the path for binary use of ffmpeg, use which ffmpeg in the terminal. My was /opt/local/bin/ffmpeg .

0
source

All Articles