Я создаю видео, захватывая изображения экрана с контекстом рендеринга каждые 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 пиксельный буфер = 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];