4

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.

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

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

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

jack
  • 41
  • 1
  • 1
  • 2

5 Answers5

3

The '&' is a shell directive to drop the task into the background. Running from Process.exec() doesn't involve the 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 answer for more details.

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

  1. spawn a new thread
  2. invoke the process via Process.exec in this thread
  3. consume stdout/stderr (both in separate threads) and finally get the exit code
Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • I have tested with thread also..but still same result. how to drop the task into the background means how to use "&" – jack Sep 19 '10 at 12:31
1
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.

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

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
nitin
  • 571
  • 1
  • 4
  • 16
1

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

Maybe you can give a try to this library: 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.

Martin Magakian
  • 3,746
  • 5
  • 37
  • 53
0

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?

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
0

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.

String[] ffmpeg = new String[] {"ffmpeg", "-ss", Integer.toString(seconds),"-i", in, "-f", "image2", "-vframes", "1", out};
Process p = Runtime.getRuntime().exec(ffmpeg);
dekz
  • 805
  • 1
  • 8
  • 17
  • /usr/sites/ffmpeg -i livestream -r4 folderpth/image%d.jpeg – jack Sep 18 '10 at 10:41
  • Did you try passing the arguments as a list of strings? Post some code in your original question to show us what you have. My code above takes an integer argument for time an input file and outputs images. – dekz Sep 18 '10 at 11:37
  • here is the sample code.. String cmd="/usr/sites/ffmpeg -i"+ livestream +" -r4 "+folderpth+"/image%d.jpeg"; try{ Process p = Runtime.getRuntime().exec(cmd); }catch{} – jack Sep 18 '10 at 19:40
  • Try it with a string array of parameters, not just a large string. I had some problems also using just a string and not a string array. – dekz Sep 18 '10 at 23:53
  • 1
    what is "in" and "out" param in your string array – jack Sep 19 '10 at 12:09
  • they are strings which are file names, so test2.png is an example of out. – dekz Sep 20 '10 at 01:51