我目前有这个代码用于从tkinter程序中打开.mp4文件。
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()
这很好,虽然视频在快速播放器中打开并且未嵌入窗口中,是否有办法将其实现到主窗口而不是在快速播放器中打开?
感谢
答案 0 :(得分:2)
这是一种方法。这将不断更新标签图像,使其成为指定视频的帧。如果你想要的话,你必须处理声音。这应该可以帮到你。
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()