Book Image

Python Multimedia

By : Ninad Sathaye
Book Image

Python Multimedia

By: Ninad Sathaye

Overview of this book

Multimedia applications are used by a range of industries to enhance the visual appeal of a product. This book will teach the reader how to perform multimedia processing using Python. This step-by-step guide gives you hands-on experience for developing exciting multimedia applications using Python. This book will help you to build applications for processing images, creating 2D animations and processing audio and video. Writing applications that work with images, videos, and other sensory effects is great. Not every application gets to make full use of audio/visual effects, but a certain amount of multimedia makes any application a lot more appealing. There are numerous multimedia libraries for which Python bindings are available. These libraries enable working with different kinds of media, such as images, audio, video, games, and so on. This book introduces the reader to the most widely used open source libraries through several exciting, real world projects. Popular multimedia frameworks and libraries such as GStreamer,Pyglet, QT Phonon, and Python Imaging library are used to develop various multimedia applications.
Table of Contents (13 chapters)
Python Multimedia Beginner's Guide
Credits
About the Author
About the Reviewers
Preface

Time for action – a simple application using PyGame


This example will make use of the modules we just discussed. For this application to work, you will need to install PyGame. The binary and source distribution of PyGame is available on Pygame's website.

  1. Create a new Python source file and write the following code in it.

    1  import pygame
    2  import sys
    3  
    4  pygame.init()
    5  bgcolor = (200, 200, 100)
    6  surf = pygame.display.set_mode((400,400))
    7
    8  circle_color = (0, 255, 255)
    9  x, y = 200, 300
    10 circle_rad = 50
    11 
    12 pygame.display.set_caption("My Pygame Window")
    13
    14 while True:
    15     for event in pygame.event.get():
    16         if event.type == pygame.QUIT:
    17             sys.exit()        
    18         elif event.type == pygame.KEYDOWN:            
    19             if event.key == pygame.K_UP:
    20                 y -= 10
    21             elif event.key == pygame.K_DOWN:
    22                 y += 10
    23             elif event.key == pygame.K_RIGHT:
    24                 x += 10
    25             elif event.key == pygame.K_LEFT:
    26                 x -= 10
    27                 
    28     circle_pos = (x, y)            
    29 	
    30     surf.fill(bgcolor)
    31     pygame.draw.circle(surf, circle_color , 
    32                        circle_pos , circle_rad)
    33     pygame.display.flip()
  2. The first line imports the pygame package. On line 4, the modules within this pygame package are initialized. An instance of class Surface is created using display.set_mode method. This is the main PyGame window inside which the images will be drawn. To ensure that this window is constantly displayed on the screen, we need to add a while loop that will run forever, until the window is closed by the user. In this simple application everything we need is placed inside the while loop. The background color of the PyGame window represented by object surf is set on line 30.

  3. A circular shape is drawn in the PyGame surface by the code on line 31. The arguments to draw.circle are (Surface, color, position, radius) . This creates a circle at the position specified by the argument circle_pos. The instance of class Surface is sent as the first argument to this method.

  4. The code block 16-26 captures certain events. An event occurs when, for instance, a mouse button or a key is pressed. In this example, we instruct the program to do certain things when the arrow keys are pressed. When the RIGHT arrow key is pressed, the circle is drawn with the x coordinate offset by 10 pixels to the previous position. As a result, the circle appears to be moving towards right whenever you press the RIGHT arrow key. When the PyGame window is closed, the pygame.QUIT event occurs. Here, we simply exit the application by calling sys.exit() as done on line 17.

  5. Finally, we need to ensure that everything drawn on the Surface is visible. This is accomplished by the code on line 31. If you disable this line, incompletely drawn images may appear on the screen.

  6. Execute the program from a terminal window. It will show a new graphics window containing a circular shape. If you press the arrow keys on the keyboard, the circle will move in the direction indicated by the arrow key. The next illustration shows the screenshot of the original circle position (left) and when it is moved using the UP and RIGHT arrow keys.

    A simple PyGame application with a circle drawn within the Surface (window). The image on the right side is a screenshot taken after maneuvering the position of the circle with the help of arrow keys:

What just happened?

We used PyGame to create a simple user interactive application. The purpose of this example was to introduce some of the basic concepts behind animation and game programming. It was just a preview of what is coming next! Later in this book we will use Pyglet framework to create some interesting 2D animations.

QT Phonon

When one thinks of a media player, it is almost always associated with a graphical user interface. Of course one can work with command-line multimedia players. But a media player with a GUI is a clear winner as it provides an easy to use, intuitive user interface to stream a media and control its playback. The next screenshot shows the user interface of an audio player developed using QT Phonon.

An Audio Player application developed with QT Phonon:

QT is an open source GUI framework. 'Phonon' is a multimedia package within QT that supports audio and video playback. Note that, Phonon is meant for simple media player functionality. For complex audio/video player functionality, you should use multimedia frameworks like GStreamer. Phonon depends on a platform-specific backend for media processing. For example, on Windows platform the backend framework is DirectShow. The supported functionality may vary depending on the platform.

To develop a media processing application, a media graph is created in Phonon. This media graph contains various interlinked media nodes. Each media node does a portion of media processing. For example, an effects node will add an audio effect, such as echo to the media. Another node will be responsible for outputting the media from an audio or video device and so on. In chapter 8, we will develop audio and video player applications using Phonon framework. The next illustration shows a video player streaming a video. It is developed using QT Phonon. We will be developing this application in Chapter 8.

Using various built-in modules of QT Phonon, it is very easy to create GUI-based audio and video players. This example shows a video player in action:

Other multimedia libraries

Python bindings for several other multimedia libraries are available on various platforms. Some of the popular libraries are mentioned below.

Snack Sound Toolkit

Snack is an audio toolkit that is used to create cross-platform audio applications. It includes audio analysis and input-output functionality and it has support for audio visualization as well. The official website for Snack Sound Toolkit is http://www.speech.kth.se/snack/.

PyAudiere

PyAudiere (http://pyaudiere.org/) is an open source audio library. It provides an API to easily implement the audio functionality in various applications. It is based on Audiere Sound Library.