V
V
Vladosio2021-04-15 13:19:08
Python
Vladosio, 2021-04-15 13:19:08

Is there any automatic way to match FPS before recording video?

Is there any way to automatically pick up the FPS before recording a video so that there is no frame loss? For example, if I recorded 10 seconds of video, then the file should be 10 seconds long.
And so it turns out that my IP camera (phone) can record 30 frames per second, and in the code if you set 30 frames per second or select FPS via cap.get(cv2.CAP_PROP_FPS) , then part of the recording is lost depending on the frame size (1920*1080, 1280*720, etc.). It turns out that the larger the frame size, the less FPS is needed to make the video about the same duration and without delays.
Is it possible to somehow do it differently so that it automatically selects the necessary FPS for optimal video recording?
If you record with rtsp protocoland use cap.get(cv2.CAP_PROP_FPS) , then print(fps) outputs 180.000 frames to the console, and later an error appears:

180000.0
[mpeg4 @ 00000222e21f0a40] timebase 1/180000 not supported by MPEG 4 standard, the maximum admitted value for the timebase denominator is 65535
Could not open codec 'mpeg4': Unspecified error


import cv2
import os, os.path
import sys
import time
import pathlib
from PIL import Image

cap = cv2.VideoCapture('rtsp://IP:Port/h264_pcm.sdp')
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) 
fps = 20#cap.get(cv2.CAP_PROP_FPS)
print(fps)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
folder = pathlib.Path().absolute()
video1 = fr'{folder}/VMF/VIDEO_{time.strftime("%m.%d.%Y_%H.%M.%S")}.avi'
out = cv2.VideoWriter(video1, fourcc, fps, (int(w),int(h)))
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True: 
        frame = cv2.flip(frame,1) 
        out.write(frame)
        cv2.imshow('video',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    else:
        break

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2021-04-15
@Vladosio

FPS is the reciprocal of time. Measure how long it takes you to process one frame, and get the maximum available FPS for real-time processing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question