我已经从AVAssets构建AVMutableComposition和VideoComposition并且可以播放它。我也可以使用AVAssetExportSession导出它,但AVAssetExportSession不提供对设置的很多控制,所以我使用AVAssetReader/AVAssetWriter导出它,但不幸的是,我收到了一个我不明白的错误,并且只有部分写入输出文件。导出AVAssetReaderVideoCompositionOutput进行中途,然后用AVFoundationErrorDomain退出代码= -11800
这是我到目前为止的代码。我忽略了作者和尽可能多的其他内容(包括一些错误检查),我认为这使代码更易于阅读,因为它很多。请注意,我还没有处理音轨 - 我试图一次完成这一步,但也许这是我的问题?
变量asset
是AVMutableComposition。
// ------- reader
_assetReader = [AVAssetReader assetReaderWithAsset:asset error:error];
_assetReader.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
_duration = asset.duration;
// --- video reader
NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
NSDictionary *videoOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
_assetReaderOutput = [AVAssetReaderVideoCompositionOutput assetReaderVideoCompositionOutputWithVideoTracks:videoTracks
videoSettings:videoOptions];
((AVAssetReaderVideoCompositionOutput *)_assetReaderOutput).videoComposition = composition;
[_assetReader addOutput:_assetReaderOutput];
// -- leaving out the export settings and construction
// of the assetWriter since that doesn't seem to be the issue
// -- here's the actual export:
[self.assetWriter startWriting];
[self.assetWriter startSessionAtSourceTime:kCMTimeZero];
[self.assetReader startReading];
CMSampleBufferRef buffer;
while ([self.assetReader status]==AVAssetReaderStatusReading)
{
if(![self.assetWriterInput isReadyForMoreMediaData])
continue;
buffer = [self.assetReaderOutput copyNextSampleBuffer];
NSLog(@"READING");
if(buffer)
[self.assetWriterInput appendSampleBuffer:buffer];
NSLog(@"WRITTING...");
}
if(self.assetReader.status == AVAssetReaderStatusFailed) {
NSLog(@"%@", self.assetReader.error); //<--------- I get a problem here
}
//Finish the session:
[self.assetWriterInput markAsFinished];
[self.assetWriter finishWriting];
NSLog(@"Write Ended");
的问题是一个很短的时间后退出循环,我得到这样的输出:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x17f1dfa0 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x191617f0 "The operation couldn’t be completed. (OSStatus error -308.)", NSLocalizedFailureReason=An unknown error occurred (-308)}