How to merge audio and video in Java

╄→гoц情女王★ 提交于 2019-12-10 13:45:51

问题


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?


回答1:


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.



回答2:


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



回答3:


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.

  • http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html
  • http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String)
  • http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/



回答4:


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




回答5:


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;
}


来源:https://stackoverflow.com/questions/11811181/how-to-merge-audio-and-video-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!