简体   繁体   中英

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

I wrote a small media player in python using TKinter and GStreamer which is embedded in my application. The player is based on the code below which is a small modification to Way to play video files in Tkinter? which works.

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()

I need to zoom in in some part of the video and therefore I need to use the the GStreamer Base Plugins called videocrop and videoscale which are both part of GStreamer 1.0.

Unfortunately after spending several days of research, I have not been able to find a simple python example on how these plugins can be used in TKiinter (I am not using Gtk nor any other library).

Could anyone provide me an example on how these could be used? Any help would be much appreciated. Thanks in advance…

I figured it out, and it can be done by adding a video-filter element. the code to add is the following:

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)

and below id the entire source code, tested on both linux and 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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