我已经从 AVAssets 构建了 AVMutableComposition 和 VideoComposition 并且能够播放它。我也可以使用 AVAssetExportSession 导出它,但是 AVAssetExportSession 不能提供太多的设置控制,所以我使用 AVAssetReader/AVAssetWriter 导出它,但不幸的是我遇到了一个我不明白的错误,并且只是部分写入输出文件。
这是我到目前为止的代码。我已经省略了作者和尽可能多的其他内容(包括一些错误检查),我认为这些内容与使代码更易于阅读无关,因为它很多。请注意,我还没有处理音轨——我正在尝试一步一步来,但这也许是我的问题?
变量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)}