从AVAssetWriterInputPixelBufferAdaptor创建的具有透明背景的视频变为黑色背景

时间:2015-12-18 07:12:42

标签: ios screen-capture avassetwriter core-video cvpixelbuffer

我通过每0.01秒捕获具有渲染上下文的屏幕图像来创建视频。捕获的图像具有透明背景。但是当使用AVAssetWritter创建带有这些图像的视频时,AVAssetWriterInputPixelBufferAdaptor将其透明背景变为黑色。

我使用以下代码: -

canvasAssetWriter = [[AVAssetWriter alloc] initWithURL:[self tempFileURL:@“output.mp4”] fileType:AVFileTypeQuickTimeMovie error:& error];

NSParameterAssert(canvasAssetWriter);

//Configure video
NSDictionary* videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [NSNumber numberWithDouble:1024.0*1024.0], AVVideoAverageBitRateKey,
                                       nil ];

NSLog(@"System Version : %f WindowWidth : %f, windowHeight : %f",kSystemVersion,kWindowWidth,kWindowHeight);

NSDictionary* videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt:(kSystemVersion < 8.0)?kWindowHeight:kWindowWidth], AVVideoWidthKey,
                               [NSNumber numberWithInt:(kSystemVersion < 8.0)?kWindowWidth:kWindowHeight], AVVideoHeightKey,
                               videoCompressionProps, AVVideoCompressionPropertiesKey,
                               nil];

canvasVideoAssetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain];

NSParameterAssert(canvasVideoAssetWriterInput);
canvasVideoAssetWriterInput.expectsMediaDataInRealTime = NO;


NSDictionary* bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInt:kCMPixelFormat_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];

avAdaptor = [[AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:canvasVideoAssetWriterInput sourcePixelBufferAttributes:bufferAttributes] retain];

//add input
[canvasAssetWriter addInput:canvasVideoAssetWriterInput];
[canvasAssetWriter startWriting];
[canvasAssetWriter startSessionAtSourceTime:CMTimeMake(0, 1000)];

CVPixelBufferRef pixelBuffer = NULL;                 CGImageRef cgImage = CGImageCreateCopy([capturedImage CGImage]);

            CFDataRef image = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));


        pixelBuffer = [self pixelBufferFasterWithImage:cgImage];

            if(pixelBuffer !=nil)
            {
                // set image data into pixel buffer
                CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
                uint8_t* destPixels = CVPixelBufferGetBaseAddress(pixelBuffer);
                CFDataGetBytes(image, CFRangeMake(0, CFDataGetLength(image)), destPixels);  //XXX:  will work if the pixel buffer is contiguous and has the same bytesPerRow as the input data

                BOOL success = [avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];

0 个答案:

没有答案
相关问题