简体   繁体   中英

Python 3 OpenCV unable to record and save video

I am using cv2 version 4.2.0, I am trying to record and save a video but I get an error.

The Code

import numpy as np
import os
import cv2


filename = 'myvid.avi'
frames_per_second = 24.0
res = '720p'

def change_res(cap, width, height):
    cap.set(3, width)
    cap.set(4, height)

STD_DIMENSIONS =  {
    "480p": (640, 480),
    "720p": (1280, 720),
    "1080p": (1920, 1080),
    "4k": (3840, 2160),
}

def get_dims(cap, res='1080p'):
    width, height = STD_DIMENSIONS["480p"]
    if res in STD_DIMENSIONS:
        width,height = STD_DIMENSIONS[res]
    ## change the current caputre device
    ## to the resulting resolution
    change_res(cap, width, height)
    return width, height

VIDEO_TYPE = {
    'avi': cv2.VideoWriter_fourcc(*'XVID'),
    #'mp4': cv2.VideoWriter_fourcc(*'H264'),
    'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}

def get_video_type(filename):
    filename, ext = os.path.splitext(filename)
    if ext in VIDEO_TYPE:
      return  VIDEO_TYPE[ext]
    return VIDEO_TYPE['avi']



cap = cv2.VideoCapture(0)
out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))

while True:
    ret, frame = cap.read()
    out.write(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
out.release()
cv2.destroyAllWindows()

The Error

cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback

I tried adding the full directory as directed on other answers but that fails too and shows me the same error. I am following a video tutorial and the same code seems to work out fine for the instructor.

You are using cv2.VideoCapture(0) that captures video from a camera , code is probably not working because you don't have a connected camera.

In case you do have a connected camera there are other reasons the code may not work.
In case you don't have a camera you may download and install virtual camera application (that captures the display for example).

I suggest you try reading video from a file first.

The code sample below:

  • Generates synthetic video file (build input video file).
    The purpose of generating synthetic video is creating a "self contained" example.
  • Read video frames from the generated file, and write frames to output video file.
    The resolution of the input and the output video is the same.

Here is the code:

import numpy as np
import cv2
import os

intput_filename = 'input_vid.avi'

# Generate synthetic video to be used as input:
###############################################################################
width  = 640
height = 480

n_frames = 250

# Use motion JPEG codec (for testing)
synthetic_out = cv2.VideoWriter(intput_filename, cv2.VideoWriter_fourcc(*'MJPG'), 25, (width, height))

for i in range(n_frames):
    img = np.full((height, width, 3), 60, np.uint8)
    cv2.putText(img, str(i), (width//2-100*len(str(i)), height//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (30, 255, 30), 20)
    #cv2.imshow('img',img)
    #cv2.waitKey(100)
    synthetic_out.write(img)

synthetic_out.release()
###############################################################################



filename = 'myvid.avi'
frames_per_second = 24.0
res = '720p'

def change_res(cap, width, height):
    cap.set(3, width)
    cap.set(4, height)

STD_DIMENSIONS =  {
    "480p": (640, 480),
    "720p": (1280, 720),
    "1080p": (1920, 1080),
    "4k": (3840, 2160),
}

def get_dims(cap, res='1080p'):
    width, height = STD_DIMENSIONS["480p"]
    if res in STD_DIMENSIONS:
        width,height = STD_DIMENSIONS[res]
    ## change the current capture device
    ## to the resulting resolution
    change_res(cap, width, height)
    return width, height

VIDEO_TYPE = {
    'avi': cv2.VideoWriter_fourcc(*'XVID'),
    #'mp4': cv2.VideoWriter_fourcc(*'H264'),
    'mp4': cv2.VideoWriter_fourcc(*'mp4v'),
}

def get_video_type(filename):
    filename, ext = os.path.splitext(filename)
    if ext in VIDEO_TYPE:
      return  VIDEO_TYPE[ext]
    return VIDEO_TYPE['avi']


# Read video from file instead of from camera.
cap = cv2.VideoCapture(intput_filename)
#cap = cv2.VideoCapture(0)

fourcc = get_video_type(filename)

# Get resolution of input video
width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))


# out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))

# The second argument of VideoWriter is FOURCC code
# Set the size of the output video to be same as the input (get_dims is not working).
out = cv2.VideoWriter(filename, fourcc, frames_per_second, (width, height))

while True:
    ret, frame = cap.read()

    if not ret:
        # Break loop if ret is False
        break;

    out.write(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(100) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

Please let me know if the example is working or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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