18

I need a media information extraction library (pure Java or JNI wrapper) that can handle common media formats. I primarily use it for video files and I need at least these information:

  1. Video length (Runtime)
  2. Video bitrate
  3. Video framerate
  4. Video format and codec
  5. Video size (width X height)
  6. Audio channels
  7. Audio format
  8. Audio bitrate and sampling rate

There are several libraries and tools around but I couldn't find for Java.

Emre Yazici
  • 10,136
  • 6
  • 48
  • 55
  • What formats/encodings are we talking about here? People have different definitions of "common" for media formats. – aperkins Jan 30 '10 at 16:48
  • 1
    I mean MPEG, DivX, XviD, Matroska, Real Video, Windows Media formats for videos; MP3 and AC3 for audios. There are no certain formats specifically I need, so the more format support, the better for me. – Emre Yazici Jan 30 '10 at 17:46
  • Is subs4me intended to use for Linux platform only? when I tried your sample, I hit exception because of this line in lib `if (!(Platform.isLinux()))return;` – kitokid Feb 06 '12 at 07:36
  • JavaCv is a wrapper that is based on openCV and/or ffmpeg. You can get all this informations with it. sample code: OpenCVFrameGrabber frameGrabber = new OpenCVFrameGrabber(video_in); frameGrabber.getFrameRate(); – Alexander Sidikov Pfeif Oct 22 '14 at 11:21

4 Answers4

14

After a few days of asking this question, I have found MediaInfo which supplies dozens of technical and tag information about a video or audio file.

There is a JNI wrapper for MediaInfo in subs4me's source tree that I find very useful. Here are some code snippets that show how to extract some information from a media file:

String fileName   = "path/to/my/file";
File file         = new File(fileName);
MediaInfo info    = new MediaInfo();
info.open(file);

String format     = info.get(MediaInfo.StreamKind.Video, i, "Format", 
                        MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
int bitRate       = info.get(MediaInfo.StreamKind.Video, i, "BitRate", 
                        MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
float frameRate   = info.get(MediaInfo.StreamKind.Video, i, "FrameRate", 
                        MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
short width       = info.get(MediaInfo.StreamKind.Video, i, "Width", 
                        MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);

int audioBitrate  = info.get(MediaInfo.StreamKind.Audio, i, "BitRate", 
                        MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
int audioChannels = info.get(MediaInfo.StreamKind.Audio, i, "Channels", 
                        MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);

Please note that the above code is a basic example and does not contain any error checking (which is a bad habit in a real scenario). Also note that information that you can extract with MediaInfo does not limited to the ones above. See MediaInfo's raw output to learn which kind of media information you can extract or read MediaInfo C++ SDK.

Emre Yazici
  • 10,136
  • 6
  • 48
  • 55
  • Is subs4me intended to use for Linux platform only? when I tried your sample, I hit exception because of this line in lib if (!(Platform.isLinux()))return; – kitokid Feb 06 '12 at 07:36
  • I am not sure subs4me is completely portable. I am using the code above on Windows without any problem. – Emre Yazici Feb 07 '12 at 04:31
  • Hi Emre, could u share me the usage of subs4me? I just put its jar file into my project and grab the MediaInfo. but it throws java.lang.NoClassDefFoundError when calling the class MediaInfo info = new MediaInfo();. Any missing or necessary steps , so I can use subs4me? I am lost. Thanks. – kitokid Feb 08 '12 at 01:46
  • Hi PTY, I am not using subs4me. As I refer my question, I am just using the example wrapper that comes with subs4me, not the subs4me's itself. I have no idea how subs4me works. – Emre Yazici Feb 08 '12 at 06:24
  • thanks. So without using subs4m4, how do you grab media information? do you use any library to extract those information? Thanks again. – kitokid Feb 08 '12 at 06:50
  • I am using MediaInfo to extract media information, not subs4me. – Emre Yazici Feb 08 '12 at 17:47
  • I found that all the info was returned as Strings, so I had to change the code (eg "String bitRate"), also I had to set i=0 (for stream number), but other that that, it all worked. Thanks, I would have had no chance without your code. – localhost Jun 14 '13 at 05:42
  • 1
    If you're gonna use the MediaInfo wrapper from the FileBot source (copied by subs4me) then grab it from the original source that is actually maintained: https://github.com/svn2github/filebot/blob/master/source/net/filebot/mediainfo/MediaInfo.java – rednoah Mar 02 '16 at 09:46
  • The link to the source is now old and returns a 404. The new link is: https://github.com/filebot/filebot – Chris Jan 27 '19 at 04:19
1

You don't need a framework for this stuff, for example AVI video container format has a 56 byte header containing all the relevant metadata. There's bunch of other formats on that site too, all of them seem to be quite trivial to implement.

Esko
  • 29,022
  • 11
  • 55
  • 82
  • I am not writing a media informatin extraction tool, so I don't want to learn each media format and implement proper extraction class. Also note that there are numerous container formats that have much more complex structure (and some of them are proprietary), AVI is one of the simplest case. – Emre Yazici Jan 30 '10 at 18:18
1

Try Xuggler. Here's some Xuggler source code that queries a media file and prints out all sorts of useful meta-data.

  • Art
Art Clarke
  • 2,495
  • 18
  • 15
0

Have you tried Java Media Framework API http://java.sun.com/javase/technologies/desktop/media/jmf/ but I am not sure if it will have enough support as Xuggle above which uses FFMpeg, the only advantage of using this would be cross platform support.

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265