Search code examples
c++open-sourcempeg

C library to build video directly from buffers on Linux, preferably portable to Windows and Mac OS X


I have a program that creates frame buffers in OpenGL. I can save out the buffers as .png or as .jpg files, and I am familiar with some utilities on Windows (like http://www.radgametools.com/bnkmain.htm) that will convert multiple image files to .mpg

But I would like an open source library to call with my buffer already in RAM. Does such an API exist?


Solution

  • Since you are looking for a library, I suggest taking a look at OpenCV (it's cross-platform library supported on Windows/Linux/Mac).

    I've written the code below some time ago. It loads two JPG images from the current directory and creates a video file with it. I believe it's more than enough to get you started.

    #include <cv.h>
    #include <highgui.h>
    
    int main()
    {
        IplImage* img1 = cvLoadImage("img1.jpg", CV_LOAD_IMAGE_UNCHANGED);
        IplImage* img2 = cvLoadImage("img2.jpg", CV_LOAD_IMAGE_UNCHANGED);
    
        float fps = 20;
        CvVideoWriter* writer = cvCreateVideoWriter("out.avi", CV_FOURCC('M','J','P','G'), fps, cvGetSize(img1), true);
        if (!writer)
        {
          fprintf (stderr, "VideoWriter failed!\n");
          return -1;
        }
    
        cvWriteFrame(writer, img1);
        cvWriteFrame(writer, img2);
    
        cvReleaseVideoWriter(&writer);
        cvReleaseImage(&img1);
        cvReleaseImage(&img2);
    }