I have implemented a Python script which displays a video to the user and records it. The video is saved either compressed or uncompressed.
In an old program I have seen, that the "DIVX" MPEG-4 and the "IYUV" codecs were used. For some reason, they don't work on my computer (OUTPUT_VIDEO is a MP4-file).
OpenCV: FFMPEG: tag 0x58564944/'DIVX' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'
The "MPJPG" codec works with ".avi" files.
Because I'm not sure about the codecs, I would like to ask, which codecs should I use for my script to achieve the following requirements:
This is my source code: main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
import platform
#=========== TO CHANGE ===========
INPUT_VIDEO = os.path.join("..", "resources", "video.mp4")
OUTPUT_VIDEO = os.path.join("..", "recorded", "recorded.avi")
compress = False
#=========== TO CHANGE ===========
WINDOW_NAME = "Video Recorder"
player = cv2.VideoCapture(INPUT_VIDEO)
# Get the frames per second (fps) of the video.
fps = player.get(cv2.CAP_PROP_FPS)
# Get width and height via the video capture property.
width = player.get(cv2.CAP_PROP_FRAME_WIDTH)
height = player.get(cv2.CAP_PROP_FRAME_HEIGHT)
# Define the codec and create VideoWriter object according to the used operating system.
four_cc = None
if platform.system() == "Windows":
if compress:
four_cc = cv2.VideoWriter_fourcc(*"MJPG") # *"DIVX") # DIVX MPEG-4 codec.
else:
four_cc = cv2.VideoWriter_fourcc(*"MJPG") # *"IYUV") # Uncompressed yuv420p in avi container.
elif platform.system() == "Linux":
if compress:
four_cc = cv2.VideoWriter_fourcc(*"DIVX") # DIVX MPEG-4 codec.
else:
four_cc = cv2.VideoWriter_fourcc(*"IYUV") # Uncompressed yuv420p in avi container.
recorder = cv2.VideoWriter(OUTPUT_VIDEO, four_cc, fps, (int(width), int(height)))
if player is None:
quit()
while player.isOpened():
ret, frame = player.read()
if ret:
cv2.imshow(WINDOW_NAME, frame)
recorder.write(frame)
else:
break
key_code = cv2.waitKey(1)
# Closes the window if the ESC key was pressed.
if key_code == 27:
break
# Closes the window if the X button of the window was clicked.
if cv2.getWindowProperty(WINDOW_NAME, 1) == -1:
break
player.release()
recorder.release()
cv2.destroyAllWindows()
I'm using a Windows 7 computer, with opencv-contrib-python 3.4.0.12 and Python 3.6.
User contributions licensed under CC BY-SA 3.0