import sys
import os
if sys.version_info[0] < 3:
import Tkinter as tkinter
else:
import tkinter
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject
# Needed for set_window_handle():
gi.require_version('GstVideo', '1.0')
from gi.repository import GstVideo
def set_frame_handle(bus, message, frame_id):
if not message.get_structure() is None:
if message.get_structure().get_name() == 'prepare-window-handle':
display_frame = message.src
display_frame.set_property('force-aspect-ratio', True)
display_frame.set_window_handle(frame_id)
NUMBER_OF_FRAMES = 8 # with more frames than arguments, videos are repeated
relative_height = 1 / float(NUMBER_OF_FRAMES)
# Only argument number checked, not validity.
number_of_file_names_given = len(sys.argv) - 1
if number_of_file_names_given < 1:
print('Give at least one video file name.')
sys.exit()
if number_of_file_names_given < NUMBER_OF_FRAMES:
print('Up to', NUMBER_OF_FRAMES, 'video file names can be given.')
file_names = list()
for index in range(number_of_file_names_given):
file_names.append(sys.argv[index + 1])
window = tkinter.Tk()
window.title("Multiple videos in a column using Tk and GST 1.0")
window.geometry('480x960')
Gst.init(None)
GObject.threads_init()
for number in range(NUMBER_OF_FRAMES):
display_frame = tkinter.Frame(window, bg='')
relative_y = number * relative_height
display_frame.place(relx = 0, rely = relative_y,
anchor = tkinter.NW, relwidth = 1, relheight = relative_height)
frame_id = display_frame.winfo_id()
player = Gst.ElementFactory.make('playbin', None)
fullname = os.path.abspath(file_names[number % len(file_names)])
player.set_property('uri', 'file://%s' % fullname)
player.set_state(Gst.State.PLAYING)
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.connect('sync-message::element', set_frame_handle, frame_id)
window.mainloop()
import tkinter as tk
from tkVideoPlayer import TkinterVideo
root = tk.Tk()
videoplayer = TkinterVideo(master=root, scaled=True)
videoplayer.load(r"samplevideo.mp4")
videoplayer.pack(expand=True, fill="both")
videoplayer.play() # play the video
root.mainloop()
import os
import sys
import tkinter as tkinter
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst as gst
from gi.repository import GObject as gobject
def on_sync_message(bus, message, window_id):
return
# message does not have structure field any more.
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()
gst.init(None)
player = gst.ElementFactory.make('playbin', '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()
6条答案
按热度按时间lb3vh1jj1#
你可以使用
python-gstreamer
来播放视频(我在Linux上可以用,但在Windows上也可以用)。这需要python-gstreamer
和python-gobject
,我建议你使用这个一体化的安装程序。下面是代码:
jgwigjjp2#
下面的代码适用于Ubuntu 16.04下的GStreamer 1.0和Python 3,它还支持在一个窗口中堆叠成一列的八个视频播放器(声音通道只是简单地混合在一起)。
libav/ffmpeg分支在Ubuntu 14.04下产生了一些问题,这些问题似乎在16.04下得到了解决。请注意,除了gstreamer1.0-plugins-* 之外,您还需要gstreamer1.0-libav包。
这段代码基于@koehlma在2011年给出的答案,假设GStreamer 0.10和Python 2。
y0u0uwnf3#
使用
tkvideoplayer
版本〉= 2.0.0库,可以帮助您播放、暂停、查找、获取视频的元数据等。示例:
GitHub page中有一个创建完整视频播放器的示例
参考documentation
gab6jxml4#
简单的方法是使用
tkVideo
来安装它
这是脚本,告诉你它是如何工作容易!
这是PyPI链接到它的更多信息。
vsikbqxv5#
koehlma的回答相当过时,“Tkinter”应该是“tkinter”;“商品及服务税”应为“商品及服务税”;“playbin 2”应该是“playbin”。“STATE_PLAYING”应该是“State.PLAYING”。下面的设置和代码在Debian 11上有效,截止日期为2022年4月5日。
首先,您需要安装的软件包包括
代码应更新为:
kmb7vmvb6#
我最近做了一个软件包来实现这个功能,无边框视频播放器,它也可以同步播放音频。
代码:
repository