使用 AVCaptureSession 录制视频
Posted
技术标签:
【中文标题】使用 AVCaptureSession 录制视频【英文标题】:Recording video with AVCaptureSession 【发布时间】:2013-10-19 08:34:38 【问题描述】:我正在尝试使用 AVCaptureSession 和 AVCaptureMovieFileOutput 录制视频,但每当我尝试开始录制时,我都会在 didFinishRecordingToOutputFileAtURL
AVCaptureFileOutputRecordingDelegate 方法中收到此错误。
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x15de7c40 AVErrorRecordingSuccessfullyFinishedKey=false, NSLocalizedDescription=The operation could not be completed, NSLocalizedFailureReason=An unknown error occurred (-12673), NSUnderlyingError=0x15d88aa0 "The operation couldn’t be completed. (OSStatus error -12673.)"
这是我用来添加 AVCaptureMovieFileOutput 并初始化我的 AVCaptureSession 的代码
- (AVCaptureSession *)session
if (!_session)
_session = [[AVCaptureSession alloc] init];
// ADD CAMERA DEVICE
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:self.videoDevice error:&error];
if (!videoInput)
NSLog(@"ERROR: trying to open camera: %@", error);
else
[_session addInput:videoInput];
// ADD AUDIO DEVICE
error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:self.audioDevice error:&error];
if (!audioInput)
NSLog(@"ERROR: trying to open mic: %@", error);
else
[_session addInput:audioInput];
// ADD OUTPUT FILE
if ([_session canAddOutput:self.movieFileOutput])
[_session addOutput:self.movieFileOutput];
[_session startRunning];
return _session;
我的 AVCaptureMovieFileOutput 像这样延迟加载
- (AVCaptureMovieFileOutput *)movieFileOutput
if (!_movieFileOutput)
_movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
Float64 TotalSeconds = 60; //Total seconds
int32_t preferredTimeScale = 30; //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);
_movieFileOutput.maxRecordedDuration = maxDuration;
_movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;
return _movieFileOutput;
我不确定我做错了什么,因为我见过的大多数教程都是这样的。
谢谢
【问题讨论】:
你检查过文件的目标路径是否可写吗? 【参考方案1】:基本错误代码是-12673,这通常是由于尝试写入不可写的目录或文件引起的。尝试使用此代码编写:
NSString *documentsDirPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSURL *documentsDirUrl = [NSURL fileURLWithPath:documentsDirPath isDirectory:YES];
NSURL *url = [NSURL URLWithString:@"out.mp4" relativeToURL:documentsDirUrl];
[self.movieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];
【讨论】:
这似乎是问题所在。感谢您的帮助!以上是关于使用 AVCaptureSession 录制视频的主要内容,如果未能解决你的问题,请参考以下文章
使用 AVFoundation 框架(AVCaptureSession)在 iPhone 中录制视频?
为啥使用 AVCaptureSession、AVAssetWriter 和 AVPlayer 录制和回放视频会卡顿?
AVCaptureSession 和 AVAudioSession 在播放背景音乐时录制视频只能工作一次