Python -OpenCV VideOwriter输出无效的视频和不同的文件大小
我只是试图使用OpenCV视频捕获阅读视频,然后使用视频作者输出相同的视频。但是结果视频无法播放,每次我运行该功能时,尽管具有相同的输入视频,输出视频的文件大小不同。
cap = cv2.VideoCapture("video_input.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
out = cv2.VideoWriter("video_output.mp4",cv2.VideoWriter_fourcc(*'mp4v'), fps, (int(width), int(height)))
count_frame = 0
if (cap.isOpened()== False):
print("Error opening video stream or file")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
out.write(frame)
count_frame = count_frame + 1
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
注意:输入视频具有8000帧,如果我使用条件仅编写第一个2000帧,则视频作者在没有问题的视频中输出了2000帧。
有人知道我的问题吗?这个精确的代码在几周前可以正常工作。
编辑:我还想补充一点,我正在Jupyterlab上的虚拟机上运行此代码。但是,当我尝试在其他虚拟机中运行时,它运行得很好。 OPENCV版本在两个VM中都是相同的
I am simply trying to read a video using openCV Video Capture, and then outputting that same video using Video Writer. But the resultant video is not playable and each time I run the function, although having the same input video the output video has different file sizes.
cap = cv2.VideoCapture("video_input.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
out = cv2.VideoWriter("video_output.mp4",cv2.VideoWriter_fourcc(*'mp4v'), fps, (int(width), int(height)))
count_frame = 0
if (cap.isOpened()== False):
print("Error opening video stream or file")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
out.write(frame)
count_frame = count_frame + 1
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Note: The input video has 8000 frames, if i use a condition to only write the first 2000 frames the video writer outputs those 2000 frames in a video without problem.
Does anyone know what is my problem? This exact code use to work fine some weeks ago.
Edit: I would also like to add that I´m running this code on a virtual machine on jupyterLab. But when i try to run in other virtual machine, it runs perfectly fine. The openCV versions are the same in both VM's
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论