Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: None  |  size: 2.11 KB  |  views: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Hold multiple Frames in Memory before sending them to AVAssetWriter
  2. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
  3. {      
  4.     //...
  5.     CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  6.     uint8 *baseAddress = (uint8*)CVPixelBufferGetBaseAddress(imageBuffer);
  7.     NSData *rawFrame = [[NSData alloc] initWithBytes:(void*)baseAddress length:(height * bytesPerRow)];
  8.     [m_frameDataArray addObject:rawFrame];
  9.     [rawFrame release];
  10.     //...
  11. }
  12.        
  13. -(void)writeFramesToFile
  14. {
  15.     //...
  16.     NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
  17.                                     [NSNumber numberWithInt:640], AVVideoWidthKey,
  18.                                     [NSNumber numberWithInt:480], AVVideoHeightKey,
  19.                                     AVVideoCodecH264, AVVideoCodecKey,
  20.                                     nil ];
  21.     AVAssetWriterInput *bufferAssetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
  22.     AVAssetWriter *bufferAssetWriter = [[AVAssetWriter alloc]initWithURL:pathURL fileType:AVFileTypeQuickTimeMovie error:&error];
  23.     [bufferAssetWriter addInput:bufferAssetWriterInput];
  24.  
  25.     [bufferAssetWriter startWriting];
  26.     [bufferAssetWriter startSessionAtSourceTime:startTime];
  27.     for (NSInteger i = 1; i < m_frameDataArray.count; i++){
  28.         NSData *rawFrame = [m_frameDataArray objectAtIndex:i];
  29.         CVImageBufferRef imgBuf = [rawFrame bytes];
  30.         [pixelBufferAdaptor appendPixelBuffer:imgBuf withPresentationTime:CMTimeMake(1,10)]; //<-- EXC_BAD_ACCESS
  31.         [rawFrame release];
  32.     }
  33.     //... (finishing video file)
  34. }
  35.        
  36. CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  37. CVPixelBufferLockBaseAddress(imageBuffer, 0);
  38. uint8 *baseAddress = (uint8*)CVPixelBufferGetBaseAddress(imageBuffer);
  39. NSData *rawFrame = [[NSData alloc] initWithBytes:(void*)baseAddress length:(height * bytesPerRow)];
  40. [m_frameDataArray addObject:rawFrame];
  41. CVPixelBufferUnlockBaseAddress(imageBuffer, 0);