11

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?

Community
  • 1
  • 1
The Student
  • 27,520
  • 68
  • 161
  • 264

3 Answers3

15

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.

mdma
  • 56,943
  • 12
  • 94
  • 128
  • The mp4parser project have no svn or downloads. I'm trying with JMF + Jffmpeg, but I'm having a hard time with it. – The Student Jun 16 '10 at 14:05
  • @Tom - The mp4parser sources are there - I just checked out the sources from http://mp4parser.googlecode.com/svn/trunk/ – mdma Jun 16 '10 at 16:17
  • Have you tried the isoViwer - I fired it up just to see and it shows you the composition, including a "track" box that contains "chunks" which contain samples. – mdma Jun 16 '10 at 22:06
  • There is an error in a class on its package: com.coremedia.iso.gui.GenericSamplePane line 50 `The method getSampleDescriptionBox() in the type TrackMetaData is not applicable for the arguments (long)` – The Student Jun 17 '10 at 13:25
  • And delete the arg does not work. I opened an issue for the project: http://code.google.com/p/mp4parser/issues/detail?id=1 – The Student Jun 17 '10 at 13:31
  • I'm also trying with the IBM toolkit, I there's no documentation, just samples. And, strangely, the samples have just xml files, nothing about java files. – The Student Jun 17 '10 at 14:43
  • It worked with IBMPlayerForMpeg4SDK.jar, I'll post the code bellow the question. Thanks! – The Student Jun 17 '10 at 15:16
8

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.

Curtis
  • 3,931
  • 1
  • 19
  • 26
  • I haven't seen a pure-java decoder, everything uses native libraries. If you find one though, I'd LOVE to hear about it! – Curtis Jun 24 '10 at 14:46
  • 2
    Xuggler now combines the native files with the provided JAR file, no need for DLLs! – SpellingD Jan 26 '13 at 01:20
0

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820