我正在构建一个使用 AVFoundation 来捕获视频的 iOS 视频应用程序。启动应用程序时,我创建了一个 AVCaptureSession,当用户按下“录制”按钮时,会捕获视频并将其写入“temp.MOV”名称下的 Documents 文件夹。这对我的 AVAssetExporter 来说没有问题,它可以合并并重新格式化我的作文。
但是,如果我尝试使用 UIImagePicker 中的视频,则 AVAssetExporter 会因错误而停止。
Export failed: Operation Stopped
Export failed: Error Domain=AVFoundationErrorDomain Code=-11841 "Operation Stopped" UserInfo=0x17db5100 {NSLocalizedDescription=Operation Stopped, NSLocalizedFailureReason=The video could not be composed.}
我想知道 UIImagePicker 中的视频是否需要做一些特别的事情。
//从 AVCaptureSession 写入的文件
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
NSLog(@"didFinishRecordingToOutputFileAtURL %@", outputFileURL);
//DOCUMENTS/temp.mov
self.movieFileURLForExport = outputFileURL;
}
UIImagePickerController 视频导入代码
- (IBAction)iba_importVideo:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc]
initWithObjects: (NSString *) kUTTypeMovie, nil];
picker.delegate = self;
picker.allowsEditing = YES;
picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] ||
[type isEqualToString:(NSString *)kUTTypeMovie]) {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"PickedVideoURL %@", [videoURL absoluteString]);
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/films"];
NSString *moviepath = [dataPath stringByAppendingString:@"/temp.mov"];
BOOL success = [videoData writeToFile:moviepath atomically:YES];
NSLog(@"Write Successs: %@", success ? @"YES" : @"NO");
[self checkDIRECTORYContents];
self.view.userInteractionEnabled = NO;
self.movieFileURLForExport = [NSURL fileURLWithPath:moviepath];
[picker dismissViewControllerAnimated:YES completion:nil];
}
}