Search code examples
javamp4parser

How to obtain FPS using mp4parser?


Framerate can be calculated as

FPS = framescale / timescale

Timescale is easy to obtain using isoFile.getMovieBox().getMovieHeaderBox().getTimescale()

But I cannot figure out where is the framescale stored within the mp4parser isobox structure.

Some sample code I came across here uses low-level buffer read on the following path: /moov/trak/mdhd, but that doesn't seem to be accessible using mp4parser.

Question: Is it possible to extract framescale and/or framerate using mp4parser? Or is it better to use ffmpeg instead?

Note: I prefer using native libraries such as mp4parser over external executables (ffmpeg) where possible


Solution

  • This is an old question, but here is how you can get the frame rate using Mp4Parser:

    for(int i=0; i<video.getTracks().size();i++){
        Track videoTrack = video.getTracks().get(i);
        if(videoTrack.getHandler().equals("vide)){
            TrackMetaData metaData = videoTrack.getTrackMetaData();
            //Get timescale
            long timeScale = metaData.getTimescale();
            //Get video duration in Us
            long durationUs = videoTrack.getDuration() * 1000 *1000 / timeScale;
            //Get total frames
            long totalFrames = videoTrack.getSamples().size();
            //Calculate the frame duration
            long perFrameDurationUs =  durationUs /  totalFrames;
            //FPS
            long fps = 1000 * 1000 / perFrameDurationUs;
        }
    }
    

    video is refering to Movie video = MovieCreator.build(filePath);