I want to retrieve the Resolution and Duration of the video file that a end user uploads in my site before it gets uploaded into my Server. In this way, I want to check if the resolution and duration meets my requirement. Went through many APIs and they all give this Meta data information only after video gets uploaded into the Server. My Server is running on Linux and am using Java, Struts. Are there any APIs which will give this information before video file gets uploaded?
3 Answers
Consider uploading, by AJAX, a small portion of the beginning of the file to the server side. In most file formats, 50-100 kbytes of data should be enough (and that number can be determined per file format; in FLV, AVI, MKV files even few kbytes should be enough). The server side can then use mediainfo, xuggle or plain ffmpeg to get the dimensions and duration, and accordingly allow or disallow the upload by the client.
Here's a tutorial with code examples on how to send only a part of the file using the HTML5 file API.

- 3,620
- 1
- 18
- 22
-
This requires Browser to be HTML5 compliant. Am targeting users who also use IE7 / IE8 which are not HTML5 compliant. Your comments? – Vikas V Nov 27 '12 at 05:47
-
We're going into deep hacking here... :) You could use regular upload, and abort the upload mid-way by the server (by violently `close()`ing the socket). I don't think you can do that with Java servlets, you need CGI-like interface in which you're getting access to the request payload as a socket stream. – onon15 Nov 27 '12 at 06:59
You can (now) just check the size (and some other things, but no duration nor resolution, AFAIK), client side, with HTML5:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
and

- 1
- 1

- 49,480
- 26
- 114
- 243
On browsers support HTML5, you can use an MP4 parser (such as mp4.js) to parse the file headers.
- To get the file duration, you will need to find the
moov
atom of the file, and inside it themvhd
atom. The duration, in seconds, can be calculated by dividing themvhd
's Duration field by the TimeScale field. - To get the resolution, you will need to find inside the
moov
atom, thetrak
atom for the video track (ignore the audio track, or other tracks if you encounter them; usually the file has only one video track), and in it thetkhd
atom, which has width and height fields.

- 3,620
- 1
- 18
- 22
-
Its only for MP4 if am not wrong. In my case, end user can upload video files of other formats as well (avi etc). How this can be achieved if video files are of other formats? – Vikas V Nov 26 '12 at 04:48
-
You *could* implement parsers for other file formats as well. Most likely, you will not find pre-made libraries such as `mp4.js` for less common formats, but in fact most formats are simpler to parse than mp4. But I admit, that makes my whole idea less practical. – onon15 Nov 26 '12 at 06:27
-
Ok. Any links to tutorials you can suggest for doing the same in Java for different formats? – Vikas V Nov 26 '12 at 06:47
-
If you're considering implementing in native code, [mediainfo](http://mediainfo.sourceforge.net/) is probably the most accurate and varied open-source querying tool for media formats. Another option, again using native code, is to use ffmpeg for it; there are Java wrappers (e.g. [xuggle](http://www.xuggle.com/)) but they will require a DLL with the native code. I'm not acquainted with an open-source pure-java solution. – onon15 Nov 26 '12 at 06:53
-
I gave a try for [xuggle](http://www.xuggle.com/) & [mediainfo](http://mediainfo.sourceforge.net/). I am getting Duration & Resolution only after video gets uploaded into my Server but not before that. So, the question still spins around "Can these data be obtained before video upload starts" ? – Vikas V Nov 26 '12 at 08:59
-
I'm sorry, I must have misunderstood. When you were referring to "doing the same in Java" you meant doing them on the server side? In that case you'd have a problem with workflow of HTML forms, in which the server-side code gets the form contents only after the upload is complete. I think this can be solved though, it's another solution so I'll write it up as a separate answer. – onon15 Nov 26 '12 at 09:17