在不保存 opencv 的情况下编写和读取视频
Writing and Reading video without saving in opencv
我想在用 cv2.VideoWriter
编写视频后不保存视频来阅读。
例如:
video = cv2.VideoWriter('using.mp4', cv2.VideoWriter_fourcc(*'MJPG'), 10, size)
现在,写完这个 cv2.VideoWriter
对象后,是否可以像 video.read()
那样读取它,但是由于 read()
是 cv2.VideoCapture
的函数,它会抛出一个错误
Exception has occurred: AttributeError
'cv2.VideoWriter' object has no attribute 'read'
那么,有没有可能阅读 cv2.VideoWriter
的方法?
从视频编写器读取帧的另一种方法是将帧保存在列表中,而不是将每个帧保存在循环中。完成后,您可以将它们写在循环外并保存为 video.read()
video = cv2.VideoWriter('using.mp4', cv2.VideoWriter_fourcc(*'MJPG'), 10, size)
for frame in frames:
writer.write(frame)
for frame in frames:
# do other stuff here
详细示例(注意我更改了 fourcc - 你的示例对我不起作用)
import cv2
def cam_test(port: int = 0) -> None:
frames = []
cap = cv2.VideoCapture(port)
if not cap.isOpened(): # Check if the web cam is opened correctly
print("failed to open cam")
else:
print('cam opened on port {}'.format(port))
for i in range(10 ** 10):
success, cv_frame = cap.read()
if not success:
print('failed to capture frame on iter {}'.format(i))
break
frames.append(cv_frame)
cv2.imshow('Input', cv_frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Now you have the frames at hand
if len(frames) > 0:
# if you want to write them
size = (frames[0].shape[1], frames[0].shape[0])
video = cv2.VideoWriter(
filename='using.mp4',
fourcc=cv2.VideoWriter_fourcc(c1='m', c2='p', c3='4', c4='v'),
fps=10,
frameSize=size
)
for frame in frames:
video.write(frame)
# and to answer your question, you wanted to do video.read() which would have gave you frame by frame
for frame in frames:
pass # each iteration is like video.read() if video.read() was possible
return
if __name__ == '__main__':
cam_test()
我想在用 cv2.VideoWriter
编写视频后不保存视频来阅读。
例如:
video = cv2.VideoWriter('using.mp4', cv2.VideoWriter_fourcc(*'MJPG'), 10, size)
现在,写完这个 cv2.VideoWriter
对象后,是否可以像 video.read()
那样读取它,但是由于 read()
是 cv2.VideoCapture
的函数,它会抛出一个错误
Exception has occurred: AttributeError
'cv2.VideoWriter' object has no attribute 'read'
那么,有没有可能阅读 cv2.VideoWriter
的方法?
从视频编写器读取帧的另一种方法是将帧保存在列表中,而不是将每个帧保存在循环中。完成后,您可以将它们写在循环外并保存为 video.read()
video = cv2.VideoWriter('using.mp4', cv2.VideoWriter_fourcc(*'MJPG'), 10, size)
for frame in frames:
writer.write(frame)
for frame in frames:
# do other stuff here
详细示例(注意我更改了 fourcc - 你的示例对我不起作用)
import cv2
def cam_test(port: int = 0) -> None:
frames = []
cap = cv2.VideoCapture(port)
if not cap.isOpened(): # Check if the web cam is opened correctly
print("failed to open cam")
else:
print('cam opened on port {}'.format(port))
for i in range(10 ** 10):
success, cv_frame = cap.read()
if not success:
print('failed to capture frame on iter {}'.format(i))
break
frames.append(cv_frame)
cv2.imshow('Input', cv_frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Now you have the frames at hand
if len(frames) > 0:
# if you want to write them
size = (frames[0].shape[1], frames[0].shape[0])
video = cv2.VideoWriter(
filename='using.mp4',
fourcc=cv2.VideoWriter_fourcc(c1='m', c2='p', c3='4', c4='v'),
fps=10,
frameSize=size
)
for frame in frames:
video.write(frame)
# and to answer your question, you wanted to do video.read() which would have gave you frame by frame
for frame in frames:
pass # each iteration is like video.read() if video.read() was possible
return
if __name__ == '__main__':
cam_test()