简体   繁体   English

保存使用 opencv 录制的视频时出错

[英]Error when saving video recorded with opencv

I am recording with openCV , at first the recording goes well, but I want that when I hit the spacebar, everything that is recorded then is saved on the device (this is where it gets damaged).我正在使用openCV录制,起初录制顺利,但我希望当我按下空格键时,录制的所有内容都保存在设备上(这是损坏的地方)。 I get the following errors:我收到以下错误:

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' 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'

and then the window freezes.然后 window 冻结。

this is the code:这是代码:

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()

For.mp4 video creation, you need to initialize as *mp4v创建.mp4视频,需要初始化为*mp4v

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

But if the above initialization won't work for you, then you need to compile ffmpeg with opencv .但是如果上面的初始化对你不起作用,那么你需要用opencv ffmpeg Other option is using Motion-JPEG video format, which is much lower quality compared to .mp4其他选项是使用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