title: Specify Compression Quality in Python for OpenCV Video Object
body: <p>I must create low resolution AVI videos using a large number of jpeg images contained in individual directories. I have nearly one hundred directories, and each directory may contain many thousands of images. </p>
  
  <p>In order to automate this process, I have written a python script using OpenCV to create a video object, load each image from a given directory, and write each image to a specific video file for that directory. All this works great. My problem is how to control the compression quality for the video object. </p>
  
  <p>The VideoWriter module accepts 5 parameters. The second parameter, 'fourcc', sets the compression code. </p>
  
  <blockquote>
    <p>cv2.VideoWriter.open(filename, fourcc, fps, frameSize[, isColor]) </p>
  </blockquote>
  
  <p>We can specify a compression code within cv2.VideoWriter using a "four character code" (i.e. fourcc). </p>
  
  <blockquote>
    <p>fourcc = cv2.cv.CV_FOURCC('M','S','V','C') #Microspoft Video 1</p>
  </blockquote>
  
  <p>This approach works except the compression quality of the video object is always set at it's maximum.    </p>
  
  <p>If we let fourcc = -1, a Windows Video Compression dialog box opens which allows the user to select a video compression AND set Compression Quality (or Temporal Quality Ratio) between 0 and 100.  </p>
  
  <p><a href="https://i.stack.imgur.com/FHcL1.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FHcL1.png" alt="screenshot of this Windows Video Compression dialog box"></a></p>
  
  <p>Each video must be an AVI and must meet certain file size requirements. The video file is too big if the maximum compression quality is used. However, requiring the user to select the video compression and compression quality for each video defeats the script's automation.  </p>
  
  <p>So, how does one specify a the compression quality for the video object without using the Windows Video Compression dialog box?</p>
  
  <p>The entirety of my code is posted below</p>
  
  <pre><code>import cv2, os
  
  Workspace = "J:\jpg to AVI test"
  year = "2014"
  file_extension = "avi"
  Image_file_dir = Workspace + "\\12 - ECD-BONNETT CREEK Y INT (" + year + ")"
  print Image_file_dir
  Image_file_list = os.listdir(Image_file_dir)
  print "Image_file_list: " + str(Image_file_list)
  
  img_cnt = 0
  
  for Image_file in Image_file_list:
  
      if Image_file.split(".")[-1] == "jpg":
  
          Image_file_path = Image_file_dir + "\\" + Image_file
          print Image_file_path
  
          img1 = cv2.imread(Image_file_path)
  
          height , width , layers =  img1.shape
  
          break
  
  fourcc = cv2.cv.CV_FOURCC('M','S','V','C') #Microspoft Video 1
  ##fourcc = -1
  video_object = Workspace + "\\12 - ECD-BONNETT CREEK Y INT (" + year + ")." + file_extension
  video = cv2.VideoWriter(video_object,\
                          fourcc,\
                          9,\
                          (width,height))
  
  for Image_file in Image_file_list:
  
      if Image_file.split(".")[-1] == "jpg":
  
          img_cnt += 1
          Image_file_path = Image_file_dir + "\\" + Image_file
          print str(img_cnt) + ") " + Image_file_path
  
          img1 = cv2.imread(Image_file_path)
  
          video.write(img1)
  
  cv2.destroyAllWindows() 
  video.release()
  </code></pre>