CV2 wont allow decimal fps and the video display size is off

cv2.waitKey will not allow for decimals, because of this my videos that have frame rates of 59.97fps will only run at 59. This causes the video and the audio not to sync up correctly. Also, the display size of certain videos does not fill the whole screen, instead, they get cut off. Thanks in advance :slight_smile: Here is my code:

import cv2
import numpy as np
#ffpyplayer for playing audio
from ffpyplayer.player import MediaPlayer

userinput = input('What video u want? ')
variable = 'j'
video_path = ""

while(userinput != 'v'):
    if(userinput.find("LTP") != -1):
        video_path = "LTP.mp4"
    if(userinput.find("Henry") != -1):
        video_path= "Henry Smith.mp4"
    if(userinput.find("Apple")!= -1):
        video_path = "Apple Factory.mp4"
    if(userinput.find("Grinch") != -1):
        video_path = "Grinch Sample.mp4"
def PlayVideo(video_path):
    player = MediaPlayer(video_path)
    fps = video.get(cv2.CAP_PROP_FPS)
    interval = int(1000/fps)
    while True:
        audio_frame, val = player.get_frame()
        if not grabbed:
            print("End of video")
        if cv2.waitKey(interval) & 0xFF == ord("q"):
        cv2.imshow("Video", frame)
        if val != 'eof' and audio_frame is not None:
            img, t = audio_frame

Your code has a bug in this. It considers fps on in waitKey, but obviously all other statements take time too. You need to measure whole elapsed time, by frame or from beginning, and adjust any waits for that.

How would I do that?

Can someone please explain what matt means. Sorry this is a project for school and I am new to this,

you have to measure the elapsed time spent per iteration in your while - loop.

then –

cv2.waitKey(interval - time_per_iteration)

also, the waitKey() call should go after imshow(), else you’re refreshing the previous frame, not the current

but please, opencv’s VideoCapture is not meant to be (ab)used to build a media player, rather use ffmpeg instead

hey berak how would i do that!