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
2009-08-22 00:15:51
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
2009-08-24 15:39:10
OK, if you have questions about parsing, just edit the question.
tulskiy
2009-08-24 16:28:12
InputStream.read does not necessarily read fully. For an easy solution, wrap in `DataInputStream` and call `readFully`.
Tom Hawtin - tackline
2009-09-27 04:00:07
Forgot to add a while loop around read().
tulskiy
2009-09-27 04:12:32
A:
This is a question not an answer: why did someone down vote this question?
Stevoni
2009-08-22 00:22:48
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
2010-09-08 18:44:44