Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What is Pyglet?
Sound and Video in the Pyglet
Audio Drivers
Supported Media Types
FFmpeg Installation
Loading Media
Audio Synthesis
Controlling Playback
Gapless Playback
Incorporating Video
Positional Audio
Ticking the Clock
Frequently Asked Questions
Why use Pyglet?
What are the advantages of Pyglet?
Which is better, pygame or pyglet?
Last Updated: Mar 27, 2024

Sound and Video in the Pyglet

Author Sagar Mishra
0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


Pyglet is a Python windowing and multimedia framework that may be used to create games and other visually rich applications. Pyglet is smoother and performs better in terms of speed, which is always a concern when building a game. Let's understand the essential components of Pyglet, which will help further in developing a visually rich application. This article will discuss the Sound and Video in the Pyglet and their components.

What is Pyglet?

Source Image- Link

Pyglet is a Python programming language library that provides an object-oriented application programming interface to create games and other multimedia applications. Pyglet runs on Microsoft Windows, Linux, and macOS, released under a BSD Licence. Pyglet makes it simple and easy to play and mix many sounds in your game. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Sound and Video in the Pyglet

Pyglet can play multiple audio and video formats. Audio is played back with anyone from OpenAL, XAudio2, DirectSound, or Pulseaudio, allowing hardware-accelerated mixing and surround-sound 3-D positioning. Video is played into OpenGL textures and can be effortlessly manipulated in real-time through applications and incorporated into 3-D environments.

Audio Drivers

Pyglet can use XAudio2, OpenAL, Pulseaudio, or DirectSound to playback audio. Only these types of drivers can be used in an application. In maximum instances, you won't need to concern yourself with selecting a driver. However, you can manually pick one if desired. Choosing the driver has to be done before the module is loaded. The available drivers that rely upon your operating system are as follow:

  • DirectSound: DirectSound is available on Windows only and is installed by default on Windows XP and later versions. Pyglet uses the most effective DirectX 7 features. On Windows Vista, DirectSound doesn't support hardware audio blending or surround sound.
  • XAudio2: XAudio2 is available only on Windows Vista and above and is the substitute for DirectSound. XAudio2 offers hardware-accelerated audio support for newer operating systems. Note that during a few stripped-down versions of Windows 10, XAudio2 might not be available till the required DLLs are installed.
  • OpenAL: OpenAL has Mac Operating System X. Windows users can download a frequent driver from or their sound device's manufacturer. Most Linux distributions have OpenAL available in the repositories for download.
  • Pulse: Pulseaudio became the standard Linux audio implementation in the last few years and is installed through default with most modern Linux distributions. Pulseaudio does not consist of positional audio and is restricted to stereo. It is suggested to use OpenAL if positional audio is required.

Supported Media Types

Windows and Linux both support a restricted quantity of compressed audio types without the need for FFmpeg. While FFmpeg helps an extensive array of formats and codecs, it might be an unnecessarily large dependency when simple audio playback is wanted on these OS. 

These formats are supported natively under the subsequent systems and codecs:

  • Windows Media Foundation: Eg. MP3, WMA, AFS
  • GStreamer: Eg. FLAC, MP3, OGG, M4A
  • PyOgg: Eg. FLAC, OGG, OPUS
  • FFmpeg: Eg. AU, MP2, WAV, WMA

FFmpeg Installation

FFmpeg is an open-source media tool that you can use to transform any video format into the one you need. The tool is command-line only, so it does not have a graphical, clickable interface. If you are used to installing standard graphical Windows programs, installing FFmpeg may look complex at first- but don't worry; it is pretty easy!

You can set up FFmpeg for your platform using the following commands found on the FFmpeg download page. You must select the shared build for the targeted Operating System with the architecture like the Python interpreter.

The error message when the wrong architectures were downloaded on windows is:

WindowsError: [Error 193] %1 is not a valid Win32 application


At last, check whether you download the shared builds, not the static or the dev builds.

Loading Media

Audio and video files are loaded simultaneously, using the feature, giving a filename: 

source ='explosion.wav') 


If the media record is bundled with the application, remember to use the resource module, and for reference, you can see Application resources. 

The final result of loading a media file is a Source object. This object gives helpful information about the kind of media encoded in some files and serves as an opaque object used for playing back the record. 

  • Load function: The load() function will enhance a MediaException if the format is unknown. IOError will also be raised if the file can't be examined from the disk. Future versions of pyglet may also support reading from arbitrary file-like objects, but a valid filename should currently be given. The length of the media file is provided by the duration property, which returns the media's length in seconds.
  • Audio Metadata: Audio metadata is supplied in the source's audio_format attribute, None for silent videos. This metadata isn't typically helpful to applications.
  • Video Metadata: Video metadata is supplied in the source's video_format attribute, which is None for audio files. It is suggested that this attribute is checked before trying to playback a video file – if a film file has readable audio music but an unknown video format, it'll appear as an audio file.

Audio Synthesis

The module documentation for each will offer extra information on building them. However, at a minimum, you'll need to specify the duration. You will also need to set the audio frequency (full waveforms will default to 440Hz). Some waveforms, like the FM, have additional parameters. Some basic waveforms which are available:

  • Sine
  • Sawtooth
  • Square
  • FM
  • Silence
  • WhiteNoise
  • Digitar

Controlling Playback

A player will play any source which is queued on it. Multiple numbers of sources can be queued on a single participant, but once queued, a source can not be dequeued (till it is removed automatically once complete). The primary use of this queueing mechanism is to facilitate "gapless" transitions among playback of media files.

Gapless Playback

To playback multiple sources without any audible gaps, SourceGroup is provided. A SourceGroup can include media sources with identical audio or video format. 

First, create an instance of SourceGroup; after that, add all preferred additional sources with the add() method. Afterward, you can queue the SourceGroup on a Player as it was a single source.

Incorporating Video

When the player is playing back a source with video format, use the texture property to achieve the video frame image. This can be used to display the recent video image synchronized with the audio track.

The texture is an example of a pyglet.image.Texture, with an internal format of either the GL_TEXTURE_RECTANGLE_ARB or GL_TEXTURE_2D. While the texture will generally be created only once and subsequently updated each frame, you have to make no such assumption in your application – future versions of pyglet may also use a couple of texture objects.

Positional Audio

Pyglet consists of features for positioning sound inside a 3-D space. However, this is specifically effective with a surround-sound setup and relevant to stereo systems. A Player in pyglet has an associated role in 3-D space; it is equal to an OpenAL "source." The properties for setting those parameters are defined in more detail in the API documentation, for example, position and pitch.

Ticking the Clock

If you're using pyglet's media libraries outside of a pyglet app, you may need to apply a few kinds of loops to tick the pyglet clock periodically (possibly every 200ms or so); otherwise, only the primary small sample of media will be played:


Frequently Asked Questions

Why use Pyglet?

Pyglet is a powerful and simple to use a library for developing visually rich GUI packages like games, multimedia, and many more on Windows, Linux, and Mac OS.

What are the advantages of Pyglet?

Pyglet has the advantage of multi-monitor desktops and multiple windows. It can load images, music, sound, and video in nearly all formats. It permits us to apply it for both industrial and different open-source projects. 

Which is better, pygame or pyglet?

Speed-wise, Pyglet is faster than pygame out-of-the-box, and speed is usually a concern while developing with pygame (you need to update the smallest parts of the screen, and remembering what has been modified can be tedious).


In this article, we have extensively discussed the topic of Sound and Video in the Pyglet and its components in detail. We hope that this blog has helped you enhance your knowledge regarding the subject of Sound and Video in the Pyglet and if you would like to learn more, check out our articles on Web Applications. We hope this article has helped you understand the Sound and Video in the Pyglet. Still, the knowledge never stops, have a look at more related articles: PygletPygame, and many more. Do upvote our blog to help other ninjas grow. 

A ninja never stops learning, so to feed your quest to learn and become more advanced and skilled, head over to our practice platform Coding Ninjas Studio to practice advanced-level problems. Attempt mock tests, read interview experiencesinterview bundles, and much more! You can refer to programming fundamentals and SQL practice questions to sharpen your skills. If you need guidance at any stage while learning, then you can go to our Guided path.

Thank you for reading. 

Keep Learning and Keep improving.

Previous article
Images in Pyglet
Next article
Application Resources in Pyglet
Live masterclass