sannies / mp4parser

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

How to get FPS? #384

Closed michalrames closed 4 years ago

michalrames commented 5 years ago

Provided there's no API for reading track FPS, I wanted to calculate it as FPS=framescale/timescale. Timescale can be extracted easily, but I can't see any way to extract framescale.

Am I missing something? Is this a feature request?

My SO question: https://stackoverflow.com/questions/56749565/how-to-obtain-fps-using-mp4parser

Thanks

HBiSoft commented 4 years ago

You can get the FPS by doing the following:

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);

michalrames commented 4 years ago

Thanks. I have since migrated my code to ffprobe. I think this is worth adding to the examples and/or documentation.

HBiSoft commented 4 years ago

@michalrames Please consider closing the issue