是否有用于 mp4 文件的 Java API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3015393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a Java API for mp4 files?
提问by Tom Brito
Mp3 files can be handled using this mp3 SPI support, but I'm not finding something similar to mp4 files.
可以使用此mp3 SPI 支持处理 Mp3 文件,但我没有找到类似于 mp4 文件的内容。
Any help would be appreciated.
任何帮助,将不胜感激。
--UPDATE
- 更新
What I want to do is get the file's size, as I do with wave files using this code:
我想要做的是获取文件的大小,就像我使用以下代码处理波形文件一样:
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
--ANSWER
- 回答
Here is the answer code using the hint of @mdma (IBM toolkit):
这是使用@mdma(IBM 工具包)提示的答案代码:
/**
* Use IBMPlayerForMpeg4SDK to get mp4 file duration.
*
* @return the mp4File duration in milliseconds.
*/
public static long getMp4Duration(File mp4File) throws IllegalStateException, IOException {
PlayerControl playerControl = PlayerFactory.createLightweightMPEG4Player();
playerControl.open(mp4File.getAbsolutePath());
long mili = playerControl.getDuration();
// int sec = (int) ((mili / 1000) % 60);
// int min = (int) ((mili / 1000) / 60);
// System.out.println("IBM Tookit result = " + min + ":" + sec);
return mili;
}
--
——
Related, language independent, question: Anyone familiar with mp4 data structure?
相关,语言无关,问题: 有人熟悉 mp4 数据结构吗?
采纳答案by mdma
Mp4 is a container format - to be able to find the duration of the audio inside, you have to first parse the content out of the container. You can extract the content of an mp4 file using isobox mp4parser.
Mp4 是一种容器格式——为了能够找到里面音频的持续时间,你必须首先解析容器外的内容。您可以使用isobox mp4parser提取 mp4 文件的内容。
Once you've done that, you then have the raw audio data. If it's one of the supported formats in java (wav, mp3, etc..) then you can just open this file like you have done for wavs already. Initially you will probably extract the audio to a separate file, for simplicity's sake and easier debugging. When this is working, you can then do the extraction inline - you implement an InputStreamFilter that extracts the audio content from the mp4 on the fly, so no additional external files are required.
完成此操作后,您将拥有原始音频数据。如果它是 java 中支持的格式之一(wav、mp3 等),那么您可以像打开 wav 一样打开这个文件。最初您可能会将音频提取到一个单独的文件中,为简单起见和更容易调试。当这工作时,您可以进行内联提取 - 您可以实现一个 InputStreamFilter 来动态提取 mp4 中的音频内容,因此不需要额外的外部文件。
IBM Alphaworkshave a pure java MP4 decoder library available, but it's possibly overkill for your present needs.
IBM Alphaworks有一个可用的纯 Java MP4 解码器库,但对于您目前的需求来说可能有点过分。
回答by Aaron Digulla
You don't specify what you want to do. If you just want to play the files, you can use MPlayer
and control it remotely via the ProcessBuilder
API and stdio.
您没有指定要做什么。如果您只想播放文件,您可以MPlayer
通过ProcessBuilder
API 和 stdio远程使用和控制它。
回答by Curtis
Xuggler ( http://www.xuggle.com/xuggler/) provides about the best Java wrapper for FFMPEG that I've seen - it'll let you decode the images out of almost any file, and then do whatever you like with them.
Xuggler ( http://www.xuggle.com/xuggler/) 提供了我见过的最好的 FFMPEG Java 包装器——它可以让你从几乎任何文件中解码图像,然后做任何你喜欢的事情他们。