1

0.01 秒ごとにレンダリング コンテキストで画面イメージをキャプチャしてビデオを作成しています。背景が透明なキャプチャ画像。しかし、AVAssetWritter、AVAssetWriterInputPixelBufferAdaptor を使用してこれらの画像でビデオを作成すると、透明な背景が黒色に変わります。

私は次のコードを使用しています:-

canvasAssetWriter = [[AVAssetWriter alloc] initWithURL:[self tempFileURL:@"output.mp4"] fileType:AVFileTypeQuickTimeMovie エラー:&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];
4

0 に答える 0