我已经从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)}
答案 0 :(得分:0)
似乎我正在关注一些糟糕的示例代码,而不是正确的示例代码,这非常清楚,如果非常冗长,关于如何执行此操作:
虽然我已经完全重写了我的代码,但我认为我遇到的具体问题是没有在[self.assetReaderOutput copyNextSampleBuffer]返回的CMSampleBufferRef上调用CFRelease。你需要在循环中做到这一点。