1

I'm not able to write an mp4 video file with cv2 on Rpi4. All I'm getting in feedback is VIDIOC_DQBUF: Invalid argument

writer = cv2.VideoWriter('test.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (640, 480), True)
stream = cv2.VideoCapture(0)
ret, frame = stream.read()
while ret:
    writer.write(frame)
    cv2.imshow('Video', frame)
    ret, frame = stream.read()
    if cv2.waitKey(1) & 0xFF==27:
        break

stream.release()
writer.release()
cv2.destroyAllWindows()

The video is displaying using cv2.imshow(frame), and the file is outputted, however no frames are actually written to it, so the video file appears corrupted.

I am assuming this is a codec error. I've tried displaying the codecs using fourcc=-1 in VideoWriter() though the other fourcc's I've tried didn't work either. Has anyone had success using opencv writing videos on rpi4?

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34

1 Answers1

0

I've tested your code and it worked well on my Raspberry Pi 4. I'm using the latest Raspberry Pi OS and OpenCV 4.3.0. I can also use avi codec:

out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), 30.0, (640,480))

If you cannot use both of them, try to make some updates for your rpi4.

yushulx
  • 11,695
  • 8
  • 37
  • 64