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?
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?
If you have audio and video file then you can merge them to a single audio video file using FFmpeg:
ffmpeg -i audiofile.mp3 -i moviefile.avi -acoded libfaac -vcodec libx264 output.mp4
You need to have ffmpeg
built in with x264
and liblame
support for that. And watch out for copyright infringement due to mp3/x264 licenses, how you do it do not mind ;) –
Bursary 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
I would recommend to look into ffmpeg and merge them trough command line with the required arguments needed for merging the video and audio files. You can use java Process to execute native processes.
depending on the formats, you could use JMF, the Java Media Framework, which is ancient and was never that great, but might be good enough for your purposes.
If it doesn't support your formats, you could use the FFMPEG wrapper which, if I am remembering correctly, provides a JMF interface but uses FFMPEG: http://fmj-sf.net/ffmpeg-java/getting_started.php
As the other answers suggested already, ffmeg does seem to be the best solution here.
Here the code I ended up with:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
public static File mergeAudioToVideo(
File ffmpegExecutable, // ffmpeg/bin/ffmpeg.exe
File audioFile,
File videoFile,
File outputDir,
String outFileName) throws IOException, InterruptedException {
for (File f : Arrays.asList(ffmpegExecutable, audioFile, videoFile, outputDir)) {
if (! f.exists()) {
throw new FileNotFoundException(f.getAbsolutePath());
}
}
File mergedFile = Paths.get(outputDir.getAbsolutePath(), outFileName).toFile();
if (mergedFile.exists()) {
mergedFile.delete();
}
ProcessBuilder pb = new ProcessBuilder(
ffmpegExecutable.getAbsolutePath(),
"-i",
audioFile.getAbsolutePath(),
"-i",
videoFile.getAbsolutePath() ,
"-acodec",
"copy",
"-vcodec",
"copy",
mergedFile.getAbsolutePath()
);
pb.redirectErrorStream(true);
Process process = pb.start();
process.waitFor();
if (!mergedFile.exists()) {
return null;
}
return mergedFile;
}
© 2022 - 2024 — McMap. All rights reserved.