gpt4 book ai didi

python - OpenCV MP4 创建

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:31 27 4
gpt4 key购买 nike

我一直在尝试使用 OpenCV 在 python 中编写 MP4 视频文件。

当我同时使用 linux 和 windows 时,AVI 创建工作正常:

out = cv2.VideoWriter('x.avi', 0, 30, (640, 480))

fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter('x.avi', fourcc, 30, (640, 480))

甚至

fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter('x', fourcc, 30, (640, 480))

.

当我尝试保存 MP4 但没有任何保存 - 使用:

fourcc = cv2.VideoWriter_fourcc(*"H264")
out = cv2.VideoWriter('x.mp4', fourcc, 30, (640, 480))

fourcc = cv2.VideoWriter_fourcc(*"AVC1")
out = cv2.VideoWriter('x.mp4', fourcc, 30, (640, 480))

没有错误发生,只是没有保存。

在过去的几天里,我尝试了所有方法,竭尽全力避免创建 AVI,然后使用 ffmpeg 将其转换为 MP4,因为我发现这是一种可怕的做法。

最佳答案

cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)

fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))

while(True):
ret, frame = cap.read()
out.write(frame)
cv2.imshow('frame', frame)
c = cv2.waitKey(1)
if c & 0xFF == ord('q'):
break

cap.release()
out.release()
cv2.destroyAllWindows()

关于python - OpenCV MP4 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38804931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com