سؤال

أود تحويل أ CGImage إلى CMSampleBufferRef وإلحاقها بـ AVAssetWriterInput باستخدام appendSampleBuffer: طريقة. لقد تمكنت من الحصول على CMSampleBufferRef باستخدام الكود التالي ، ولكن appendSampleBuffer: ببساطة يعود 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 :(
}

PS أعلم أن هناك تسرب الذاكرة في هذا الرمز ، لقد حذفت بعض التعليمات البرمجية للبساطة.

هل كانت مفيدة؟

المحلول

آها ، لقد فاتني تماما AVAssetWriterInputPixelBufferAdaptor الفصل الذي تم صنعه خاصة بالنسبة للأنابيب المخازن المؤقتة للبكسل في مدخلات الكاتب. الآن يعمل الرمز ، حتى بدون الفوضى CMSampleBuffer أمور.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top