簡體   English   中英

保存使用 opencv 錄制的視頻時出錯

[英]Error when saving video recorded with opencv

我正在使用openCV錄制,起初錄制順利,但我希望當我按下空格鍵時,錄制的所有內容都保存在設備上(這是損壞的地方)。 我收到以下錯誤:

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

然后 window 凍結。

這是代碼:

def record_video():

     capture = cv2.VideoCapture(0)

     while capture.isOpened():

     res, video = capture.read()
     cv2.imshow("Recording Window", video)

     if cv2.waitKey(1) == 27: #ESC key to stop recording
         break
    
     elif cv2.waitKey(1) == 32: #SPACE key to start recording and save it to the device

         width = int(capture.get (cv2.CAP_PROP_FRAME_WIDTH))
         height = int(catch.get (cv2.CAP_PROP_FRAME_HEIGHT))

         code = cv2.VideoWriter_fourcc (* 'MPEG') #format
         recorder = cv2.VideoWriter("video.mp4",code,20,(width, height))

         while True:
             result, video_record = capture.read()
             recorder.write(video_record)
        
             if cv2.waitKey(1) & 0xFF == ord("q"): #q key to stop recording
                 break

         recorder.release()
         capture.release()
         cv2.destroyAllWindows()
        
    capture.release()
    cv2.destroyAllWindows()

創建.mp4視頻,需要初始化為*mp4v

code = cv2.VideoWriter_fourcc(*'mp4v') #format

但是如果上面的初始化對你不起作用,那么你需要用opencv ffmpeg 其他選項是使用Motion-JPEG視頻格式,與.mp4相比質量要低得多

code = cv2.VideoWriter_fourcc(*'MJPG') #format
recorder = cv2.VideoWriter("video.avi",code,20,(width, height))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM