• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED Java execute outside app, wait for completion

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

ssjwizard

Has slightly less legible writing than Thideras
Joined
Mar 12, 2002
Ok so I have gotten sick and tired of fighting with all these little problems that my media network has with Matroska containers so I am creating a simple batch remuxer. Its actually working perfectly well but its unsuitable for directories with more than 10 files or so in them because its trying to convert everything at the same time. Now I tried using the Process.waitFor() method to fix this but it hangs up every now and then and I have to manually kill the process and start whatever file it was stuck on again after the fact.

This is the code I tried using, and it does cause it to process one at a time but as I mentioned it hags up from time to time.

Code:
          Process proc = Runtime.getRuntime().exec(command);
          /*try {//this block hangs every 3-4 runs and FFmpeg must be manually killed. 
            if(proc.waitFor() != 0){
              System.out.println("\nRemuxing was interupted\n");
            }
          } catch (InterruptedException e) {
            System.out.println("\nRemuxing was interupted\n");
            e.printStackTrace();
          }*/


Edit, well I ended up switching it out for ProcessBuilder which seems to deal with the problem I was having.
 
Last edited:
was it a simple switch in code above ? or is it an entire different block now ?

if you got time id be interested if you could post your solution
 
Its pretty much the same as before though theres alot more substance inside of it now. The only real change was to use processbuilder to create my app thread rather than calling the system runtime myself.


Code:
            //setup the process builder
          ProcessBuilder pb = new ProcessBuilder();
          pb.redirectErrorStream(true);
          pb.directory(dir);
           
            //now batch remux with FFmpeg.
          for(String file : files){
            if(file.contains(".mkv")){
              System.out.println("Beginning remux of " + file);

                //prepare file name
              String newfilename = file.substring(0, file.length()-4) + ".mp4\"";

                //prepare command
              String[] command =  {"cmd","/c", "ffmpeg -i \"" + path + "\\" + file + "\" -map 0 -acodec copy -vcodec copy -scodec mov_text \"" + path + "\\" + newfilename};

                //run ffmpeg
              pb.command(command);
              Process proc = pb.start();        

                //capture the output
              InputStreamReader isr = new  InputStreamReader(proc.getInputStream());
              BufferedReader br = new BufferedReader(isr);          
              List<String> lines = new ArrayList<String>();
              String line;
              while ((line = br.readLine()) != null) {
                lines.add(line);
              }

                //wait to start the next file
              try {
                if(proc.waitFor() != 0){
                  System.out.println("\nRemuxing was interupted\n");
                  for(String l : lines){ System.out.println(l); }
                }
                else{ 
                  int end = newfilename.length()-1;
                  System.out.println("Completed remux of " + newfilename.substring(0,end) + "\n"); 
                } 
              } 
              catch (InterruptedException e) {
                System.out.println("\nRemuxing was interupted\n");
                e.printStackTrace();
              }

            }//end if //remux block
 
Last edited:
Back