[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java program to convert video from wmv to avi

Posted on 2010-08-12
6
Medium Priority
?
1,116 Views
Last Modified: 2013-12-22
Hi,

i had created a java program which converts the format from wmv to avi . My programming is running fine but i want to get a confirmation message after finishing the conversion. Then i included the proc.waitFor() method for waiting till the process gets finished.

 After putting this line the program gets hanged and only continuing after terminating the program

Below is my program
package com.suku.dev;


import java.io.*;

public class TestingRuntime {
	
	public static void main(String args[])throws Exception
	
	{
		
		
		
		   Runtime rt=  Runtime.getRuntime();
		     
		     Process proc=  rt.exec("ffmpeg -i \"C:\\videos\\Nuv.wmv\"  -vcodec mpeg4 \"C:\\videos\\Sui.avi\" ");
		    
                  // if i include till above line...my program works fine
		     // If i include below lines then the program gets hanged 
		
		     InputStream stdin = proc.getInputStream();
	            InputStreamReader isr = new InputStreamReader(stdin);
	            BufferedReader br = new BufferedReader(isr);
	            String line = null;
	            System.out.println("<OUTPUT>");
	            while ( (line = br.readLine()) != null)
	                System.out.println(line);
	            System.out.println("</OUTPUT>");
	            int exitVal = proc.waitFor();            
	         
		     System.out.println("Process exitValue: " +exitVal);
	            
	      
	  
	}

}

Open in new window

                                                   

Please let me know the way to identify after the process gets finished
0
Comment
Question by:sukumarr
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:WhiteMage
ID: 33424486
>>>>After putting this line the program gets hanged and only continuing after terminating the program

That's exactly what "proc.waitFor()" does.  It waits until the process (ffmpeg in this case) is terminated.

If after you have no more input, you know the process is done, then you could put this instead of proc.waitFor:

proc.destroy();
int exitVal = proc.exitValue();
0
 

Author Comment

by:sukumarr
ID: 33425118
I tried to replace proc.waitFor() with destroy and exitValue as you said but i looks like, the process is still hanging and i'm unable to figure out at which exact place the input streaming ends

Do i need to put some Thread sleep method and check for every frequency or any other better way...
0
 
LVL 8

Expert Comment

by:WhiteMage
ID: 33425288
What exactly is the output of the program?  There might be a line that says when it is done.

Also, perhaps, the line is never going to null?  Does the ending "</OUTPUT>" get printed?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 33425562
you may need to also read any output written to stderr

http://helpdesk.objects.com.au/java/runtime-exec-locks-up
0
 
LVL 8

Accepted Solution

by:
WhiteMage earned 1000 total points
ID: 33425614
Yes, that is the problem.  To clarify objects vague response, use getErrorStream.  I downloaded the program and tried it and found that this was the problem before objects response.  It worked perfectly with this:

InputStream stdin = proc.getErrorStream();

Now, a problem that you might run into is that it does hang if the file already exists.  To get around that, use "-y" flag to overwrite an already existing file.  Or you could parse the input and write to the proc.getOutputStream:

//added -y flag to overwrite a file if it already exists
Process proc=  rt.exec("ffmpeg -y -i \"C:\\videos\\Nuv.wmv\"  -vcodec mpeg4 \"C:\\videos\\Sui.avi\" ");
0
 

Author Closing Comment

by:sukumarr
ID: 33427412
@objects

Thanks for giving hint and it worked perfectly. But i couldn't understand why it's writing to stderr instead of stdin.

@WhiteMage

Thanks for helping. Though the file exists, it's overwriting by default without providing -y flag. Anyways i appreciate your help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question