如何在 Tkinter 中播放视频文件? [英] Way to play video files in Tkinter?

查看:285
本文介绍了如何在 Tkinter 中播放视频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法播放AVIMP4 等?

Is there a way to play video files like AVI, MP4, etc.?

我尝试使用 PyMedia,但显然它只适用于 Pygame.

I tried using PyMedia, but apparently it only works with Pygame.

我的问题的解决方案是什么?

What is the solution to my problem?

推荐答案

你可以使用 python-gstreamer 来播放视频(这对我来说适用于 Linux,但它也适用于 Windows).这需要 python-gstreamerpython-gobject,我会推荐您可以使用此多合一安装程序.

You could use python-gstreamer for playing videos (this works for me on Linux, but it should also work on Windows). This requires python-gstreamer and python-gobject, I would recommend you to use this all-in-one installer.

代码如下:

import os
import sys
import Tkinter as tkinter

import gobject
import gst

def on_sync_message(bus, message, window_id):
        if not message.structure is None:
            if message.structure.get_name() == 'prepare-xwindow-id':
                image_sink = message.src
                image_sink.set_property('force-aspect-ratio', True)
                image_sink.set_xwindow_id(window_id)

gobject.threads_init()

window = tkinter.Tk()
window.geometry('500x400')

video = tkinter.Frame(window, bg='#000000')
video.pack(side=tkinter.BOTTOM,anchor=tkinter.S,expand=tkinter.YES,fill=tkinter.BOTH)

window_id = video.winfo_id()

player = gst.element_factory_make('playbin2', 'player')
player.set_property('video-sink', None)
player.set_property('uri', 'file://%s' % (os.path.abspath(sys.argv[1])))
player.set_state(gst.STATE_PLAYING)

bus = player.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect('sync-message::element', on_sync_message, window_id)

window.mainloop()

这篇关于如何在 Tkinter 中播放视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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