sannies / mp4parser

A Java API to read, write and create MP4 files
Apache License 2.0
2.76k stars 566 forks source link

Feature Request - Obtain metadata for an MP4 stream by reading minimum bytes #233

Open mslinn opened 7 years ago

mslinn commented 7 years ago

Here is a SO posting which shows how to inhale an entire MP4 (stored locally), then discover its length:

public static long getAudioLength(byte[] content) throws Exception {
    IsoFile isoFile = new IsoFile(new MemoryDataSourceImpl(content));
    double lengthInSeconds = (double)isoFile.getMovieBox().getMovieHeaderBox().getDuration() / isoFile.getMovieBox().getMovieHeaderBox().getTimescale();
    return (long)lengthInSeconds;
}

I need to parse large MP4s stored on AWS S3 and obtain their length by reading a minimum of data. S3 has an API that allows a file to read as a Stream. An implementation of DataSource that knows how to handle a data stream seems like a way to accomplish the goal.

Seems it would be a generally useful feature to be able to obtain MP4 metadata from a Stream, and read the minimum number of bytes necessary. Is anyone interested in writing the DataSource subclass?

Related issues: #195 (Get total number of frames/video length of a mp4.file) and #196 (Support of parsing MP4 arriving as a stream).