基于OPenCV的视频播放变慢

分别使用tkinter,pygame,OpenCV播放视频,发现均有视频播放变慢的现象,源码如下:
使用tkinter,OpenCV播放视频:


from tkinter import *
import cv2
from PIL import Image, ImageTk

def video_play():
    while video.isOpened():
        ret, frame = video.read()  # 读取照片
        # print('读取成功')
        if ret == True:
            img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # 转换颜色使播放时保持原有色彩
            current_image = Image.fromarray(img).resize((1920,1080))  # 将图像转换成Image对象
            imgtk = ImageTk.PhotoImage(image=current_image)
            movieLabel.imgtk = imgtk
            movieLabel.config(image=imgtk)
            movieLabel.update()  # 每执行以此只显示一张图片,需要更新窗口实现视频播放
        else:
            break

root = Tk()
root.overrideredirect(True)
root.state("zoomed")
movieLabel = Label(root,width=1920,height=1080)  # 创建一个用于播放视频的label容器
movieLabel.pack()
while True:
    video = cv2.VideoCapture(r"E:\放松\xjj\掠影\原神\1612179174bfee75ebffaca79a.mp4_last.mp4")  # 使用opencv打开本地视频文件
    video_play()  # 调用video_play实现视频播放

mainloop()

使用pygame,OpenCV播放视频:


import pygame
import sys
import cv2
import numpy as np
import os
import moviepy.editor as mpy


if __name__ == '__main__':
    # 截取背景音乐
    # audio_background = mpy.AudioFileClip('cs1.mp4')
    #
    # audio_background.write_audiofile('cs1.mp3')
    pygame.init()  # 初始化pygame
    FPS = 100
    #设置窗口位置
    os.environ['SDL_VIDEO_WINDOW_POS']="%d,%d" % (5,30)
    FPSClock = pygame.time.Clock()
    size = width, height = 960, 540  # 设置窗口大小
    screen = pygame.display.set_mode(size)  # 显示窗口
    color = (255, 255, 255)  # 设置颜色
    ogg=pygame.mixer.Sound("cs1.mp3")
    #pygame.mixer.music.load("")

    videoCapture = cv2.VideoCapture("cs2.mp4")
    ogg.play()

    while True:
        a=pygame.time.get_ticks()

        if videoCapture.isOpened():
            #从opncv读一段视频进来
            ret, frame = videoCapture.read()
            if ret :
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                # frame = np.asarray(frame.resize((1920, 1080)))
                frame = cv2.resize(frame,(960,540),interpolation=cv2.INTER_CUBIC)
                frame = np.rot90(frame, k=-1)
                frame = pygame.surfarray.make_surface(frame)

                frame=pygame.transform.flip(frame,False,True)

                screen.blit(frame, (0,0))

        for event in pygame.event.get():  # 遍历所有事件
            if event.type == pygame.QUIT:  # 如果单击关闭窗口,则退出
                sys.exit()
        pygame.display.flip()  # 更新全部显示
        FPSClock.tick(FPS)
        pygame.time.get_ticks()

使用OpenCV播放视频:

import cv2
import numpy as np
# import moviepy.editor as mpy
import pygame
pygame.init()
# ogg=pygame.mixer.Sound("cs1.mp3")
# 背景音乐
cap = cv2.VideoCapture("cs1.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
# ogg.play()
while (True):
    # 读帧
    ret, frame = cap.read()
    # 显示图像
    cv2.imshow("video", cv2.resize(frame,(960,540),interpolation=cv2.INTER_CUBIC))
    # 图像的按比例压缩,
    if cv2.waitKey(int(1000/fps)) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

tkinter的我改了下 感觉快了一点
但是有个问题是 你的播放就没在tkinter里面 不能关闭
所以我觉得有问题
其次单纯用opencv还是很快的


from tkinter import *
import cv2
from PIL import Image, ImageTk


def video_play():
    while True:
        if video.isOpened():
            ret, frame = video.read()  # 读取照片

        if ret:
            img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # 转换颜色使播放时保持原有色彩
            current_image = Image.fromarray(img).resize((1280, 768))  # 将图像转换成Image对象

            imgtk = ImageTk.PhotoImage(image=current_image)
            movieLabel.imgtk = imgtk
            movieLabel.config(image=imgtk)
            movieLabel.update()  # 每执行以此只显示一张图片,需要更新窗口实现视频播放

        else:
            break


root = Tk()
root.overrideredirect(True)
root.state("zoomed")
movieLabel = Label(root, width=1280, height=768)  # 创建一个用于播放视频的label容器
movieLabel.pack()
video = cv2.VideoCapture(r"请君01.mp4")  # 使用opencv打开本地视频文件
# video.set(cv2.CAP_PROP_FPS, 30)
# video.set(cv2.CAP_PROP_FRAME_WIDTH, 600)
# video.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)
video_play()  # 调用video_play实现视频播放

mainloop()

opencv

import cv2
vid_cam = cv2.VideoCapture('请君01.mp4')
vid_cam.set(cv2.CAP_PROP_FPS, 25)
vid_cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
vid_cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 360)

while vid_cam.isOpened():
    ret, image_frame = vid_cam.read()
    cv2.imshow('frame', cv2.resize(image_frame,(960,540),interpolation=cv2.INTER_CUBIC))
    FPS_MS = int((1/30)*1000)
    if cv2.waitKey(FPS_MS) & 0xFF == ord('q'):
        break
vid_cam.release()
cv2.destroyAllWindows()

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 给你找了一篇非常好的博客,你可以看看是否有帮助,链接:编译opencv错误

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^