我已經從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)}