首页 > 解决方案 > CV2 不允许十进制 fps 并且视频显示大小已关闭

问题描述

cv2.waitKey 不允许使用小数,因此我的帧速率为 59.97fps 的视频只能以 59 运行。这会导致视频和音频无法正确同步。此外,某些视频的显示尺寸不会填满整个屏幕,而是会被截断。在此先感谢:)这是我的代码:

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"
        break
    if(userinput.find("Henry") != -1):
        video_path= "Henry Smith.mp4"
        break
    if(userinput.find("Apple")!= -1):
        video_path = "Apple Factory.mp4"
        break
    if(userinput.find("Grinch") != -1):
        video_path = "Grinch Sample.mp4"
        break
def PlayVideo(video_path):
    video=cv2.VideoCapture(video_path)
    player = MediaPlayer(video_path)
    fps = video.get(cv2.CAP_PROP_FPS)
    interval = int(1000/fps)
    while True:
        grabbed, frame=video.read()
        audio_frame, val = player.get_frame()
        if not grabbed:
            print("End of video")
            break
        if cv2.waitKey(interval) & 0xFF == ord("q"):
            break
        cv2.imshow("Video", frame)
        
        if val != 'eof' and audio_frame is not None:
            #audio
            img, t = audio_frame
    
    video.release()
    cv2.destroyAllWindows()
    
PlayVideo(video_path)

标签: pythonpython-3.xpython-3.7

解决方案


推荐阅读