是否有用于 mp4 文件的 Java API? [英] Is there a Java API for mp4 files?

查看:35
本文介绍了是否有用于 mp4 文件的 Java API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用此 mp3 SPI 支持处理 Mp3 文件,但我没有找到类似于 mp4 文件的内容.

Mp3 files can be handled using this mp3 SPI support, but I'm not finding something similar to mp4 files.

任何帮助将不胜感激.

--更新

我想要做的是获取文件的大小,就像我使用此代码处理波形文件一样:

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

--答案

这是使用@mdma(IBM工具包)提示的答案代码:

Here is the answer code using the hint of @mdma (IBM toolkit):

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

--

相关,语言无关,问题:有人熟悉mp4数据结构吗?

Related, language independent, question: Anyone familiar with mp4 data structure?

推荐答案

Mp4 是一种容器格式——要能够找到里面音频的时长,你必须先解析容器外的内容.您可以使用 isobox mp4parser 提取 mp4 文件的内容.

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.

完成此操作后,您将拥有原始音频数据.如果它是 java 中支持的格式之一(wav、mp3 等),那么您可以像打开 wav 一样打开这个文件.最初,您可能会将音频提取到一个单独的文件中,为简单起见和更容易调试.当这工作时,您可以进行内联提取 - 您可以实现一个 InputStreamFilter 来动态地从 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.

IBM Alphaworks 有一个纯 Java MP4 解码器库可用,但对于您目前的需求.

IBM Alphaworks have a pure java MP4 decoder library available, but it's possibly overkill for your present needs.

这篇关于是否有用于 mp4 文件的 Java API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆