PYGLET – Get Current Media Texture in Player

In this article we will see how we can get current media texture of player of PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a “heavyweight” object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire screen (fullscreen). This module allows applications to specify a search path for resources. Pyglet can play WAV files, and if FFmpeg is installed, many other audio and video formats. Playback is handled by the Player class, which reads raw data from Source objects and provides methods for pausing, seeking, adjusting the volume, and so on. Getting the texture for the current video frame should be called every time we display a frame of video, as multiple textures might be used. The return value will be None if there is no video in the current source.

We can create a window and player object with the help of commands given below

# creating a window
window = pyglet.window.Window(width, height, title)

# creating a player for media
player = pyglet.media.Player()

In order to do this we use get_texture play method with the player object

Syntax : player.get_texture()

Argument : It takes no argument



Return : It returns pyglet.image.Texture object

Below is the implementation

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pyglet module
import pyglet
  
# width of window 
width = 800
    
# height of window 
height = 500
    
# caption i.e title of the window 
title = "Geeksforgeeks"
    
# creating a window 
window = pyglet.window.Window(width, height, title) 
  
  
# video path
vidPath ="gfg.mp4"
  
# creating a media player object
player = pyglet.media.Player()
  
# creating a source object
source = pyglet.media.StreamingSource()
  
# load the media from the source
MediaLoad = pyglet.media.load(vidPath)
  
# add this media in the queue
player.queue(MediaLoad)
  
# play the video
player.play()
  
# on draw event
@window.event
def on_draw():
      
    # clea the window
    window.clear()
      
    # if player sorce exist
    # and video format exist
    if player.source and player.source.video_format:
          
        # get the texture of video and
        # make surface to display on the screen
        player.get_texture().blit(0, 0)
          
          
# key press event     
@window.event 
def on_key_press(symbol, modifier): 
    
    # key "p" get press 
    if symbol == pyglet.window.key.P: 
          
        # pause the video
        player.pause()
          
        # printing message
        print("Video is paused")
          
          
    # key "r" get press 
    if symbol == pyglet.window.key.R: 
          
        # resume the video
        player.play()
          
        # printing message
        print("Video is resumed")
          
  
          
# seek video at time stamp = 4
# and pause the video
player.seek(4)
player.pause()
  
# getting texture of the video
value = player.get_texture()
  
# printing value of texture
print("Texture : " + str(value))
          
          
  
# run the pyglet application
pyglet.app.run()

chevron_right


Texture : 



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.