Solved

java program to convert video from wmv to avi

Posted on 2010-08-12
6
1,051 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 92

Assisted Solution

by:objects
objects earned 250 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 250 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 71
oracle 11g 23 83
table example 4 29
expectj telnet failing 5 36
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

777 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