#!/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
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(os.sep)
if self.endmime[0] == "audio":
self.biblio[self.autoid] = root+os.sep+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.create_pipeline()
def create_pipeline(self):
n = self.numeros[0]
self.song = self.biblio[n].split(os.sep)
print 'Reproduciendo: '+self.song[-1]
cdsrc = 'filesrc location="%s" name=file ! decodebin ! volume name=volume ! alsasink' % (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.nexts)
self.pipeline.set_state(gst.STATE_PLAYING)
self.loop = gobject.MainLoop()
self.loop.run()
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"""
self.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:
self.tags += self.file_tags['title'] + " "
except:
self.tags += "Título desconocido."
try:
self.tags += "de: "+self.file_tags['artist']+"\n"
except:
self.tags += "Artista desconocido."
self.song = self.tags.split(os.sep)
print 'Reproduciendo', self.song[-1]
def nexts(self, w, *args):
if self.n < self.total_tracks:
self.n += 1
else:
self.n = 0
n = self.numeros[self.n]
self.song = self.biblio[n].split(os.sep)
print 'Reproduciendo: '+self.song[-1]
self.loop.quit()
self.pipeline.set_state(gst.STATE_NULL)
self.pipeline.get_by_name('file').set_property('location', self.biblio[n])
self.pipeline.set_state(gst.STATE_PLAYING)
self.loop.run()
directory = '/media/HD/Música'
mega = list()
p = Player()