Skip to content

Instantly share code, notes, and snippets.

@fossfreedom
Created June 1, 2014 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fossfreedom/37ee44971e9f4950e561 to your computer and use it in GitHub Desktop.
Save fossfreedom/37ee44971e9f4950e561 to your computer and use it in GitHub Desktop.
example of obtaining tag data from audio files using gstreamer
#!/usr/bin/env python
import os
import sys
from gi.repository import Gst as gst
from gi.repository import GObject as 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
gst.init_check(None)
self.pbin = gst.ElementFactory.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():
for x in range(taglist.n_tags()):
name = taglist.nth_tag_name(x)
print(' %s: %s' % (name, taglist.get_string(name)[1]))
if name == "date":
datetrue, date = taglist.get_date(name)
if datetrue:
print "this is the year of the track: %d" % date.get_year()
print date.get_month()
print date.get_day()
if name == "datetime":
datetrue, datetime = taglist.get_date_time(name)
if datetrue and datetime.has_year():
print datetime.to_iso8601_string()
print datetime.get_year()
if taglist.get_string(name)[0]:
self.file_tags[name] = taglist.get_string(name)[1]
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment