Solved

java program to convert video from wmv to avi

Posted on 2010-08-12
6
1,042 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
count11 challenge 6 47
computer science syllabus 3 52
where is session ID cache stored 1 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now