Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge audio and video in Java

We have created an application that records web camera streams using Xuggler, but video and audio are separated.

We need to merge, not concatenate, these two files.

How can this be done in Java?

like image 380
kashif181 Avatar asked Aug 04 '12 18:08

kashif181


2 Answers

If you have audio and video file then you can merge them to a single audio video file using FFmpeg:

  1. Download FFmpeg: http://ffmpeg.zeranoe.com/builds/
  2. Extract downloaded file to specific folder, say c:\ffmpeffolder
  3. Using cmd move to specific folder c:\ffmpeffolder\bin
  4. Run following command: $ ffmpeg -i audioInput.mp3 -i videoInput.avi -acodec copy -vcodec copy outputFile.avi This is it. outputFile.avi will be the resulting file.
like image 114
arslaan ejaz Avatar answered Sep 24 '22 23:09

arslaan ejaz


You can call ffmpeg using Java as follows:

public class WrapperExe {

 public boolean doSomething() {

 String[] exeCmd = new String[]{"ffmpeg", "-i", "audioInput.mp3", "-i", "videoInput.avi" ,"-acodec", "copy", "-vcodec", "copy", "outputFile.avi"};

 ProcessBuilder pb = new ProcessBuilder(exeCmd);
 boolean exeCmdStatus = executeCMD(pb);

 return exeCmdStatus;
} //End doSomething Function

private boolean executeCMD(ProcessBuilder pb)
{
 pb.redirectErrorStream(true);
 Process p = null;

 try {
  p = pb.start();

 } catch (Exception ex) {
 ex.printStackTrace();
 System.out.println("oops");
 p.destroy();
 return false;
}
// wait until the process is done
try {
 p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("woopsy");
p.destroy();
return false;
}
return true;
 }// End function executeCMD
} // End class WrapperExe
like image 44
codegeek Avatar answered Sep 24 '22 23:09

codegeek