Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/get_title2.py
blob: f1370ae338d54f611046ab44aeef7d6a6a850c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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]
            print 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"