繁体   English   中英

如何在Java中合并音频和视频

[英]How to merge audio and video in Java

我们创建了一个使用Xuggler记录网络摄像头流的应用程序,但视频和音频是分开的。

我们需要合并而不是连接这两个文件。

如何在Java中完成?

如果您有音频和视频文件,则可以使用FFmpeg将它们合并到单个音频视频文件中:

  1. 下载FFmpeg: http//ffmpeg.zeranoe.com/builds/
  2. 将下载的文件解压缩到特定文件夹,例如c:\\ ffmpeffolder
  3. 使用cmd移动到特定文件夹c:\\ ffmpeffolder \\ bin
  4. 运行以下命令:$ ffmpeg -i audioInput.mp3 -i videoInput.avi -acodec copy -vcodec copy outputFile.avi就是这样。 outputFile.avi将是生成的文件。

您可以使用Java调用ffmpeg,如下所示:

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

根据格式的不同,您可以使用JMF,Java Media Framework,这是古老而且从未如此出色,但可能足以满足您的需求。

如果它不支持你的格式,你可以使用FFMPEG包装器,如果我没记错的话,它提供了一个JMF接口但是使用了FFMPEG: http//fmj-sf.net/ffmpeg-java/getting_started.php

正如其他答案已经提出的那样,ffmeg似乎确实是最好的解决方案。

这里的代码我最终得到:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM