代码之家  ›  专栏  ›  技术社区  ›  zoul

如何将CGImage转换为CMSampleBufferRef?

  •  14
  • zoul  · 技术社区  · 15 年前

    我想兑换一张支票 CGImage CMSampleBufferRef 并将其附加到 AVAssetWriterInput appendSampleBuffer: 方法。我设法得到了 CMSampleBufferRef公司 使用以下代码,但是 追加SampleBuffer: 简单地返回 NO CMSampleBufferRef公司 . 我做错什么了?

    - (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, &pixelBuffer);
        NSParameterAssert(status == kCVReturnSuccess && 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, &videoInfo);
        NSParameterAssert(result == 0 && videoInfo != NULL);
    
        // Create sample buffer.
        CMSampleBufferRef sampleBuffer = NULL;
        result = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault,
            pixelBuffer, true, NULL, NULL, videoInfo, &timing, &sampleBuffer);
        NSParameterAssert(result == 0 && sampleBuffer != NULL);
    
        // Ship out the frame.
        NSParameterAssert(CMSampleBufferDataIsReady(sampleBuffer));
        NSParameterAssert([writerInput isReadyForMoreMediaData]);
        BOOL success = [writerInput appendSampleBuffer:frame];
        NSParameterAssert(success); // no go :(
    }
    

    1 回复  |  直到 15 年前
        1
  •  6
  •   zoul    15 年前

    啊哈,我完全错过了比赛 AVAssetWriterInputPixelBufferAdaptor 类,该类专门用于将像素缓冲区管道化到写入程序输入中。现在代码工作了,即使没有混乱的 CMSampleBuffer

    推荐文章