如何在 python TKinter 中使用 GStreamer 在视频播放器中调整大小和裁剪?
How to resize and crop in a video player using GStreamer in python TKinter?
我使用嵌入在我的应用程序中的 TKinter 和 GStreamer 在 python 中编写了一个小型媒体播放器。该播放器基于下面的代码,该代码是对 Way to play video files in Tkinter? 的一个小修改。
import os, sys
import Tkinter as tkinter
import threading
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstVideo', '1.0')
gi.require_version('GdkX11', '3.0')
from gi.repository import Gst, GObject, GdkX11, GstVideo
def set_frame_handle(bus, message, frame_id):
if not message.get_structure() is None:
print message.get_structure().get_name()
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)
window = tkinter.Tk()
window.title('')
window.geometry('500x400')
GObject.threads_init()
Gst.init(None)
display_frame = tkinter.Canvas(window, bg='#030')
display_frame.pack(side=tkinter.TOP,expand=tkinter.YES,fill=tkinter.BOTH)
frame_id = display_frame.winfo_id()
player = Gst.ElementFactory.make('playbin', None)
filepath = os.path.realpath('kbps.mp4')
filepath2 = "file:///" + filepath.replace('\', '/').replace(':', '|')
player.set_property('uri', filepath2)
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.connect('sync-message::element', set_frame_handle, frame_id)
player.set_state(Gst.State.PLAYING)
window.mainloop()
我需要放大视频的某些部分,因此我需要使用名为 videocrop 和 videoscale[=23= 的 GStreamer 基本插件] 它们都是 GStreamer 1.0 的一部分。
不幸的是,经过几天的研究,我无法找到一个简单的 python 示例来说明如何在 TKiinter 中使用这些插件(我既没有使用 Gtk 也没有使用任何其他库)。
谁能给我举个例子说明如何使用它们?任何帮助将非常感激。提前致谢...
我想通了,加一个video-filter元素就可以了。要添加的代码如下:
VideoCrop = Gst.ElementFactory.make('videocrop', 'VideoCrop')
VideoCrop.set_property('top', 100)
VideoCrop.set_property('bottom', 100)
VideoCrop.set_property('left', 50)
VideoCrop.set_property('right', 150)
player.set_property('video-filter', VideoCrop)
及以下是完整的源代码,在 linux 和 Windows
上进行了测试
import os, sys
import Tkinter as tkinter
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstVideo', '1.0')
gi.require_version('GdkX11', '3.0')
from gi.repository import Gst, GObject, GdkX11, GstVideo
def set_frame_handle(bus, message, frame_id):
if not message.get_structure() is None:
print message.get_structure().get_name()
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)
window = tkinter.Tk()
window.title('')
window.geometry('500x400')
GObject.threads_init()
Gst.init(None)
# can aslo use display_frame = tkinter.Frame(window)
display_frame = tkinter.Canvas(window, bg='#030')
display_frame.pack(side=tkinter.TOP,expand=tkinter.YES,fill=tkinter.BOTH)
frame_id = display_frame.winfo_id()
player = Gst.ElementFactory.make('playbin', None)
filepath = os.path.realpath('kbps.mp4')
filepath2 = "file:///" + filepath.replace('\', '/').replace(':', '|')
player.set_property('uri', filepath2)
VideoCrop = Gst.ElementFactory.make('videocrop', 'VideoCrop')
VideoCrop.set_property('top', 100)
VideoCrop.set_property('bottom', 100)
VideoCrop.set_property('left', 50)
VideoCrop.set_property('right', 150)
player.set_property('video-filter', VideoCrop)
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.connect('sync-message::element', set_frame_handle, frame_id)
player.set_state(Gst.State.PLAYING)
window.mainloop()
我使用嵌入在我的应用程序中的 TKinter 和 GStreamer 在 python 中编写了一个小型媒体播放器。该播放器基于下面的代码,该代码是对 Way to play video files in Tkinter? 的一个小修改。
import os, sys
import Tkinter as tkinter
import threading
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstVideo', '1.0')
gi.require_version('GdkX11', '3.0')
from gi.repository import Gst, GObject, GdkX11, GstVideo
def set_frame_handle(bus, message, frame_id):
if not message.get_structure() is None:
print message.get_structure().get_name()
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)
window = tkinter.Tk()
window.title('')
window.geometry('500x400')
GObject.threads_init()
Gst.init(None)
display_frame = tkinter.Canvas(window, bg='#030')
display_frame.pack(side=tkinter.TOP,expand=tkinter.YES,fill=tkinter.BOTH)
frame_id = display_frame.winfo_id()
player = Gst.ElementFactory.make('playbin', None)
filepath = os.path.realpath('kbps.mp4')
filepath2 = "file:///" + filepath.replace('\', '/').replace(':', '|')
player.set_property('uri', filepath2)
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.connect('sync-message::element', set_frame_handle, frame_id)
player.set_state(Gst.State.PLAYING)
window.mainloop()
我需要放大视频的某些部分,因此我需要使用名为 videocrop 和 videoscale[=23= 的 GStreamer 基本插件] 它们都是 GStreamer 1.0 的一部分。
不幸的是,经过几天的研究,我无法找到一个简单的 python 示例来说明如何在 TKiinter 中使用这些插件(我既没有使用 Gtk 也没有使用任何其他库)。
谁能给我举个例子说明如何使用它们?任何帮助将非常感激。提前致谢...
我想通了,加一个video-filter元素就可以了。要添加的代码如下:
VideoCrop = Gst.ElementFactory.make('videocrop', 'VideoCrop')
VideoCrop.set_property('top', 100)
VideoCrop.set_property('bottom', 100)
VideoCrop.set_property('left', 50)
VideoCrop.set_property('right', 150)
player.set_property('video-filter', VideoCrop)
及以下是完整的源代码,在 linux 和 Windows
上进行了测试import os, sys
import Tkinter as tkinter
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstVideo', '1.0')
gi.require_version('GdkX11', '3.0')
from gi.repository import Gst, GObject, GdkX11, GstVideo
def set_frame_handle(bus, message, frame_id):
if not message.get_structure() is None:
print message.get_structure().get_name()
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)
window = tkinter.Tk()
window.title('')
window.geometry('500x400')
GObject.threads_init()
Gst.init(None)
# can aslo use display_frame = tkinter.Frame(window)
display_frame = tkinter.Canvas(window, bg='#030')
display_frame.pack(side=tkinter.TOP,expand=tkinter.YES,fill=tkinter.BOTH)
frame_id = display_frame.winfo_id()
player = Gst.ElementFactory.make('playbin', None)
filepath = os.path.realpath('kbps.mp4')
filepath2 = "file:///" + filepath.replace('\', '/').replace(':', '|')
player.set_property('uri', filepath2)
VideoCrop = Gst.ElementFactory.make('videocrop', 'VideoCrop')
VideoCrop.set_property('top', 100)
VideoCrop.set_property('bottom', 100)
VideoCrop.set_property('left', 50)
VideoCrop.set_property('right', 150)
player.set_property('video-filter', VideoCrop)
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.connect('sync-message::element', set_frame_handle, frame_id)
player.set_state(Gst.State.PLAYING)
window.mainloop()