无法在iOS objective-c

时间:2017-10-13 06:58:34

标签: ios objective-c mp4 avi

我正在使用相机录制视频并将其保存到我的文档文件夹中。我面临的问题是我从相机获取的视频是.avi文件,必须转换为.mp4(或任何其他允许的格式)。

我使用下面的代码:
消息来源:iOS Convert AVI to MP4 Format programatically

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyVideo.mp4"];
NSURL *outputURL = [NSURL fileURLWithPath:filePath];

[self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
{
    if (exportSession.status == AVAssetExportSessionStatusCompleted) {
        // Video conversation completed
    }          
}];

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler {
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
         handler(exportSession);
     }];
}

exportSession状态始终失败。但是,当我尝试将.mov转换为.mp4时,确实有效。

为什么我无法将.avi文件转换为.mp4文件?

1 个答案:

答案 0 :(得分:0)

尝试打印错误消息:

switch ([exportSession status]) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
            break;
            case AVAssetExportSessionStatusCompleted:
            NSLog(@"Successfully");
            break;
        default:
            break;
        }

    }];
}