2017-02-17 56 views
0

Python 3.5.2,Windows 10上的anaconda 4.2.0。 OpenCV從conda版本3.1.0開始安裝。opencv和python 3 - 無法將視頻幀寫入磁盤

我試圖通過打開它來處理視頻文件,轉換每個幀,並將結果放入一個新的視頻文件。輸出文件是創建的,但大小約爲800字節,其空。輸入文件有大約4000幀,大約150 MB。

這裏是我的代碼,它非常接近OpenCV文檔的指南。

import cv2 
import progressbar 
# preprocess video 
# args.input is a valid file name  
    outname = 'foo.mp4' 
    cap = cv2.VideoCapture(args.input)  

    codec = int(cap.get(cv2.CAP_PROP_FOURCC)) 
    framerate = app_config.camera.framerate #240 
    size = (app_config.camera.width, app_config.camera.height) #1080 x 720 

    vw = cv2.VideoWriter(filename=outname, fourcc=codec, fps=framerate, frameSize=size, isColor=False) 
    curframe = 0 
    with progressbar.ProgressBar(min_value=0, max_value=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))) as pb: 
     while cap.isOpened(): 
      ret, frame = cap.read() 
      if ret: 
       #update the progress bar 
       curframe += 1 
       pb.update(curframe) 
       # convert to greyscale 
       grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
       # invert colors 
       inverted = cv2.bitwise_not(grey) 
       vw.write(inverted) 

       #cv2.imshow('right', right) 
       #if cv2.waitKey(1) & 0xFF == ord('q'): 
       # break 
      else: 
       break 

     cap.release() 
     vw.release() 
     cv2.destroyAllWindows() 

我收到以下錯誤:

OpenCV: FFMPEG: tag 0x7634706d/'mp4v' is not supported with codec id 13 and format 'mp4/MP4 (MPEG-4 Part 14)' 
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???' 

我收到類似的錯誤(以及我對H.264庫路徑不正確的環境變量警告),如果我嘗試設置codec = cv2.VideoWriter_fourcc(*'H264')

+0

當你輸入「cv2 .__ version__」時,你會得到什麼,只是爲了確保你有正確的opencv加載。另外,你確定「foo.mp4」的高度和高度是1080 * 720嗎?如果沒有,它會寫入一個空的視頻文件。如果這些不起作用,請嘗試執行「fourcc = cv2.VideoWriter_fourcc('m','p','4','v')」,「video = cv2.VideoWriter()」,「video.open(outname, fourcc,framerate,size,False。希望這有助於! – crazjo

回答

0

確保inverted的尺寸與videoWriter定義中size參數的尺寸相匹配。

還使用'M','P','4','V'編解碼器與.mp4容器。