OpenGL ES: storing a series of textures as a movie file
I have been grabbing live video frames from the iPhone camera and applying them as a texture to flat OpenGL geometry in order to use shaders for pro开发者_高级运维cessing the camera input. Does anyone know if it's possible to then grab the processed textures and store them in a movie file?
It seems like this would take a lot of processing power, and I'm not sure what the best way to proceed is -- or if it's even possible. The motivation here is to be able to do interesting shader effects on video grabbed from the camera and then store the results to share with others.
Thanks in advance for your thoughts.
I can't comment as to performance, but what you want to do is:
- create an
AVAssetWriter
(which can take incoming samples and write them out in H.264; no other file formats seem to be supported in the current iOS release) - attach an
AVAssetWriterInput
to the asset writer to provide video frames (as an asset writer takes one or more inputs, each to provide different aspects of the asset); - attach an
AVAssetWriterInputPixelBufferAdaptor
to the asset writer input so that you can push pixel buffers at the input. You'll want to tell the asset writer input that it is to expect media data in real time. Doing so tells it just to try to be always ready for input. You get to specify frame timings as and when you supply frames, so it doesn't put a performance requirement on the rest of your code.
From there, you just need to push CVImageBuffers to your pixel buffer adaptor. There's a convenient Core Video OpenGL buffer type in OS X, but it seems to be absent on iOS. Instead you'll need to glReadPixels
alongside CVPixelBufferCreate
, etc, locking the pixel buffer and writing directly to its base address. Hopefully you'll get something you can pass directly to OpenGL, without needing to shuffle bytes in between.
Supposing the iPhone has a dedicated H.264 encoder, the main performance bottleneck is likely to be the glReadPixels, causing whatever is in the frame buffer to need to be transferred and reformatted for the CPU. If you set it all up and results seem slow, I'd investigate there first.
精彩评论