gpt4 book ai didi

ios - AVAssetWriter 可以透明写入文件吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:53 25 4
gpt4 key购买 nike

如果我包含的图像没有填满整个渲染区域,我用 AVAssetWriter 编写的每个文件都有黑色背景。有什么方法可以写透明吗?这是我用来获取像素缓冲区的方法:

- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image {

CGSize size = self.renderSize;

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CVPixelBufferPoolCreatePixelBuffer(NULL, self.adaptor.pixelBufferPool, &pxbuffer);

CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
size.width,
size.height,
kCVPixelFormatType_32ARGB,
(__bridge CFDictionaryRef) options,
&pxbuffer);


if (status != kCVReturnSuccess){
NSLog(@"Failed to create pixel buffer");
}

CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);

CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, size.width,
size.height, 8, 4*size.width, rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);

CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);

CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

return pxbuffer;
}

和 AVAssetWriter 代码:

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:self.renderSize.width], AVVideoWidthKey,
[NSNumber numberWithInt:self.renderSize.height], AVVideoHeightKey,
nil];

AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];

NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];
self.adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
sourcePixelBufferAttributes:bufferAttributes];

buffer = [self pixelBufferFromCGImage:[stickerImage CGImage]];

BOOL append_ok = YES;
int j = 0;
while (append_ok && j < totalFrames) {
if (self.adaptor.assetWriterInput.readyForMoreMediaData) {

CMTime frameTime = CMTimeMake(frameCount,(int32_t) fps);
append_ok = [self.adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
if(!append_ok){
NSError *error = self.assetWriter.error;
if(error!=nil) {
NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
}
}
}
else {
printf("adaptor not ready %d, %d\n", frameCount, j);
[NSThread sleepForTimeInterval:0.1];
}
j++;
frameCount++;
}
if (!append_ok) {
printf("error appending image %d times %d\n, with error.", frameCount, j);
}

最佳答案

简而言之,这是不可能的。 AVAssetWriter 使用 h264 视频编解码器输出。 h264 视频编解码器不支持 alpha channel ,因此您很不幸。

一个更复杂的解决方案是将 alpha channel (透明度)渲染成灰度 h264 编码视频,并在观看时将摩擦和 alpha 信息组合在一起。

关于ios - AVAssetWriter 可以透明写入文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21538729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com