2013-12-10 168 views
2

我已经从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)} 

回答

0

看来我是以下一些不好的示例代码,而不是由苹果提供正确的样本代码,是相当清楚的,如果非常冗长,如何做到这一点:

https://developer.apple.com/library/Mac/DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html#//apple_ref/doc/uid/TP40010188-CH9-SW2

虽然我已经完全重写我的代码,我认为SP我遇到的ecific问题是从我的[self.assetReaderOutput copyNextSampleBuffer]返回的CMSampleBufferRef上调用CFRelease。你需要在循环中做到这一点。