如何在 iOS 应用程序中使用 AVFoundation 获取视频的 FPS?我已经尝试了下面的代码,但它似乎不正确。
AVAssetTrack *videoAssetTrack = [myAsset tracksWithMediaType:AVMediaTypeVideo].firstObject;
NSLog(@"FPS is : %f ",videoAssetTrack.nominalFrameRate);
我通过以下代码行获得了正确的 FPS:
AVAsset * myAsset = [[AVURLAsset alloc] initWithURL: _videoUrl options: nil];
AVAssetTrack * videoAssetTrack = [myAsset tracksWithMediaType: AVMediaTypeVideo].firstObject;
NSLog(@"FPS is : %f ", videoAssetTrack.nominalFrameRate);
extension AVAsset {
/**
* Get FPS from AVAsset
*/
var fps: Float? {
self.tracks(withMediaType: .video).first?.nominalFrameRate
}
}
iOS 16
您可以受益于 Swift 并发开发,通过使用
Load属性异步检索
FPS
、size or dimension
和 duration
。
var asset: AVAsset? = AVAsset(url: filePath)
if let asset = asset,
let videoTrack = try? await asset.loadTracks(withMediaType: .video).first {
let size = try? await videoTrack.load(.naturalSize)
let fps = try? await videoTrack.load(.nominalFrameRate)
let duration = try? await asset.load(.duration)
}