How to Play Mp4 Videos in Python Pyglet

In this Python Pyglet article i want to show  you How To Play Mp4 Videos In Python Pyglet . if you are interested in Python GUI Development, you can check the below links.

 

 

 

This is the complete code for this article

 

 

 

In these lines of code we can implement many functions to a media player using the Player class. Use of this class is also necessary for video playback. There are no parameters to its construction, also a player will play any source that is “queued” on it. Any number of sources can be queued on a single player, but once queued, a source can never be dequeued (until it is removed automatically once complete). The main use of this queuing mechanism is to facilitate “gapless” transitions between playback of media files.

And also make sure that you have an Mp4 video in your working project

 

 

 

 

These are the standard controls for controlling playback

Method Description
play() Begin or resume playback of the current source.
pause() Pause playback of the current source.
next_source() Dequeue the current source and move to the next one immediately. next() can also be used but it is deprecated because of incompatibilities with Python 3.
seek() Seek to a specific time within the current source.

 

 

 

There are several properties that describe the player’s current state:

Property Description
time The current playback position within the current source, in seconds. This is read-only (but see the seek() method).
playing True if the player is currently playing, False if there are no sources queued or the player is paused. This is read-only (but see the pause() and play() methods).
source A reference to the current source being played. This is read-only (but see the queue() method).
volume The audio level, expressed as a float from 0 (mute) to 1 (normal volume). This can be set at any time.

 

 

 

When a Player is playing back a source with video, use the get_texture() method to obtain the video frame image. This can be used to display the current video image synchronized with the audio track.

 

 

And finally

This will enter pyglet’s default event loop, and let pyglet respond to application events such as the mouse and keyboard. Your event handlers will now be called as required, and the run() method will return only when all application windows have been closed.

If you are coming from another library, you may be used to writing your own event loop. This is possible to do with pyglet as well, but it is generally discouraged; see The application event loop for details.

 

 

 

 

 

This the result i have just taken a screen capture of the video.

How To Play Mp4 Videos In Python Pyglet
How To Play Mp4 Videos In Python Pyglet

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “How to Play Mp4 Videos in Python Pyglet”

  1. there is a way to loop the video something like pyglet.app.run(loop=’true’)
    or pyglet.app.run.loop() ?

  2. I find that the sound of the video is later after the flame texture ,they’re out of sync,how to solve this problem? Code like this:
    import pyglet
    window=pyglet.window.Window()
    path=’myvideo.mp4′
    source=pyglet.media.load(path)

    player = pyglet.media.Player()
    player.queue(source)

    player.play()

    @window.event
    def on_draw():
    window.clear()
    player.get_texture().blit(0,0)

    pyglet.app.run()

Comments are closed.

Share via
Copy link
Powered by Social Snap
×