This worked for me to get the time in seconds, you can add this method to your code:
-(float) getDurationInSecondsForVideoPath:(NSString*)videoPath {
NSURL * videoURL = [NSURL URLWithString: videoPath];
AVURLAsset * avAsset = [AVURLAsset videoURL];
CMTime avDuration = [avAsset duration];
float avDurationInSeconds = CMTimeGetSeconds(avDuration);
return avDurationInSeconds;
}
Note: Make sure that the videoPath
works before use the method, otherwise the method will return 0.0000
or nan
For more information:AVAsynchronousKeyValueLoading, AVURLAsset and AVAsset.