[英]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,所以我想知道的是,
Is there a way of adding audio to an MP4 file in Java without using FFmpeg, or at least using it in some way where I don't have to install the command line tool?有没有一种方法可以在不使用 FFmpeg 的情况下将音频添加到 Java 中的 MP4 文件,或者至少以某种方式使用它,而无需安装命令行工具?<\/li>
If there is, do I have to use AAC\/M4A files?如果有,我必须使用 AAC\/M4A 文件吗? If yes, then I'd also like to know how to convert a WAV or MP3 file to AAC\/M4A in Java.如果是,那么我也想知道如何将 WAV 或 MP3 文件转换为 Java 中的 AAC\/M4A。 Is there a good library for doing the job?有没有一个很好的图书馆来做这项工作? I've been stuck at this point for about a week now and I hope this thread will lead to some more useful result.我已经被困在这一点上大约一个星期了,我希望这个线程会带来一些更有用的结果。<\/li><\/ul> I hope someone will be able to help, I wish you a happy day, whether you can help or not.我希望有人能帮忙,祝你天天快乐,不管你能不能帮忙。
Bye for now, Robbe.再见,罗比。
"
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.