我目前正在使用 AVCaptureSession 捕获视频和音频,并已设置管道以使用 AVAssetWriter 将数据写入磁盘。对于短视频,这完全没有问题。
但是,如果录制媒体的长度超过了任意时间,通常约为 2 分钟,则完成写入的调用会失败,但仍会设法生成仅播放大约 50% 内容的视频文件。我在写入过程中不断调用 isReadyForMoreMediaData 和 appendSampleBuffer ,它们总是返回 true。在完成调用之前,我似乎没有收到任何出现问题的通知。
在失败的 finishWriting 调用后 AVAssetWriters 错误设置为以下
Capture failed to end with status 3 and error Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x210b7880 {NSLocalizedFailureReason=An unknown error occurred (268451843), NSUnderlyingError=0x210b1ee0 "The operation couldn’t be completed. (OSStatus error 268451843.)", NSLocalizedDescription=The operation could not be completed}
不是世界上最有用的错误...
紧随其后的是对我设置的 AVCaptureSessionRuntimeErrorNotification 侦听器的调用,其中包含错误
Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo=0x21011940 {NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action}
再次不是很有帮助......
这是我用来为音频配置 AVAssetWriter 输入的代码
//\Configure Audio Writer Input
AudioChannelLayout acl;
bzero(&acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary* audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,
[ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
[ NSData dataWithBytes: &acl length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
[ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
nil];
m_audioWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings] retain];
这是我用来为视频配置 AVAssetWriterInput 的代码
videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:640], AVVideoWidthKey,
[NSNumber numberWithInt:480], AVVideoHeightKey,
nil];
m_videoWriterInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
m_videoWriterInput.expectsMediaDataInRealTime = YES;
这是我用来初始化 AVAssetWriter 的代码
m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:outputPath] fileType:AVFileTypeQuickTimeMovie error:&error];
m_audioAndVideoWriter.shouldOptimizeForNetworkUse = YES;
[m_audioAndVideoWriter addInput:m_videoWriterInput];
[m_audioAndVideoWriter addInput:m_audioWriterInput];
有没有人遇到过类似的问题?或者知道可能导致此问题的 finishWriting 调用中发生了什么?或者也许有人知道调试这个问题的一些新方法?
谢谢