2012-09-28 120 views
1

试图通过AVAssetWriter和AVAssetWriterInput方法将多个视频连接成一个时。在第三个视频后,我收到AVAssetWriter.error中的“无法编码”错误。此媒体所需的编码器正忙

此外,我能够通过控制台看到成功读取到缓冲区,但只有最后成功读取的视频结束了mov。任何一个或两个问题的深入了解,下面的来源和日志。由于

+(void)doWriteWithVideoWriter:(AVAssetWriter *)videoWriter withIndex:(int)index withWriterInputArray:(NSMutableArray *)writersArray withInstance:(VideoCombinerManager *)theInstance{ 
if ([writersArray count] > 0) 
{ 
    int newIndex = index+1; 

    NSError *readerError; 
    NSDictionary *videoOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; 

    AVAsset *sourceAsset = [theInstance.loadedAssetsForCompilationDictionary objectForKey:[theInstance.sortedKeysArray objectAtIndex:index]]; 
    AVAssetTrack *videoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
    AVAssetReaderTrackOutput *currentReaderTrackOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:videoOptions]; 
    AVAssetReader *currentReader = [[AVAssetReader alloc] initWithAsset:sourceAsset error:&readerError]; 
    [currentReader addOutput:currentReaderTrackOutput]; 
    [currentReader startReading]; 

    AVAssetWriterInput *writerInput = [[writersArray objectAtIndex:0] retain]; 

    dispatch_queue_t _processingQueue = dispatch_queue_create("asdf", NULL); 
    [writerInput requestMediaDataWhenReadyOnQueue:_processingQueue usingBlock:^{ 
    if ([writerInput isReadyForMoreMediaData]) 
    { 
     CMSampleBufferRef nextSampleBuffer; 

     if ([currentReader status] == AVAssetReaderStatusReading && 
     (nextSampleBuffer = [currentReaderTrackOutput copyNextSampleBuffer])) { 

     if (nextSampleBuffer) 
     { 
      BOOL result = [writerInput appendSampleBuffer:nextSampleBuffer]; 
      if (!result) 
      { 
      NSLog(@"videoWriter.error.userInfo: %@", videoWriter.error.userInfo); 
      } 
      CFRelease(nextSampleBuffer); 
     } 
     } 
     else 
     { 
     NSLog(@"writer done: %d", index); 
     dispatch_release(_processingQueue); 

     [writersArray removeObjectAtIndex:0]; 

     [writerInput markAsFinished]; 
     [writerInput release]; 

     [currentReader release]; 
     [currentReaderTrackOutput release]; 

     [VideoCombinerManager doWriteWithVideoWriter:videoWriter withIndex:newIndex withWriterInputArray:writersArray withInstance:theInstance]; 
     } 
    } 
    }]; 
} 
else 
    [videoWriter finishWriting]; 

} 

作家完成:0

作家做:1名

作家完成:2

videoWriter.error.userInfo: { 
    NSLocalizedDescription = "Cannot Encode"; 
    NSLocalizedFailureReason = "The encoder required for this media is busy."; 
    NSLocalizedRecoverySuggestion = "Stop any other actions that encode media and try again."; 
    NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-12915 \"The operation couldn\U2019t be completed. (OSStatus error -12915.)\""; 
} 

videoWriter.error.userInfo: { 
    NSLocalizedDescription = "Cannot Encode"; 
    NSLocalizedFailureReason = "The encoder required for this media is busy."; 
    NSLocalizedRecoverySuggestion = "Stop any other actions that encode media and try again."; 
    NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-12915 \"The operation couldn\U2019t be completed. (OSStatus error -12915.)\""; 
} 

回答

3

至于不能编码错误,你有太多的作家和/或writer并发输入。当我面对同样的错误时,我意识到我必须释放所有以前使用过的作家和writerInput才能摆脱它。 既然你确实释放了它们,你的问题可能就是你同时拥有了许多它们。