Fourcc:
https
1 读取视频:
# python read video
# python read video
import cv2
import numpy as np
cap = cv2.VideoCapture(0) # 视频编解码器
id = 0
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
frame_h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_h)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_w)
print(f"fcrame_h: {frame_h}, frame_w: {frame_h}, frame_count: {frame_count}")
cv2.namedWindow("frame", cv2.WINDOW_NORMAL)
show_h, show_w = 320, 640
cv2.resizeWindow("frame", show_w, show_h)
while True:
id += 1
ret, frame = cap.read()
if ret == False:
print(f"video has breaken.")
mask = frame[:, :, 0]
# Get the timestamp of the current frame
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC)
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if id == frame_count:
print(f"finish.")
break
2 保存视频
import cv2
import numpy as np
width, height = 1920,1080
# Define the codec and create a VideoWriter object
# *'MP4V', mp4
# *'XVID', avi
write_fps = 20.0
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('res_wan.mp4', fourcc, write_fps , (width, height))
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out = cv2.VideoWriter('res_wan.avi', fourcc, write_fps , (width, height))
test_num = 500
while test_num:
test_num -= 1
test_img = np.zeros((frame_h, frame_w, 3))
out.write(test_img)
```