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

标签 iphone objective-c avfoundation avassetwriter

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

-(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/

相关文章:

iphone - 在 Xcode 中添加自定义框架时出错

iphone - 如何才能获得 iPhone 系统音频录制的最佳效果?

iphone - NSMutableArray replaceObjectAtIndex : withObject: Not Working

ios - 简单的Objective-C属性设置错误

iphone - 暂停执行直到 Websocket 连接

ios - 管理数组 - 发送到不可变对象(immutable对象)的变异方法

ios - 从 cellForRowAtIndexPath 返回两个不同的单元格

ios - iPhone : Container View Controller and Camera Preview: preview is not displayed in child view controller

swift - 使用 Swift 从视频中抓取帧

ios - AVAssetExportSession 中的上限帧率或比特率?