AVAssetExportSession занимает от 10 до 15 секунд для преобразования аудио из видео

Я реализовал сценарий конвертации аудио из видео, но он занимает от 10 до 15 секунд, я хочу, чтобы он работал намного быстрее, так как он не удобен для пользователя в моем приложении. Пожалуйста, предложите мне решение

-(void)extractAudioFromVideo{

//Create a audia composition and add audio track
AVMutableComposition *newAudioAsset = [AVMutableComposition composition];
AVMutableCompositionTrack *dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

//Get video asset from which the audio should be extracted
NSURL *url      = outputMainUrl;
AVAsset *srcAsset  = [AVAsset assetWithURL:url];

NSArray *trackArray = [srcAsset tracksWithMediaType:AVMediaTypeAudio];
if(!trackArray.count){
    NSLog(@"Track returns empty array for mediatype AVMediaTypeAudio");
    return;
}

AVAssetTrack *srcAssetTrack = [trackArray  objectAtIndex:0];

//Extract time range
CMTimeRange timeRange = srcAssetTrack.timeRange;

//Insert audio from the video to mutable avcomposition track
NSError *err = nil;
if(NO == [dstCompositionTrack insertTimeRange:timeRange ofTrack:srcAssetTrack atTime:kCMTimeZero error:&err]){
    NSLog(@"Failed to insert audio from the video to mutable avcomposition track");
    return;
}

//Export the avcompostion track to destination path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSString *dstPath = [documentsDirectory stringByAppendingString:@"/sample_audio.mp4"];
NSURL *dstURL = [NSURL fileURLWithPath:dstPath];
urlOrigionalAudio = dstURL;

//Remove if any file already exists
[[NSFileManager defaultManager] removeItemAtURL:dstURL error:nil];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];
NSLog(@"support file types= %@", [exportSession supportedFileTypes]);
exportSession.outputFileType = @"public.mpeg-4";
exportSession.outputURL = dstURL;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    AVAssetExportSessionStatus status = exportSession.status;
    if(status == AVAssetExportSessionStatusCompleted){
        urlOrigionalAudio = exportSession.outputURL;
        [SVProgressHUD dismiss];
    }
    else if (status == AVAssetExportSessionStatusFailed){
        [SVProgressHUD showErrorWithStatus:@"Failled!"];
        NSLog(@"Export status not yet completed");
    }
}];

}

0 ответов

Другие вопросы по тегам