Skip to content
Advertisement

cv2 creates a unreadable .mp4v (and .avi) file

I’m trying to create a simple screen recorder with Python. This is the code

JavaScript

It doesn’t give any error while it’s going, and it creates a .mp4v file correctly, however when I try to watch the video that was supposed to be recorded, it can’t be opened. I tried with VLC and other apps but it’s unsupported everywhere.

Can someone tell me why?

Advertisement

Answer

There are several implementation issues:

  • As far "output.mp4v" is not a valid mp4 file extension in the current context.
    Change the file name to "output.mp4"
  • "MP4V" is case sensitive and suppposed to be "mp4v".
  • As gerda commented, the frame size may not be 1920×1080.
  • The else:, break at the end of the loop may break the loop after one frame.
  • cv2.waitKey is not working without using cv2.imshow (without an open windows).
    The loop is not terminated when q is pressed.
    The code may never reach to out.release().

Based on the following post, I tried to create a portable code that waits for Esc key, without root privilege in Linux.
The solution I found (waiting for Esc) seem a bit complicated… You may try other solutions.


Code sample:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement