繁体   English   中英

如何在python TKinter中使用GStreamer调整视频播放器的大小和裁剪?

[英]How to resize and crop in a video player using GStreamer in python TKinter?

我使用嵌入在我的应用程序中的TKinter和GStreamer在python中编写了一个小型媒体播放器。 播放器基于以下代码,这是对在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()

我需要放大视频的某些部分,因此我需要使用GStreamer基本插件,称为videocropvideoscale ,它们都是GStreamer 1.0的一部分。

不幸的是,在花了几天的研究后,我无法找到一个简单的python示例来说明如何在TKiinter中使用这些插件(我没有使用Gtk或任何其他库)。

谁能给我一个例子说明如何使用它们? 任何帮助将非常感激。 提前致谢…

我想通了,可以通过添加视频过滤器元素来完成。 要添加的代码如下:

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)

在id和下面的是完整源代码,已在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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM