Iphone 将OpenGL视图捕获到AvassetWriterInputPixelBufferAdapter

Iphone 将OpenGL视图捕获到AvassetWriterInputPixelBufferAdapter,iphone,export,avfoundation,opengl-es-2.0,avassetwriter,Iphone,Export,Avfoundation,Opengl Es 2.0,Avassetwriter,我正在尝试创建一个AVAssetWriter来截屏openGL项目。我从来没有写过AVAssetWriter或AvassetWriterInputPixelBufferAdapter,所以我不确定我是否做得对 - (id) initWithOutputFileURL:(NSURL *)anOutputFileURL { if ((self = [super init])) { NSError *error; movieWriter = [[AVAssetW

我正在尝试创建一个AVAssetWriter来截屏openGL项目。我从来没有写过AVAssetWriter或AvassetWriterInputPixelBufferAdapter,所以我不确定我是否做得对

- (id) initWithOutputFileURL:(NSURL *)anOutputFileURL {
    if ((self = [super init])) {
        NSError *error;
        movieWriter = [[AVAssetWriter alloc] initWithURL:anOutputFileURL fileType:AVFileTypeMPEG4 error:&error];
        NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                       AVVideoCodecH264, AVVideoCodecKey,
                                       [NSNumber numberWithInt:640], AVVideoWidthKey,
                                       [NSNumber numberWithInt:480], AVVideoHeightKey,
                                       nil];
        writerInput = [[AVAssetWriterInput
                        assetWriterInputWithMediaType:AVMediaTypeVideo
                        outputSettings:videoSettings] retain];
        writer = [[AVAssetWriterInputPixelBufferAdaptor alloc] initWithAssetWriterInput:writerInput sourcePixelBufferAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,nil]];

        [movieWriter addInput:writerInput];
        writerInput.expectsMediaDataInRealTime = YES;
    }

    return self;
}
课程的其他部分:

- (void)getFrame:(CVPixelBufferRef)SampleBuffer:(int64_t)frame{
    frameNumber = frame;
    [writer appendPixelBuffer:SampleBuffer withPresentationTime:CMTimeMake(frame, 24)]; 
}

- (void)startRecording {
   [movieWriter startWriting];
   [movieWriter startSessionAtSourceTime:kCMTimeZero];
}

- (void)stopRecording {
   [writerInput markAsFinished];
   [movieWriter endSessionAtSourceTime:CMTimeMake(frameNumber, 24)];
   [movieWriter finishWriting];
}
assetwriter由以下人员启动:

    NSURL *outputFileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"]];
    recorder = [[GLRecorder alloc] initWithOutputFileURL:outputFileURL];
通过以下方式记录视图:

    glReadPixels(0, 0, 480, 320, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    for(int y = 0; y <320; y++) {
    for(int x = 0; x <480 * 4; x++) {
        int b2 = ((320 - 1 - y) * 480 * 4 + x);
        int b1 = (y * 4 * 480 + x);
        buffer2[b2] = buffer[b1];
    }
}    
pixelBuffer = NULL;
CVPixelBufferCreateWithBytes (NULL,480,320,kCVPixelFormatType_32BGRA,buffer2,1920,NULL,0,NULL,&pixelBuffer);
[recorder getFrame:pixelBuffer :framenumber];
    framenumber++;
注:

pixelBuffer是一个CVPixelBufferRef。 framenumber是一个int64\t。 缓冲区和缓冲区2是一个字节


我没有收到任何错误,但当我完成录制时,没有文件。任何帮助或帮助链接将不胜感激。opengl已从摄像机的实时馈送中恢复。我已经能够将屏幕保存为UIImage,但希望获得我所创建内容的电影。

如果您正在编写RGBA帧,我认为您可能需要使用AvassetWriteInputPixelBufferAdapter来编写它们。这个类应该管理一个像素缓冲池,但我得到的印象是,它实际上将您的数据按摩到YUV中


如果这是可行的,那么我想你会发现你的颜色都被交换了,在这一点上你可能需要编写像素着色器来将它们转换为BGRA。或抖动在CPU上执行此操作。取决于您。

我想我正在发送RGBA数据,但我可能错了。我正在使用AvassetWriterInputPixelBufferAdapter。我认为问题在于我没有向它发送一个像素缓冲池。我不知道该怎么做。如果你正在使用AvassetWriterInputPixelBufferAdapter,那么你应该更新你的问题。