[英]Can't save video on Raspberry PI by using python with OpenCV
[英]Can't save a video in opencv
我正在嘗試使用 opencv 寫入方法保存我的視頻,但視頻以 0 kb 保存。 我的代碼有什么問題。
import cv2
cap = cv2.VideoCapture("k1.mp4")
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
fourcc = cv2.VideoWriter_fourcc(*'MP42')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))
while cap.isOpened():
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
fgmask = fgbg.apply(gray)
thresh = 2
maxValue = 255
ret, th1 = cv2.threshold(fgmask, thresh, maxValue, cv2.THRESH_BINARY)
color_space = cv2.applyColorMap(th1, cv2.COLORMAP_JET)
result_vid = cv2.addWeighted(frame, 0.7, color_space, 0.7, 0)
cv2.imshow("vid", result_vid)
out.write(result_vid)
if cv2.waitKey(20) == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
import cv2
cap = cv2.VideoCapture(0)
# Automatically grab width and height from video feed
# (returns float which we need to convert to integer for later on!)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# MACOS AND LINUX: *'XVID' (MacOS users may want to try VIDX as well just in case)
# WINDOWS *'VIDX'
writer = cv2.VideoWriter('local_capture.mp4', cv2.VideoWriter_fourcc(*'VIDX'),25, (width, height))
# This loop keeps recording until you hit Q or escape the window
# You may want to instead use some sort of timer, like from time import sleep and then just record for 5 seconds.
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Write the video
writer.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
# This command let's us quit with the "q" button on a keyboard.
# Simply pressing X on the window won't work!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
writer.release()
cv2.destroyAllWindows()
問題是視頻編解碼器和視頻容器格式不匹配。
執行代碼時,我收到一條錯誤消息(在控制台窗口中):
OpenCV: FFMPEG: tag 0x3234504d/'MP42' 不支持編解碼器 ID 15 和格式 'mp4 / MP4 (MPEG-4 Part 14)'
[mp4 @ 00000155e95dcec0] 在流 #0 中找不到編解碼器 msmpeg4v2 的標簽,容器當前不支持編解碼器
fourcc = cv2.VideoWriter_fourcc(*'MP42')
,並且M420
應用視頻編解碼器MPEG-4v2 。'output.mp4'
。.mp4
擴展名應用 MP4 容器格式。 顯然.mp4
視頻文件不能包含使用MPEG-4v2
編解碼器編碼的視頻。
您可以更改編解碼器或更改文件格式。
例子:
'output.avi'
或'output.wmv'
有效。MPEG-4
: fourcc = cv2.VideoWriter_fourcc(*'mp4v')
(並保留文件名'output.mp4'
)也有效。還有一個問題:
在ret, frame = cap.read()
之后添加以下代碼:
if not ret:
break;
Although your solution was to match the video codec and the video container format correctly, I wanted to add that another common reason the output file size is 0 kb when writing videos with OpenCV is a discrepancy between the captured video frame size and the output video frame尺寸。 這可以通過將硬編碼的 output 幀大小替換為從輸入視頻計算得出的幀大小來解決。
cap = cv2.VideoCapture('input.mp4')
w = int(cap.get(3))
h = int(cap.get(4))
frameSize = (w, h)
fps = 20
fourCC = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('output.mp4', fourCC, fps, frameSize)
while(True):
ret, frame = cap.read()
...
result_vid = ...
...
out.write(result_vid)
...
cap.release()
out.release()
cv2.destroyAllWindows()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.