用于转换视频音频的AVAssetExportSession需要10到15秒

时间:2015-05-25 09:52:12

标签: ios audio avassetexportsession

我已经实现了从视频转换音频的方案,但它需要大约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 个答案:

没有答案
相关问题