Skip to content

Instantly share code, notes, and snippets.

@son-link
Created December 11, 2011 15:41
Show Gist options
  • Save son-link/1461181 to your computer and use it in GitHub Desktop.
Save son-link/1461181 to your computer and use it in GitHub Desktop.
Random music player
#!/usr/bin/env python2
#-*- coding: UTF-8 -*-
import os, gobject
import pygst
pygst.require("0.10")
import gst
from os.path import join, getsize
from mimetypes import guess_type
from random import shuffle
from sys import stdout
gobject.threads_init()
class AllFromDirectory():
def __init__(self, directory):
self.directory = directory
self.biblio = dict()
self.autoid = 0
def get(self):
for root, dirs, files in os.walk(self.directory):
for self.filen in files:
self.mime = guess_type(self.filen)
self.auxmime = str(self.mime[0])
self.endmime = self.auxmime.split('/')
if self.endmime[0] == "audio":
#self.mega.append(self.filen)
self.biblio[self.autoid] = root+'/'+self.filen
self.autoid = self.autoid + 1
return self.biblio
def getLen(self):
return len(self.biblio)
class Player():
def __init__(self):
# Obtenemos la lista de archivos
x = AllFromDirectory(directory)
self.biblio = x.get()
self.total_tracks = x.getLen()
self.numeros = range(0, int(self.total_tracks))
shuffle(self.numeros)
self.n = 0
self.block_tags = False
self.create_pipeline()
def create_pipeline(self):
n = self.numeros[0]
cdsrc = 'filesrc name=src location="%s" ! decodebin name=decode ! audioconvert name=convert ! alsasink name=sink ' % (self.biblio[n])
self.pipeline = gst.parse_launch(cdsrc)
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message::tag", self.bus_message_tag)
bus.connect("message::error", self.bus_message_error)
bus.connect("message::eos", self.next)
self.pipeline.set_state(gst.STATE_PLAYING)
def bus_message_error(self, bus, message):
e, d = message.parse_error()
print "ERROR:", e
#exit(1)
def bus_message_tag(self, bus, message):
"""Esta es la función encargada de recoger los datos del bus de Gstreamer, principalmente los tags de los ficheros de audio"""
#print 'tags'
if not self.block_tags:
self.block_tags = True
tags = ''
self.file_tags = {}
#we received a tag message
taglist = message.parse_tag()
#put the keys in the dictionary
for key in taglist.keys():
try:
self.file_tags[key] = taglist[key]
except:
return False
try:
tags += self.file_tags['title'] + " "
except:
tags += "Titulo desconocido "
try:
tags += "de: "+self.file_tags['artist']
except:
tags += "de: artista desconocido"
stdout.write('\rReproduciendo %s' % tags)
stdout.flush()
def next(self, w, *args):
if self.n < self.total_tracks:
self.n += 1
else:
self.n = 0
n = self.numeros[self.n]
#print 'Reproduciendo: '+self.biblio[n]
self.pipeline.set_state(gst.STATE_READY)
self.pipeline.get_by_name('src').set_property('location', "%s" % self.biblio[n])
self.pipeline.get_by_name('decode').connect('new-decoded-pad', self.new_decoded_pad)
self.pipeline.set_state(gst.STATE_PLAYING)
self.block_tags = False
def new_decoded_pad(self, dbin, pad, islast):
#print 'new decoded pad', dbin, pad, islast
pad.link(self.pipeline.get_by_name('convert').get_pad("sink"))
# Cambia la ruta a la carpeta que contiene la música
directory = '/home/sonlink/Música/'
mega = list()
p = Player()
loop = gobject.MainLoop()
try:
loop.run()
except KeyboardInterrupt: # Por si se pulsa Ctrl+C
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment