简体   繁体   English

OpenCV 未正确写入向视频添加帧

[英]OpenCV Not Properly Writing Adding Frames to Video

I'm using mss, numpy, and OpenCV to make my own recording software, but when I record, all of my videos end up with a 258 bytes file size (no matter the length of the video), and I can't view the video.我正在使用 mss、numpy 和 OpenCV 制作我自己的录制软件,但是当我录制时,我所有的视频最终都是 258 字节的文件大小(无论视频的长度),我无法查看视频。 No frames seem to be in my videos.我的视频中似乎没有帧​​。 What am I doing wrong?我究竟做错了什么?

Here's my code, any help would be greatly appreciated:这是我的代码,任何帮助将不胜感激:

import datetime
import numpy as np
import cv2
import mss
import mss.tools


time_stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
fourcc = cv2.VideoWriter_fourcc("m", "p", "4", "v")
captured_video = cv2.VideoWriter(f"{time_stamp}.mp4", fourcc, 20.0, (1080, 720))

with mss.mss() as sct:
    monitor = {"top": 0, "left": 0, "width": 1080, "height": 720}
    while True:
        img = np.array(sct.grab(monitor))
        img_final0 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img_final = cv2.cvtColor(img_final0, cv2.COLOR_BGR2RGB)
        cv2.imshow('LevelRecorder', img_final)
        capture = captured_video.write(img_final)
        print(capture)
        if cv2.waitKey(10) == ord("q"):
            break

The "258 bytes" file size is useful information. “258 字节”文件大小是有用的信息。 Since your files are tiny, VideoWriter actually doesn't even write any video data.由于您的文件很小,VideoWriter 实际上甚至不写入任何视频数据。

  • Make sure the frames you write() are sized 1080 by 720 and 3-channel (shape (720, 1080, 3) ), as you announced in the constructor of the VideoWriter object.确保write()的帧大小为 1080 x 720 和 3 通道(形状(720, 1080, 3) ),正如您在 VideoWriter 对象的构造函数中宣布的那样。
  • You are missing a captured_video.release() call.您缺少captured_video.release()调用。 MP4 files will be corrupted if they aren't properly finalized, even if video data actually makes it into the file.如果 MP4 文件没有正确完成,即使视频数据实际进入文件,它们也会损坏。
  • Those cvtColor operations make no sense.那些cvtColor操作毫无意义。 You simply convert back and forth.您只需来回转换。
  • Look at the Video I/O section in print(cv.getBuildInformation()) and confirm that you have ffmpeg support.查看print(cv.getBuildInformation())中的 Video I/O 部分并确认您有 ffmpeg 支持。 Without that, mp4v video and .mp4 container will not be available.没有它, mp4v视频和.mp4容器将不可用。

When in doubt, use MJPG fourcc and .avi container.如有疑问,请使用MJPG.avi容器。 Those two are built into OpenCV, always available.这两个内置在 OpenCV 中,始终可用。

At last, I finally found my answer.终于,我终于找到了我的答案。 Turns out, this problem had a super simple solution that I somehow missed this entire time.事实证明,这个问题有一个超级简单的解决方案,我不知何故一直错过了这一点。

As it turns out, either NumPy or Open Cv (I don't know which of the two), really really hates mss, but really likes Python Pillow, so, all you have to do is convert it to an image like Python Pillow, and there you go!事实证明,无论是 NumPy 还是 Open Cv(我不知道两者中的哪一个),真的讨厌 mss,但真的很喜欢 Python Pillow,所以,你所要做的就是将其转换为 Python Pillow 之类的图像,你去吧! Here's my new code (which works for me)这是我的新代码(对我有用)

import datetime
from PIL import Image
import numpy as np
import cv2
from mss import mss


time_stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
fourcc = cv2.VideoWriter_fourcc("m", "p", "4", "v")
captured_video = cv2.VideoWriter(f"{time_stamp}.mp4", fourcc, 20.0, (2880, 1800), 3)
sct = mss()

while True:
    img0 = sct.grab(sct.monitors[1])
    img_PIL = Image.frombytes('RGB', img0.size, img0.bgra, 'raw', 'BGRX') #This is where we convert it to a Pillow-like image
    img1= np.array(img_PIL) #Sorry about my variables, I was too lazy to change them :P
    img = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
    cv2.imshow('LevelRecorder', img)
    captured_video.write(img)
    if cv2.waitKey(1) == ord("q"):
        cv2.destroyAllWindows()
        break

So there it is!就是这样! Just use Python Pillow to convert each frame into something NumPy and Open Cv like using Image.frombytes().只需使用 Python Pillow 将每一帧转换为 NumPy 和 Open Cv,就像使用 Image.frombytes() 一样。 Thank you so much Christoph Rackwitz for helping , I can not bring forth into words how much of a great help you have been :)非常感谢 Christoph Rackwitz 的帮助,我无法用言语表达你对我的帮助有多大 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM