Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I convert a CGImage to CMSampleBufferRef?
    primarykey
    data
    text
    <p>I’d like to convert a <code>CGImage</code> to <code>CMSampleBufferRef</code> and append it to a <code>AVAssetWriterInput</code> using the <code>appendSampleBuffer:</code> method. I’ve managed to get the <code>CMSampleBufferRef</code> using the following code, but the <code>appendSampleBuffer:</code> simply returns <code>NO</code> when I supply the resulting <code>CMSampleBufferRef</code>. What am I doing wrong?</p> <pre><code>- (void) appendCGImage: (CGImageRef) frame { const int width = CGImageGetWidth(frame); const int height = CGImageGetHeight(frame); // Create a dummy pixel buffer to try the encoding // on something simple. CVPixelBufferRef pixelBuffer = NULL; CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, NULL, &amp;pixelBuffer); NSParameterAssert(status == kCVReturnSuccess &amp;&amp; pixelBuffer != NULL); // Sample timing info. CMTime frameTime = CMTimeMake(1, 30); CMTime currentTime = CMTimeAdd(lastSampleTime, frameTime); CMSampleTimingInfo timing = {frameTime, currentTime, kCMTimeInvalid}; OSStatus result = 0; // Sample format. CMVideoFormatDescriptionRef videoInfo = NULL; result = CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBuffer, &amp;videoInfo); NSParameterAssert(result == 0 &amp;&amp; videoInfo != NULL); // Create sample buffer. CMSampleBufferRef sampleBuffer = NULL; result = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &amp;timing, &amp;sampleBuffer); NSParameterAssert(result == 0 &amp;&amp; sampleBuffer != NULL); // Ship out the frame. NSParameterAssert(CMSampleBufferDataIsReady(sampleBuffer)); NSParameterAssert([writerInput isReadyForMoreMediaData]); BOOL success = [writerInput appendSampleBuffer:frame]; NSParameterAssert(success); // no go :( } </code></pre> <p>P.S. I know there are memory leaks in this code, I’ve omitted some of the code for simplicity.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload