#!/usr/bin/python # # RandFlacPlay.py # Author: G. Forrest Cook # Web Site: http://www.solorb.com/ # Version: 31 July 2011 # License: Gnu General Public License V3 # # Randomly play .flac audio files from a directory tree. # Usage: RandFlacPlay.py [-d musicdir] [-n count] [-c copysetdir] [-i] # # Option Explanations: # -d directory defines the top level directory of the flac library # -n number count plays a specified number of files, then exits. # -c directory copysetdir copies random flac files to the specified directory # -t directory copies the play file to directory to handle slow source media # -i plays infinitely, disables checking for already played files # # Does not support flac filenames with embedded white space. # import sys, os, random, time, string flac_player = "ogg123 -q " cp_cmd = "/bin/cp " ls_cmd = "/bin/ls " rm_cmd = "/bin/rm " # # Randomly play one flac file from a list. # The sleep at the end gives time to catch the second control-C and exit, # somewhat kludgey but it works. # def process_one_flac (): flacnum = random.randint(0, len(flac_list)-1) play_file = flac_list[flacnum] if (flac_list[flacnum] != "played"): print play_file # # Regular play mode # if ((tmpcopy == 0) & (copyset == 0)): cmd = flac_player + play_file #print cmd os.system (cmd) time.sleep (1) if (infinite == 0): flac_list[flacnum] = "played" # # copy mode or /tmp play mode # else: if (copyset == 1): cmd = cp_cmd + play_file + " " + copysetdir os.system (cmd) elif (tmpcopy == 1): song_parts = string.split (play_file, '/') song_name = song_parts [len (song_parts) -1] cmd = cp_cmd + play_file + " " + tmpdir + "/" os.system (cmd) cmd = flac_player + tmpdir + "/" + song_name os.system (cmd) time.sleep (1) cmd = rm_cmd + tmpdir + "/" + song_name + " &" os.system (cmd) return 1 else: return 0 print print "Ctrl-C once for next song, twice to exit." print random.seed () # # Decipher the command line arguments, validity checking could be improved. # dirname = "." number = 0 copyset = 0 copysetdir = "" infinite = 0 tmpcopy = 0 tmpdir = "." for i in range (len(sys.argv)): if sys.argv[i].find("-d") >= 0: dirname = (sys.argv[i+1]) if sys.argv[i].find("-n") >= 0: number = int(sys.argv[i+1]) if sys.argv[i].find("-c") >= 0: copyset = 1 copysetdir = (sys.argv[i+1]) if sys.argv[i].find("-t") >= 0: tmpcopy = 1 tmpdir = (sys.argv[i+1]) # Clean up any leftover flacs in the tmpdir. cmd = rm_cmd + tmpdir + "/*.flac > /dev/null 2>&1" os.system (cmd) if sys.argv[i].find("-i") >= 0: infinite = 1 print "Cataloging flac files in directory: %s" % (dirname) current_dir = dirname cmd = ls_cmd + "-1RL " + dirname ls_fd = os.popen (cmd) flac_list = [] # # Blast through the ls output, build a list of full path flac file names, # ignore everything else. # for ls_line in ls_fd.readlines (): if ls_line[0] == '/': current_dir = ls_line[0:len(ls_line)-2] + '/' if ls_line[-6:-1] == ".flac": flac_file = ls_line[0:len(ls_line)-1] flac_list.append (current_dir + flac_file) if (len(flac_list) == 0): print ("No songs found, exiting.") sys.exit (1) if (number > len(flac_list)): print ("-n argument exceeds number of available songs, exiting.") sys.exit (1) print "Found", len(flac_list), "flac files, starting random play." # # Play randomly through a list of random songs. # if in count mode, exit after playing the count. # if in infinite mode, play forever and ignore duplicate songs. # if (number == 0): rand_count = 0 while 1: rv = process_one_flac () if (rv == 1): rand_count = rand_count+1 if (infinite == 0 and rand_count >= len(flac_list)): print ("All songs played, exiting.") sys.exit (0) else: while (number > 0): rv = process_one_flac () if (rv == 1): number = number-1 sys.exit (0)