OpenCV - using VideoWriter gives no file
Question: I have searched for a solution of my problem on many sites (including stack
overflow). I am trying to save a video from my webcam in my Raspberry using
OpenCV. Theoretically, my code works fine (I can see my webcam in the window,
i can see python printing "frame"), but when it goes to saving the file I
cannot see anything. I have found that I should change codecs in FourCC, but
it changes nothing.

My code:

    
    
    #!/usr/bin/python
    
    import cv2
    import numpy as np
    
    def InitCamera():
    
        cap = cv2.VideoCapture(0)
        if cap is None:
            print('No camera access')
        else:
            print('Camera init done')
        return cap
    
    def InitWriter():
        fps = 20
        size = (640,480)
        outFile = 'output.mp4'
    
        #fourcc = cv2.cv.CV_FOURCC('D','I','V','X')
        #fourcc = cv2.cv.CV_FOURCC('R','G','B',' ')
        #fourcc = cv2.cv.CV_FOURCC('Y','U','Y','2')
        #fourcc = cv2.cv.CV_FOURCC('Y','U','Y','U')
        #fourcc = cv2.cv.CV_FOURCC('U','Y','V','Y')
        #fourcc = cv2.cv.CV_FOURCC('I','4','2','0')
        #fourcc = cv2.cv.CV_FOURCC('I','Y','U','V')
        #fourcc = cv2.cv.CV_FOURCC('Y','U','1','2')
        #fourcc = cv2.cv.CV_FOURCC('Y','8','0','0')
        #fourcc = cv2.cv.CV_FOURCC('G','R','E','Y')
        #fourcc = cv2.cv.CV_FOURCC('B','Y','8',' ')
        #fourcc = cv2.cv.CV_FOURCC('Y','1','6',' ')
        #fourcc = cv2.cv.CV_FOURCC('X','V','I','D')
        #fourcc = cv2.cv.CV_FOURCC('M','J','P','G')
        fourcc = cv2.cv.CV_FOURCC('M','P','E','G')
    
        out = cv2.VideoWriter(outFile, fourcc, fps, size, 1)
        if out is None:
            print('No video access')
        else:
            print('Video init done')
        return out
    
    def CapVideo(cap,out):
        while(cap.isOpened()):
            ret, frame = cap.read()
            if ret==True:
                out.write(frame)
                print('frame')
                cv2.imshow('frame',frame)
                if cv2.waitKey(1) & 0xFF==ord('q'):
                    break
    
            else:
                break
        print 'Done'
    
        cap.release()
        out.release()
        cv2.destroyAllWindows()
    
    if __name__ == '__main__':
        cam = InitCamera()
        out = InitWriter()
        CapVideo(cam,out)
    


Answer: Do you have a strong reason to write mp4 files? Consider writing to file with
an avi extension. I have struggled with this problem a lot as well and in
cases where I strongly needed an MP4 file, i ended up using ffmpeg to do the
conversion post writing the file as an avi by openCV.

I was developing on C++ and simply made a system call. Something like this:

    
    
    std::string ffmpeg_conversion_cmd = "ffmpeg -y -i data\\intermediate_result.avi -c:v libx264 -crf 19 -preset slow -c:a libfaac -b:a 192k -ac 2 data\\intermediate_result.mp4";
    std::system(ffmpeg_conversion_cmd.c_str());
    

Of course, you need to have ffmpeg installed on your Pi to do this.

