OpenCV视频解码图片
作者:互联网
1 视频解码图片
import cv2
#根据需要修改路径
cap = cv2.VideoCapture("D:/test/test.mp4")
i = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# frame 保存下来
filename = "{}.png".format(i)
# 保存的路径根据需要进行修改
cv2.imwrite("imgs/"+filename, frame)
i += 1
cap.release()
2 视频转视频
import cv2
cap = cv2.VideoCapture("D:/test/test.mp4")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter("test_out.mp4", fourcc, fps, (width, height), True)
i = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 根据需要截取视频帧
if i < 100:
out.write(frame)
i += 1
cap.release()
out.release()
标签:视频,解码,cap,cv2,ret,OpenCV,test,frame,out 来源: https://www.cnblogs.com/LuckCoder/p/16420698.html