2008-05-23
When creating audio file playing software, it is nice to be able to access the tags of the audio files for display to the user. Since I am planning to use gstreamer to handle the playing of audio in my Luma project, I thought it would be best to make a very simple test app to handle the retrieval of tags from an audio file. Actually, I still need to test this with the various filetypes that gstreamer supports; specifically vorbis files. Anyway, on with the show.

#!/usr/bin/env python
import os
import sys
import gst
import gobject

class tag_getter:
def __init__(self):
#make a dictionary to hold our tag info
self.file_tags = {}
#make a playbin to parse the audio file
self.pbin = gst.element_factory_make("playbin")
#we need to receive signals from the playbin's bus
self.bus = self.pbin.get_bus()
#make sure we are watching the signals on the bus
self.bus.add_signal_watch()
#what do we do when a tag is part of the bus signal?
self.bus.connect("message::tag", self.bus_message_tag)
#create a loop to control our app
self.mainloop = gobject.MainLoop()

def bus_message_tag (self, bus, message):
#we received a tag message
taglist = message.parse_tag()
#put the keys in the dictionary
for key in taglist.keys():
self.file_tags[key] = taglist[key]
#for this test, if we have the artist tag, we can quit
if self.file_tags['artist']:
print self.file_tags
sys.exit()

def set_file(self,file):
#set the uri of the playbin to our audio file
self.pbin.set_property("uri","file://"+file)
#pause the playbin, we don't really need to play
self.pbin.set_state(gst.STATE_PAUSED)

def run(self):
#start the main loop
self.mainloop.run()

if __name__=="__main__":
if len(sys.argv)>1:
file = sys.argv[1]
pwd = os.getcwd()
filepath = os.path.join(pwd,file)
getter = tag_getter()
getter.set_file(file)
getter.run()

else:
print "select an audio file"


Useful? Not really, but it certainly is a good building block for a more advanced application.
Comments
2010-10-10 Ryan:
THANK YOU SO MUCH! This made it so much easier to figure out how to get tags through gstreamer. I've searched for forever trying to figure this out and this helped me figure it out in 5 minutes. Thanks again. Keep up the good work.
2010-10-10 jezra:
You are very welcome.
2011-03-25 Sam Brookfield:
Again, thanks, really helpful
2011-03-25 Sam Brookfield:
For anyone interested, the image tag is just a binary image - you can write it to a file -
if key == 'image':
img = open('temp.png', 'w')
img.write(taglist[key])
img.close()
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
5 plus 9
Answer:
required
  • Tags:
  • Python
  • Gstreamer
subscribe
 
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008