An example using VideoCapture and VideoWriter class
#include <iostream>
#include <stdio.h>
int main(int, char**)
{
    
    
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    
    cap >> src;
    
    if (src.empty()) {
        cerr << "ERROR! blank frame grabbed\n";
        return -1;
    }
    bool isColor = (src.type() == 
CV_8UC3);
     
    double fps = 25.0;                          
    string filename = "./live.avi";             
    writer.
open(filename, codec, fps, src.size(), isColor);
    
        cerr << "Could not open the output video file for write\n";
        return -1;
    }
    
    cout << "Writing videofile: " << filename << endl
         << "Press any key to terminate" << endl;
    for (;;)
    {
        
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        
        
            break;
    }
    
    return 0;
}