title: Saving video using OpenCV
body: <p>Given below is the Python script I am using for accessing an IP Camera and saving the incoming video stream:</p>
  
  <pre><code>import cv2
  from cv2 import cv
  writer=cv2.VideoWriter("vid.avi",cv.CV_FOURCC('X', '2', '6', '4'), 24, (320,240))
  def cvloop():    
      stream=urllib.urlopen('URL of the camera')
      bytes=''
      while True:
          bytes+=stream.read(1024)
          a = bytes.find('\xff\xd8')
          b = bytes.find('\xff\xd9')
          if a!=-1 and b!=-1:
              jpg = bytes[a:b+2]
              bytes= bytes[b+2:]
              i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)            
              tki = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(i, cv2.COLOR_BGR2RGB)))
              cv2.imshow('i',i)
              writer.write(i)
              if cv2.waitKey(1) ==27:
                  exit(0)
  </code></pre>
  
  <p>Now, if you want to know why I am parsing the jpg images from the stream the answer can be found <a href="https://stackoverflow.com/questions/21702477/how-to-parse-mjpeg-http-stream-from-ip-camera">here</a>. The answer explains everything.</p>
  
  <p>As for the video writer script, I have referred OpenCV's documentation which can be found <a href="http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Now instead of using the default list of codecs I am using H-264 but I have tried all the codecs installed on my system.</p>
  
  <p><strong>Question:</strong> Now the problem is on executing the program the video file is being made, on running it I see nothing. Also, on using the software <code>GSpot</code> to analyse the video, I am getting the number of frames in the video. Same is the case with <code>FFMPEG</code>. Where am I going wrong?</p>
  
  <pre><code>Camera: Axis IP Camera M1103
  OS: Windows 7 32bit
  </code></pre>