简体   繁体   English

在没有 FFmpeg 的 Java (JDK) 中将音频添加到 MP4

[英]Add audio to an MP4 in Java (JDK) without FFmpeg

I am currently working on a project in Java, where I have to combine several pictures to an MP4 file and also add audio track to this file (as WAV or MP3).我目前正在使用 Java 进行一个项目,我必须将几张图片组合到一个 MP4 文件中,并将音轨添加到这个文件中(如 WAV 或 MP3)。

I have managed to create an MP4 file from the images using JCodec<\/strong><\/a> .我已经设法使用JCodec<\/strong><\/a>从图像中创建了一个 MP4 文件。 I chose this library because if I should ever want this to run on Android, this library is compatible there.我之所以选择这个库,是因为如果我想让它在 Android 上运行,这个库在那里是兼容的。

I tried using the MP4 Parser<\/strong><\/a> but it only accepts AAC\/M4A<\/code> files.我尝试使用MP4 Parser<\/strong><\/a> ,但它只接受AAC\/M4A<\/code>文件。 But I want this to work with WAV<\/code> files or MP3<\/code> .但我希望这适用于WAV<\/code>文件或MP3<\/code> 。 All other useful libraries I found so far use FFmpeg in some way.到目前为止,我发现的所有其他有用的库都以某种方式使用 FFmpeg。 I do not want the people using the program having to install the FFMPEG command line tool which I'd then call from Java.我不希望使用该程序的人必须安装 FFMPEG 命令行工具,然后我会从 Java 调用该工具。

So what I want to know is,所以我想知道的是,

using this code you can add .wav and other supported formats by jdk to your java code使用此代码,您可以将 jdk 的 .wav 和其他支持的格式添加到您的 java 代码中

import java.io.File;
import javax.sound.sampled.*;

use try catch or throws method here i used try catch method在这里使用 try catch 或 throws 方法 我使用了 try catch 方法

public class Music {
    public static void main(String[] args){

        try{
            File file = new File("--file location--");
            AudioInputStream audioStream =AudioSystem.getAudioInputStream(file);
            Clip clip = AudioSystem.getClip();
            clip.open(audioStream);

            clip.start();

            Thread.sleep(s*1000); // s = time in seconds

        }catch(Exception e){
            System.out.println(e);
            System.out.println("File Not Supported");
         }

    }
}

Click here to open my github repository AddingAudioToJava单击此处打开我的 github 存储库

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

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