Save OpenGL Rendering Result As A Video

  • Read OpenGL framebuffer
  • Close the video file
  • Create a Video File to Write
cv::VideoWriter outputVideo;
outputVideo.open( "video.avi", -1, 20.0f, cv::Size( width, height ), true);
cv::Mat pixels( height, width, CV_8UC3 );
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels.data );
cv::Mat cv_pixels( height, width, CV_8UC3 );
for( int y=0; y<height; y++ ) for( int x=0; x<width; x++ )
{
cv_pixels.at<cv::Vec3b>(y,x)[2] = pixels.at<cv::Vec3b>(height-y-1,x)[0];
cv_pixels.at<cv::Vec3b>(y,x)[1] = pixels.at<cv::Vec3b>(height-y-1,x)[1];
cv_pixels.at<cv::Vec3b>(y,x)[0] = pixels.at<cv::Vec3b>(height-y-1,x)[2];
}
outputVideo << cv_pixels;
outputVideo.release();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store