Quantcast
Channel: Active questions tagged 22.04 - Ask Ubuntu
Viewing all articles
Browse latest Browse all 4447

OpenCV VideoCapture recording is sped up

$
0
0

I am trying to save some footage from a webcam but am coming across some issues. My webcam supports compressed (MJPG) and raw (YUYv 4:2:2) formats and I am trying to get MJPG due to the faster fps. I have simplified my code (below) and have reproduced the issue.

import cv2from datetime import datetime, timedeltafourcc = cv2.VideoWriter_fourcc(*'avc1')  fn = "out.mp4"cap = cv2.VideoCapture(0, cv2.CAP_V4L2)cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))fps = int(cap.get(cv2.CAP_PROP_FPS))out = cv2.VideoWriter(fn, fourcc, fps, (int(cap.get(3)), int(cap.get(4))))frames_written = 0print(f"FPS {fps}")show = Falseend = datetime.utcnow() + timedelta(seconds=20)while end >= datetime.utcnow():    ret, frame = cap.read()    if not ret:        raise Exception("NO FRAME")    out.write(frame)    frames_written+=1    if show:        cv2.imshow("f", frame)        cv2.waitKey(33)cap.release()out.release()cv2.destroyAllWindows()

When I try to use the webcam video device directly in OpenCV, the recorded footage although smooth, is sped up.

I have also tried creating a dummy video device and sending ffmpeg output to it using the following command:

ffmpeg -f v4l2 -input_format mjpeg -framerate 30 -video_size 1920x1080 -i /dev/video0 -pix_fmt yuyv422 -f v4l2 /dev/video4  

When I try to use the dummy video device as the src the first couple seconds seem normal speed but is choppy but then speeds up and then is less choppy. Any help would be appreciated. I have been banging my head for the last couple of days.

My output for v4l2-ctl --list-formats-exts are:

ioctl: VIDIOC_ENUM_FMTType: Video Capture[0]: 'MJPG' (Motion-JPEG, compressed)    Size: Discrete 2592x1944        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 2560x1440        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 1920x1080        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 1280x1024        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 1280x720        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 960x540        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 848x480        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 800x600        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 640x480        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 320x240        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 160x120        Interval: Discrete 0.033s (30.000 fps)[1]: 'YUYV' (YUYV 4:2:2)    Size: Discrete 2592x1944        Interval: Discrete 0.500s (2.000 fps)    Size: Discrete 2560x1440        Interval: Discrete 0.500s (2.000 fps)    Size: Discrete 1920x1080        Interval: Discrete 0.500s (2.000 fps)    Size: Discrete 1280x1024        Interval: Discrete 0.200s (5.000 fps)    Size: Discrete 1280x720        Interval: Discrete 0.200s (5.000 fps)    Size: Discrete 960x540        Interval: Discrete 0.067s (15.000 fps)    Size: Discrete 848x480        Interval: Discrete 0.050s (20.000 fps)    Size: Discrete 800x600        Interval: Discrete 0.050s (20.000 fps)    Size: Discrete 640x480        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 320x240        Interval: Discrete 0.033s (30.000 fps)    Size: Discrete 160x120        Interval: Discrete 0.033s (30.000 fps)

Viewing all articles
Browse latest Browse all 4447

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>