6

i know duration of video type questions have been answered before but i am facing real trouble in getting duration of an .mp4 file by using AVAsset and by AVURLAsset. i am using Following code

NSString *itemPathString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:obmodel.actualname];
NSURL *itemPathURL = [NSURL URLWithString:itemPathString];

if([[NSFileManager defaultManager] fileExistsAtPath:itemPathString]){

    NSLog(@"File Exists");
}

AVAsset *videoAsset = (AVAsset *)[AVAsset assetWithURL:itemPathURL];
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

NSLog(@"duration: %.2f", CMTimeGetSeconds(videoAssetTrack.timeRange.duration));
if(CMTimeGetSeconds(videoAsset.duration) >= 59.0){
}

but every time it returns 0 i have also imported following headers

#import <AVFoundation/AVAsset.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>

i have also tried some other code that i know but neither one worked for me. My video is working properly if I play it through iExplorer, my Path is good but don't know whats i'm missing. i would like to share that what i have found is that my AVAssets are not loaded properly please suggest me what should be done.

Syed Ali Salman
  • 2,894
  • 4
  • 33
  • 48

1 Answers1

9

Yes, I've had similar problems before. For me the problem went away when I used:

NSURL *itemPathURL = [NSURL fileURLWithPath:itemPathString];

instead of

NSURL *itemPathURL = [NSURL URLWithString:itemPathString];

If you're completely sure your path is ok, this should fix your problem.

Hope this helps!

LuisCien
  • 6,362
  • 4
  • 34
  • 42
  • Hi @LuisCien, need your help in https://stackoverflow.com/questions/55605987/issue-while-merging-video-audio-ios-swift4 ... This Question is same like this .. please take a look at this... Thanks – Ahtazaz Apr 10 '19 at 06:53