#!/usr/bin/env python # # Random video player # This works by creating a list of all video files in the current dir recursively # It then maintains a list of all files played. # Removes all the files that have been played from the first list, shuffles it # and takes a couple off the top. # # 01/07/2007 # # Andrew Pangborn # Richard Whitty import random,os from optparse import OptionParser extensions = ["avi","mpeg","mpg","wmv","rm"] # vob? # thse set the program to run in fullscreen and quit afterwards xine_command = "xine -p=f,q %s" mplayer_command = 'mplayer -fs -framedrop -really-quiet "%s"' # name of the file to store the list of what's been watched in viewed_filename = "viewed.txt" # How many videos to play number_to_play = 1 # directory to run from directory = "." # dictionary player_commands = {"mplayer":mplayer_command, "xine":xine_command} # The files that have already been watched viewed = [] verbose = False listonly = False command = "" def parse_args() : global command global number_to_play global directory global verbose global listonly parser = OptionParser() parser.add_option("--number","-n", dest='number',default=1, help="The number of video files to play") parser.add_option("--directory","-d", dest='directory',default=".", help="Directory to look for video files") parser.add_option("--player","-p", action='store',type='choice',dest='player',default="xine", help="Media player to use",choices=["mplayer","xine"]) parser.add_option('--verbose','-v',dest='verbose',default=False,action="store_true",help="Verbose, displays playlist") parser.add_option('--listonly','-l',dest='listonly',default=False,action="store_true",help="Lists the playlist so it can be stuck in some other command.") options,arguments = parser.parse_args() command = player_commands[options.player] verbose = options.verbose listonly = options.listonly number_to_play = int(options.number) directory = options.directory return # Walks the directory tree... def append_files(list,path,names): file_listing = os.listdir(path) for file in file_listing: extension = file[file.rfind('.')+1:] # traverse symlinks if os.path.islink(path+os.sep+file) : os.path.walk(path+os.sep+file,append_files,list) elif extension and os.path.isfile(path+os.sep+file) and extension in extensions: list.append(path+os.sep+file) return list # Populates the list of files we've already watched from the textfile def read_viewed_files() : list = [] # already initialised try : file = open(viewed_filename,'r') viewed_lines = file.readlines() for view in viewed_lines: view = view.strip(os.linesep) list.append(view) file.close() except IOError: pass return list # adds a filename to the viewed file def add_to_viewed(filename) : viewed_file = open(viewed_filename,'a') viewed_file.write(filename+os.linesep) viewed_file.close() return # Plays the videos contained in the list, and adds them to the viewed file. def play_videos(videos_to_play): #print videos_to_play files = "" for f in videos_to_play: files += " \"" + f + "\" " #thiscmd = command % f #print "running " + thiscmd #os.system(thiscmd) add_to_viewed(f) os.system(command % files); return # does it a different way def shuffle(list,swaps) : for i in range(swaps) : index1 = random.randint(0,len(list)-1) index2 = random.randint(0,len(list)-1) foo = list[index1] list[index1] = list[index2] list[index2] = foo return list # main function def main(): global viewed parse_args() file_list = [] os.path.walk(directory,append_files,file_list) viewed = read_viewed_files() if len(file_list) == 0 : print "ERROR: No playable files found in directory" return 1 elif len(file_list) - len(viewed) < number_to_play : os.remove("viewed.txt") viewed = read_viewed_files() print "Resetting viewed list..." unplayed_files = [name for name in file_list if name not in viewed] shuffle(unplayed_files,len(unplayed_files)*8) if listonly : s=" " for i in range(number_to_play) : s=s+'"' + unplayed_files[i] +'" ' print s elif verbose : print "\n*****************Here's the playlist:\n" for i in range(number_to_play) : print "\t"+str(i+1) + ") " + unplayed_files[i] else : play_videos(unplayed_files[0:number_to_play]) return 0 if __name__ == "__main__": main()