使用CVOpenGLESTextureCache将GLKView记录到mov文件

时间:2013-02-28 14:42:30

标签: ios opengl-es-2.0 glkit

我是opengl的新手,我正试图记录GLKView到目前为止没有运气。 这是我的代码:

     EAGLContext * _context = self.glkview.context;

#if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API
    CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, _context, NULL, &_videoTextureCache);
#else
    CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)_context, NULL, &_videoTextureCache);
#endif
    if (err)
    {
        NSLog(@"Error at CVOpenGLESTextureCacheCreate %d", err);
        return;
    }


   CFDictionaryRef empty; // empty value for attr value.
CFMutableDictionaryRef attrs;
empty = CFDictionaryCreate(kCFAllocatorDefault, // our empty IOSurface properties dictionary
                           NULL,
                           NULL,
                           0,
                           &kCFTypeDictionaryKeyCallBacks,
                           &kCFTypeDictionaryValueCallBacks);
attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                  1,
                                  &kCFTypeDictionaryKeyCallBacks,
                                  &kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(attrs,
                     kCVPixelBufferIOSurfacePropertiesKey,
                     empty);

// for simplicity, lets just say the image is 640x480
CVPixelBufferCreate(kCFAllocatorDefault, 640, 480,
                    kCVPixelFormatType_32BGRA,
                    attrs,
                    &renderTarget);
// in real life check the error return value of course.


// first create a texture from our renderTarget
// textureCache will be what you previously made with CVOpenGLESTextureCacheCreate
CVOpenGLESTextureRef renderTexture;
CVOpenGLESTextureCacheCreateTextureFromImage (
                                              kCFAllocatorDefault,
                                              _videoTextureCache,
                                              renderTarget,
                                              NULL, // texture attributes
                                              GL_TEXTURE_2D,
                                              GL_RGBA, // opengl format
                                              640,
                                              480,
                                              GL_BGRA, // native iOS format
                                              GL_UNSIGNED_BYTE,
                                              0,
                                              &renderTexture);
// check err value

// set the texture up like any other texture
glBindTexture(CVOpenGLESTextureGetTarget(renderTexture),
              CVOpenGLESTextureGetName(renderTexture));
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// bind the texture to the framebuffer you're going to render to 
// (boilerplate code to make a framebuffer not shown)

 glPixelStorei(GL_UNPACK_ALIGNMENT,4);

 glGenTextures(1, &_texureName);
 glBindTexture(GL_TEXTURE_2D, _texureName);
 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) 1024, (GLsizei) 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(renderTarget));

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                       GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture), 0);

这是将文件写入文件的方法。

- (void)writeVideoFrameAtTime:(CMTime)time {

    if (_videoTextureCache == NULL)
        return;

        BOOL success = [_assetWriterPixelBufferInput appendPixelBuffer:renderTarget withPresentationTime:time];
        //NSLog(@"writing");
        if (!success) {
            NSLog(@"Pixel Buffer not appended");
        }

}

1 个答案:

答案 0 :(得分:0)

你可以尝试这个,它是免费的,它使用起来非常简单 https://developers.everyplay.com