2014-02-05 216 views
4

我正在嘗試從.m4v視頻中提取音頻文件。它顯示了錯誤一樣,從.m4v視頻文件中提取音頻時出錯

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x15e350d0 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x15d1c6b0 "The operation couldn’t be completed. (OSStatus error -12124.)", NSLocalizedFailureReason=An unknown error occurred (-12124)} 

這是我的代碼:

-(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  = [[NSBundle mainBundle] URLForResource:@"sample_iPod" withExtension:@"m4v"]; 
    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]; 


    //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(AVAssetExportSessionStatusCompleted != status){ 
      NSLog(@"Export status not yet completed. Error: %@", exportSession.error.description); 
     } 
    }]; 
} 

我怎樣才能解決這個問題?

回答

2

我已經在模擬器中測試過你的代碼。它在模擬器(IOS7)中工作正常。但是當我在iPodTouch-5上運行時,它會像您提到的那樣顯示錯誤。花了超過15分鐘,發現和愚蠢的錯誤。

獲得如下路徑(Documentssample_audio.mp4)當我在設備運行..

@"file:///var/mobile/Applications/A1E1D85F-0198-4A0C-80F8-222F0DA1C31A/Documentssample_audio.mp4" 

所以我修改路徑..

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; 
NSString *dstPath = [documentsDirectory stringByAppendingString:@"/sample_audio.mp4"]; 

現在我下面越來越路徑( /Documents/sample_audio.mp4)並且工作正常。但我不知道,怎麼回事

@"file:///var/mobile/Applications/A1E1D85F-0198-4A0C-80F8-222F0DA1C31A/Documents/sample_audio.mp4"