Reputation: 4402
How do I get the FPS of a video using AVFoundation in an iOS app? I have tried the code below, but it does not seem right.
AVAssetTrack *videoAssetTrack = [myAsset tracksWithMediaType:AVMediaTypeVideo].firstObject;
NSLog(@"FPS is : %f ",videoAssetTrack.nominalFrameRate);
Upvotes: 4
Views: 2832
Reputation: 1064
iOS 16
You can benefit from Swift concurrency development to retrieve FPS
, size or dimension
, and duration
, by using Load properties asynchronously.
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)
}
Upvotes: 1
Reputation: 5569
extension AVAsset {
/**
* Get FPS from AVAsset
*/
var fps: Float? {
self.tracks(withMediaType: .video).first?.nominalFrameRate
}
}
Upvotes: 3
Reputation: 4402
I got correct FPS with below line of code :
AVAsset * myAsset = [[AVURLAsset alloc] initWithURL: _videoUrl options: nil];
AVAssetTrack * videoAssetTrack = [myAsset tracksWithMediaType: AVMediaTypeVideo].firstObject;
NSLog(@"FPS is : %f ", videoAssetTrack.nominalFrameRate);
Upvotes: 5