ios - AVAssetWriter 可以以透明方式写入文件吗?
<p><p>如果我包含的图像没有填满整个渲染区域,我使用 AVAssetWriter 编写的每个文件都有黑色背景。有什么办法可以写透明吗?这是我用来获取像素缓冲区的方法:</p>
<pre><code>- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image {
CGSize size = self.renderSize;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
, kCVPixelBufferCGImageCompatibilityKey,
, 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;
}
</code></pre>
<p>还有 AVAssetWriter 代码:</p>
<pre><code>NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
, AVVideoWidthKey,
, AVVideoHeightKey,
nil];
AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
, kCVPixelBufferPixelFormatTypeKey, nil];
self.adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
sourcePixelBufferAttributes:bufferAttributes];
buffer = ];
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 = ;
if(!append_ok){
NSError *error = self.assetWriter.error;
if(error!=nil) {
NSLog(@"Unresolved error %@,%@.", error, );
}
}
}
else {
printf("adaptor not ready %d, %d\n", frameCount, j);
;
}
j++;
frameCount++;
}
if (!append_ok) {
printf("error appending image %d times %d\n, with error.", frameCount, j);
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>简而言之,这是不可能的。 <code>AVAssetWriter</code> 使用 h264 视频编解码器输出。 h264 视频编解码器不支持 alphachannel ,所以很不幸,您不走运。</p>
<p>更复杂的解决方案是将 alphachannel (透明度)渲染为灰度 h264 编码视频,并在查看时将 rub 和 alpha 信息重新组合在一起。</p></p>
<p style="font-size: 20px;">关于ios - AVAssetWriter 可以以透明方式写入文件吗?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21538729/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21538729/
</a>
</p>
页:
[1]