Search code examples
objective-cavfoundationavasset

Cut AVAsset faster


I have this code to cut my AVAsset Video:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:_url options:nil];

[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@",_url] error:nil];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *outputURL = paths[0];
NSFileManager *manager = [NSFileManager defaultManager];
[manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
[manager removeItemAtPath:outputURL error:nil];

exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
CMTime start = CMTimeMakeWithSeconds(slider.min*(videoDuration-1), 600);
CMTime duration = CMTimeMakeWithSeconds(slider.max*(videoDuration-1), 600);
CMTimeRange range = CMTimeRangeMake(start, duration);
exportSession.timeRange = range;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
     switch (exportSession.status) {
         case AVAssetExportSessionStatusCompleted:

             _url = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",outputURL]];

             break;
         default:
             break;
     }

 }];

The problem: It takes a while to save the AVAsset to the URL and reload it. Is it possible to make it faster?


Solution

  • You could use AVAssetExportPresetPassthrough as your preset name, instead of AVAssetExportPresetHighestQuality. You may be able to avoid an expensive transcode that way. Setting a time range may preclude this for some formats, but it's worth trying.