Hi,
hier ein Python-Skript zum Abspielen von mp3s in zufälliger Reihenfolge in einem Verzeichnis und darunter. Es benötigt "mpg321":
http://packman.links2linux.de/package/mpg321
Kurzanleitung:
Das Python-Skript als "shuffle.py" abspeichern, "chmod +x shuffle.py" ausführen und in ein Verzeichnis mit mp3s kopieren. Dort das Skript mit "./shuffle.py" in der Konsole ausführen.
Die mp3s, die auf "mp3" enden, werden dann in zufälliger Reihenfolge abgespielt, aber jedes im Grundsatz nur einmal.
Man kann jederzeit wählen, ob man das Stück nochmal, das davor oder das danach abspielen will.
Man kann auch jederzeit ein besonderes Stück auswählen. Ansonsten: Drücke "h" für "Hilfe"
.
Ok, so ähnlich geht das auch mit "mpg321 -z" oder jedem anderen mp3-player, aber ich wollte halt noch mehr Kontrolle darüber haben und fand's so ganz bequem.
Viele Grüße
hier ein Python-Skript zum Abspielen von mp3s in zufälliger Reihenfolge in einem Verzeichnis und darunter. Es benötigt "mpg321":
http://packman.links2linux.de/package/mpg321
Kurzanleitung:
Das Python-Skript als "shuffle.py" abspeichern, "chmod +x shuffle.py" ausführen und in ein Verzeichnis mit mp3s kopieren. Dort das Skript mit "./shuffle.py" in der Konsole ausführen.
Die mp3s, die auf "mp3" enden, werden dann in zufälliger Reihenfolge abgespielt, aber jedes im Grundsatz nur einmal.
Man kann jederzeit wählen, ob man das Stück nochmal, das davor oder das danach abspielen will.
Man kann auch jederzeit ein besonderes Stück auswählen. Ansonsten: Drücke "h" für "Hilfe"
Ok, so ähnlich geht das auch mit "mpg321 -z" oder jedem anderen mp3-player, aber ich wollte halt noch mehr Kontrolle darüber haben und fand's so ganz bequem.
Code:
#!/usr/bin/env python
"""
ShufflePlayer
Searches a directory recursively for files ending with ".mp3" and
plays them as mp3s with external tool "mpg321" in random order.
Starts just on instance of "mpg321" in remote-control-mode.
Features key-input-control in terminal (press "h" for help).
Press "c" to play a certain song found.
Breaking the script with "STRG+c" may corrupt your terminal-settings;
cancel your xterm with "ALT+F4" then and start a new one.
Usually use "q" to quit.
(C) 2007 by abgdf@gmx.net, License: LGPL2.
"""
import os
import sys
import random
import tty
import termios
import fcntl
import time
import subprocess
class ShufflePlayer:
def __init__(self):
self.mp3list = self.getMp3List()
self.nrmp3s = len(self.mp3list)
if self.nrmp3s == 0:
print
print "No mp3s found in this directory."
print
sys.exit(1)
random.seed()
self.rorder = []
for i in range(self.nrmp3s):
self.rorder.append(i)
random.shuffle(self.rorder)
self.current = 0
# Needed for self.checkKey():
self.fd = sys.stdin.fileno()
self.oldterm = termios.tcgetattr(self.fd)
self.oldflags = fcntl.fcntl(self.fd, fcntl.F_GETFL)
tty.setcbreak(sys.stdin.fileno())
self.newattr = termios.tcgetattr(self.fd)
self.newattr[3] = self.newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(self.fd, termios.TCSANOW, self.newattr)
fcntl.fcntl(self.fd, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK)
# Start "mpg321" in remote-control-mode:
self.player = subprocess.Popen(["mpg321", "-R", "abc"], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, bufsize = 0, close_fds = True)
print
print "ShufflePlayer: Press \"h\" for help."
print
self.playShuffle()
def oldTerminalSettings(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.oldterm)
fcntl.fcntl(self.fd, fcntl.F_SETFL, self.oldflags)
def newTerminalSettings(self):
# tty.setcbreak(sys.stdin.fileno())
termios.tcsetattr(self.fd, termios.TCSANOW, self.newattr)
fcntl.fcntl(self.fd, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK)
def checkKey(self):
try:
c = sys.stdin.read(1)
return ord(c)
except IOError:
return 0
except KeyboardInterrupt:
self.oldTerminalSettings()
self.player.stdin.write("QUIT\n")
sys.exit(1)
def getMp3List(self):
# Get the names of all files in and
# under a directory ending with "mp3".
a = []
for root, dirs, files in os.walk(os.getcwd()):
for name in files:
b = os.path.join(root, name)
if b.endswith(".mp3"):
a.append(b)
# Now we have a list of the names of all ".mp3"-files. We sort
# them only by filenames, not by directory-names. But in the end,
# we return a list containing their exact locations:
b = []
for i in a:
b.append(self.cutDir(i))
b.sort()
c = []
for i in b:
for u in a:
if i in u:
c.append(u)
a.remove(u)
break
return c
def playShuffle(self):
while (self.current < self.nrmp3s):
song = self.mp3list[self.rorder[self.current]]
self.player.stdin.write("LOAD " + song + "\n")
print "Playing: " + self.cutDir(song)
e = "a"
while e[0:4] != "@P 0":
e = self.player.stdout.readline()
key = self.checkKey()
if key != 0:
key = chr(key)
if key == " ":
self.player.stdin.write("PAUSE\n")
self.oldTerminalSettings()
raw_input("Pause. Press Return to continue: ")
self.newTerminalSettings()
self.player.stdin.write("PAUSE\n")
if key == "h":
self.showHelp()
if key == "q":
self.oldTerminalSettings()
self.player.stdin.write("QUIT\n")
sys.exit(0)
if key == "r":
self.current -= 1
break
if key == "n":
break
if key == "p":
self.current -= 2
break
if key == "c":
self.player.stdin.write("PAUSE\n")
self.playCertainSong()
break
self.current += 1
if self.current < 0:
self.current = self.nrmp3s - 1
print "\nFinished. All songs played.\n"
self.player.stdin.write("QUIT\n")
self.oldTerminalSettings()
def playCertainSong(self):
self.oldTerminalSettings()
print "\nPlay certain song:\n"
for i in range(self.nrmp3s):
print str(i + 1) + ". " + self.cutDir(self.mp3list[i])
print
sn = ""
while not sn.isdigit():
sn = raw_input('Song to play (q to quit) ? ')
if sn == "q":
self.newTerminalSettings()
self.current -= 1
print
return
if sn.isdigit():
if sn == "0" or int(sn) > self.nrmp3s:
sn = ""
sn = int(sn) - 1
print
print "Playing certain song: " + self.cutDir(self.mp3list[sn])
print
print "Press \"s\" to continue shuffling."
print
self.player.stdin.write("LOAD " + self.mp3list[sn] + "\n")
e = "a"
self.newTerminalSettings()
while e[0:4] != "@P 0":
e = self.player.stdout.readline()
key = self.checkKey()
if key != 0:
key = chr(key)
if key == "s":
break
if key == "q":
self.oldTerminalSettings()
self.player.stdin.write("QUIT\n")
sys.exit(0)
def cutDir(self, a):
a = a.split("/").pop()
return a
def showHelp(self):
print
print "ShufflePlayer:"
print
print "Options at startup are:"
print "-r, --repeat\tShuffle endlessly."
print
print "Available keys are:"
print "n\t\tNext song."
print "p\t\tPrevious song."
print "c\t\tCertain song."
print "SPACE\t\tPause."
print "h\t\tShow (this) help."
print "q\t\tQuit ShufflePlayer."
print
def main():
if len(sys.argv) < 2:
ShufflePlayer()
else:
if sys.argv[1] == "-r" or sys.argv[1] == "--repeat":
while 1:
ShufflePlayer()
else:
ShufflePlayer()
if __name__ == "__main__":
main()