OpenCV - using VideoWriter gives no file
<p>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.</p>

<p>My code:</p>

<pre><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) &amp; 0xFF==ord('q'):
                break

        else:
            break
    print 'Done'

    cap.release()
    out.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    cam = InitCamera()
    out = InitWriter()
    CapVideo(cam,out)
</code></pre>