Payal Maniyar
Payal Maniyar

Reputation: 4402

Get FPS of video in ios

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

Answers (3)

Sajjad Sarkoobi
Sajjad Sarkoobi

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

Sentry.co
Sentry.co

Reputation: 5569

AVAsset extension for Swift 5

extension AVAsset {
   /**
    * Get FPS from AVAsset
    */
   var fps: Float? {
      self.tracks(withMediaType: .video).first?.nominalFrameRate
   }
}

Upvotes: 3

Payal Maniyar
Payal Maniyar

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

Related Questions