从.m4v视频文件提取音频时出现错误。

4

我正在尝试从.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);
        }
    }];
}

我该如何解决这个错误?
1个回答

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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接