首先說一下報(bào)錯的地方,是在使用VideoWriter保存視頻時:
'''
opencv讀取攝像頭視頻流,并且顯示
'''
import cv2
import numpy as np
#調(diào)用攝像頭
cap = cv2.VideoCapture(0)
#DIVX,X264
fourcc = cv2.VideoWriter_fourcc(*'X264')
fps = 20
#獲取圖像的高寬
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
writer = cv2.VideoWriter('video.mp4',fourcc,fps,(width,height))
while True:
#讀取視頻幀
rec, frame = cap.read()
#鏡像
frame = cv2.flip(frame,1)
#灰度圖片
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#保存視頻
writer.write(frame)
#顯示圖片
cv2.imshow('demo',frame)
#退出條件
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
writer.release()
cv2.destroyAllWindows()
出現(xiàn)如下錯誤:
OpenCV: FFMPEG: tag 0x34363258/'X264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'
[ERROR:0@0.234] global cap_ffmpeg_impl.hpp:2991 open Could not find encoder for codec_id=27, error: Encoder not found
[ERROR:0@0.234] global cap_ffmpeg_impl.hpp:3066 open VIDEOIO/FFMPEG: Failed to initialize VideoWriter
經(jīng)過查找網(wǎng)上資料,發(fā)現(xiàn)是cv2.VideoWriter_fourcc()參數(shù)存在問題,
解決方法:
將
fourcc = cv2.VideoWriter_fourcc(*'X264')
修改為:文章來源:http://www.zghlxwxcb.cn/news/detail-732592.html
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
即可完美解決問題。文章來源地址http://www.zghlxwxcb.cn/news/detail-732592.html
到了這里,關(guān)于OpenCV 報(bào)錯:FFMPEG: tag 0x34363258/‘X264‘ is not supported with codec id 27 and format ‘mp4 / MP4‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!