Tkinter 将视频插入窗口 [英] Tkinter inserting video into window

查看:53
本文介绍了Tkinter 将视频插入窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有用于从 tkinter 程序打开 .mp4 文件的代码.

I currently have this code for opening up a .mp4 file from a tkinter program.

import os
from tkinter import *

app = Tk()
app.title('Video Player')

Fcanvas = Canvas(bg="black", height=600, width=170)


def snd1():
    os.system("C:\\Users\Burky\\Desktop\\Videos\\PermsAndCombsVideo.mp4")

var = IntVar()

rb1 = Radiobutton(app, text= "Play Video", variable = var, value=1, command=snd1)
rb1.pack(anchor = W)
Fcanvas.pack()
app.mainloop()

这很好,虽然视频在快速播放器中打开并且没有嵌入到窗口中,但有没有办法将其实现到主窗口而不是在快速播放器中打开?

This is good, although the video opens up within quick-time player and is not embedded within the window, is there a way to implement this into the main window instead of it opening up in the quick-time player?

谢谢

推荐答案

这是一种方法.这将不断将标签图​​像更新为指定视频的帧.如果您也想要声音,则必须处理声音.这应该可以帮助您入门.

Here's one way to do it. This will continuously update the labels image to be the frames of the video specified. You'll have to handle sound if you want that too. This should get you started.

import tkinter as tk, threading
import imageio
from PIL import Image, ImageTk

video_name = "test.mkv" #This is your video file path
video = imageio.get_reader(video_name)

def stream(label):

    for image in video.iter_data():
        frame_image = ImageTk.PhotoImage(Image.fromarray(image))
        label.config(image=frame_image)
        label.image = frame_image

if __name__ == "__main__":

    root = tk.Tk()
    my_label = tk.Label(root)
    my_label.pack()
    thread = threading.Thread(target=stream, args=(my_label,))
    thread.daemon = 1
    thread.start()
    root.mainloop()

这篇关于Tkinter 将视频插入窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆