def video_wirte_test2():
cap = cv2.VideoCapture("./daoyou.mp4")
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# wr = cv2.VideoWriter("test.mp4",cv2.VideoWriter.fourcc(*'mp4v'),30,(640,480))
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter.fourcc(*'mp4v')
# out = cv2.VideoWriter("output_video.mp4",fourcc ,fps,(width, height))
wr = cv2.VideoWriter("test.mp4",fourcc,30,(1070, height))
idx = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 写入帧到输出视频
wr.write(frame)
# 显示帧(可选)
cv2.imshow('frame', frame)
key = cv2.waitKey(1000 // 30)
cap.release()
wr.release()
cv2.destroyAllWindows()
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter.fourcc(*'mp4v') 通过视频获取的视频的宽高,可以正常复制 out = cv2.VideoWriter("output_video.mp4",fourcc ,fps,(width, height))
自己设置的高度,不能正常复制 wr = cv2.VideoWriter("test.mp4",fourcc,30,(1070, height))
总结,复制视频时需要获取宽高,自己设置的会导致视频复制不了
如果需要改变大小需要resize
需要注意的是在 VideoWriter 中的尺寸要和 write() 中写进去的一样,也就是resize()函数的尺寸大小,不然视频会存储失败。