Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAsset duration is not correct

I have video in Mac player duration of video is 31 seconds. When I'm using it in my app and loading that file the duration of AVAsset is '28.03'.

AVAsset *videoAsset = [AVAsset assetWithURL:videoUrl];
Float64 time = CMTimeGetSeconds(videoAsset.duration);
like image 255
ghkaren Avatar asked Oct 02 '22 03:10

ghkaren


1 Answers

For some types of assets a duration is an approximation. If you need the exact duration (should be an extreme case) use:

NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
AVURLAsset *videoAsset = [URLAssetWithURL:videoUrl options:options];

You can find more informations in documentation. Calculating the duration may take some time, so remember to use asynchronous loading:

[videoAsset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
    switch ([videoAsset statusOfValueForKey:@"duration" error:nil]) {
        case AVKeyValueStatusLoaded:
            Float64 time = CMTimeGetSeconds(videoAsset.duration);
            // ...
            break;
        default:
            // other cases like cancellation or fail
            break;
    }
}];

You can find some more tips on using AVFoundation API in the video Discovering AV Foundation - WWDC 2010 Session 405

like image 80
Tomasz Bąk Avatar answered Oct 13 '22 10:10

Tomasz Bąk