简体   繁体   中英

iOS get video frame rate

I want to get the frame rate for a specific video. I tried to look at APIs in the AVFoundation and AssetsLibrary like AVURLAsset or AVAssetReader. None of them are really helpful. Does anybody know a method in the Apple's frameworks/library to get the frame rate

The easier option is to obtain an AVAssetTrack and read its nominalFrameRate property. Looks like this can help you.

Nominal frame rate is the frame rate of the track, in frames per second. (read-only) AVAssetTrack Class @property(nonatomic, readonly) float nominalFrameRate

For iOS 7+ you can use the currentVideoFrameRate property of AVPlayerItemTrack. Its the only consistent property that I've seen measure FPS. The nominalFrameRate property seems to be broken in HLS streams.

AVPlayerItem *item = AVPlayer.currentItem; // Your current item
float fps = 0.00;
for (AVPlayerItemTrack *track in item.tracks) {
    if ([track.assetTrack.mediaType isEqualToString:AVMediaTypeVideo]) {
        fps = track.currentVideoFrameRate;
    }
}

For the property currentVideoFrameRate , from Apple's document :

If the item is not playing, or if the media type of the track is not video, the value of this property is 0.0.

It requires the item is in "playing", so I used nominalFrameRate instead.

It still works in iOS 12, swift 4:

let tracks = asset.tracks(withMediaType: .video)

let fps = tracks?.first?.nominalFrameRate

Remember to handle nil checking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM