Using Gstreamer with Python2.6 in Windows XP – Pt. 3 Playing Sound Files – Which Formats Work?

(Original post from 30. Oct. 2011)

Ok, so, the last time, I managed to write a very minimalistic script that gave off a static noise and nothing more. This time, I want to use the same core script, but manage to play a music file and while doing that test the waters for format compatibility. With open source libraries, there is always an issue with mp3, so I was not too hopeful.
So, here goes. This time, I will try to use the current GStreamer documentation (http://pygstdocs.berlios.de/pygst-tutorial/playbin.html ) and extract the basic functionality from the gtk clutter. What I came up with was the following:

import os
import urllib

import pygst
pygst.require("0.10")
import gst
import gobject
gobject.threads_init()

p = os.path.join("My", "path", "to", "my", "file")
path = urllib.pathname2url(p)

player = gst.element_factory_make("playbin2", "player")
fakesink = gst.element_factory_make("fakesink", "fakesink")
player.set_property("video-sink", fakesink)

player.set_property("uri", "file:" + path)
player.set_state(gst.STATE_PLAYING)

gobject.MainLoop().run()

Two things that have to be said about the above code.

[h3]1. Why urllib?[/h3]
I had a problem, feeding gstreamer a proper uri and I always had a horrible error

0:00:00.109375000 1472 00A55488 ERROR baesrc gstbasesrc.c:2974:gst_base_src_activate_pull:(source) Failed to start in pull mode

Converting the path to a url seemed to do the trick. I am assuming this has something to do with the way Windows marks paths as opposed to Linux.

[h3]2. Playbin2 – What’s that?[/h3]
Yea, that is from the tutorial. Apparently, just throw anything in and it tries to play it. If you want to only play Audio, you need to blank out the videosink with an in-build fakesink (as above).

Now when testing it, I checked the formats .ogg, .wav, and .mp3. Unfortunately, as I suspected, while ogg and wav work very well, mp3s are not played by gstreamer. There is Fluendo (http://www.fluendo.com/shop/product/fluendo-mp3-decoder/) but that does not supply for any Windows Versions… unfortunately. If anyone, finds a way to use gstreamer for mp3s, please let me know.

As an alternative, I found mp3play ( http://code.google.com/p/mp3play/ ) , which looks very good and seems to be able to do all the things I want it to do.

The last thing I wanted to test was, whether gstreamer can play more than one file at the same time. This is essential to what I am trying to achieve in the long run. Building on the above code, I just repeated what I did and created two instances. This worked as wanted. If I have any problems later on, i am sure, I will find out in the process.

import os
import urllib

import pygst
pygst.require("0.10")
import gst
import gobject
gobject.threads_init()

# First audio
p1 = os.path.join("My", "path", "to", "my", "file")
path1 = urllib.pathname2url(p1)

player1 = gst.element_factory_make("playbin2", "player1")
fakesink1 = gst.element_factory_make("fakesink", "fakesink")
player1.set_property("video-sink", fakesink1)

player1.set_property("uri", "file:" + path1)
player1.set_state(gst.STATE_PLAYING)

# Second audio
p2 = os.path.join("My", "path", "to", "my", "other", "file")
path2 = urllib.pathname2url(p2)

player2 = gst.element_factory_make("playbin2", "player2")
fakesink2 = gst.element_factory_make("fakesink", "fakesink")
player2.set_property("video-sink", fakesink2)

player2.set_property("uri", "file:" + path2)
player2.set_state(gst.STATE_PLAYING)

gobject.MainLoop().run()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s