Mp3 files can be handled using this mp3 SPI support, but I'm not finding something similar to mp4 files.
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):
/**
* 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?
Java MP4 Parser. A Java API to read, write and create MP4 container. Manipulating containers is different from encoding and decoding videos and audio.
Most popular media players can play MP4 files. Microsoft Movies & TV (Windows), Microsoft Windows Media Player (Windows), Apple QuickTime Player (macOS), MPlayer (multiplatform), and VLC media player (multiplatform) are popular applications for playing MP4 files.
In short, MP4 is a media container format that stands for MPEG4 part 14. It's an open industry standard that is accessible to most users, as the majority of devices and operating systems support it.
An MP4 file is a multimedia file used for storing, sharing, downloading, and streaming video clips from the internet. It is a versatile file type that can store video, audio, images, and even subtitles. MP4 video files are one of the most common file formats used on the internet today.
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.
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 have a pure java MP4 decoder library available, but it's possibly overkill for your present needs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With