[英]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.