views:

942

answers:

3

I'm looking for an example of parsing an FLV header for duration specifically in Java. Given the URL of an FLV file I want to download the header only and parse out the duration. I have the FLV spec but I want an example. Python or PHP would be OK too but Java is preferred.

+1  A: 

Do you have problems downloading the header or parsing it? if it's downloading then use this code:

URL url = new URL(fileUrl);
InputStream dis = url.openStream();
byte[] header = new byte[HEADER_SIZE];
dis.read(header);

You can wrap InputStream with DataInputStream if you want to read int's rather than bytes.

After that just look at getInfo method from PHP-FLV Info or read the spec.

tulskiy
I had a combination of both. Since I've seen the spec though, the biggest question was the best way to download part of the file then parse the header. I experimented with sending the HTTP Range header to say how many bytes I wanted but only some servers send back HTTP 206 Partial Content so your solution is better.Thanks!
Jon
OK, if you have questions about parsing, just edit the question.
tulskiy
InputStream.read does not necessarily read fully. For an easy solution, wrap in `DataInputStream` and call `readFully`.
Tom Hawtin - tackline
Forgot to add a while loop around read().
tulskiy
A: 

This is a question not an answer: why did someone down vote this question?

Stevoni
A: 

As the-alchemist states in this other question:

The Red5 project has a class called FLVReader which does what you want. It's LGPL licensed.

I've tried it and it works fine. You'll need this libraries:

Getting the FLV video duration is as simple as this:

FLVReader flvReader = new FLVReader(...); // Can use a File or a ByteBuffer as input
long duration = flvReader.getDuration();  // Returns the duration in milliseconds
Toto