gpt4 book ai didi

iphone - 如何在 iPhone 中将 CVPixelBufferPool 与 AVAssetWriterInputPixelBufferAdaptor 结合使用?

转载 作者:太空狗 更新时间:2023-10-30 03:17:44 25 4
gpt4 key购买 nike

我已经使用以下代码成功地从图像创建了视频

-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size duration:(int)duration 
{
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];

AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];


//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage]];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

//Write samples:
for (int i = 0;i<[array count]; i++)
{
if([writerInput isReadyForMoreMediaData])
{
NSLog(@"inside for loop %d",i);
CMTime frameTime = CMTimeMake(1, 20);

CMTime lastTime=CMTimeMake(i, 20); //i is from 0 to 24 of the loop above

CMTime presentTime=CMTimeAdd(lastTime, frameTime);

buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:i] CGImage]];

[adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];

}
else
{
NSLog(@"error");
i--;
}
}
NSLog(@"outside for loop");

//Finish the session:
[writerInput markAsFinished];
[videoWriter finishWriting];
}

这里我使用了CVPixelBufferRef。取而代之的是,我想将 CVPixelBufferPoolRefAVAssetWriterInputPixelBufferAdaptor 结合使用。

任何人都可以提供一个我可以调试和使用的示例吗?

最佳答案

您传递的是 nil 'sourcePixelBufferAttributes',因此不会创建像素缓冲池:

AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:nil];

而是传递一些属性,例如:

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

然后您可以使用池来创建像素缓冲区,例如:

CVPixelBufferPoolCreatePixelBuffer (NULL, adaptor.pixelBufferPool, &pixelBuffer);

关于iphone - 如何在 iPhone 中将 CVPixelBufferPool 与 AVAssetWriterInputPixelBufferAdaptor 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4023842/

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