Python -OpenCV VideOwriter输出无效的视频和不同的文件大小

发布于 2025-01-23 05:55:52 字数 884 浏览 1 评论 0原文

我只是试图使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文