问题
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:
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.
回答2:
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:
- Red5 library jar for use when building applications
- Apache Commons Logging
- Apache MINA
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
来源:https://stackoverflow.com/questions/1313640/parsing-flv-header-duration-of-remote-file-in-java