Wednesday, August 13, 2008

A python script to find and play all the videos on your computer

#!/usr/bin/env python
#
# a silly script I wrote to find and play
# all the movies on my hd.
#
class FileLister:
def __init__(self, suffixes, directory=None):
self.suffixes = suffixes
if directory:
self.cwd = directory
else:
self.cwd = os.getcwd()

def list_files(self):
list = []
items = os.listdir(self.cwd)
for item in items:
for suffix in self.suffixes:
if item.endswith(suffix):
list.append(item)
return list

def change_dir(self, directory):
self.cwd = directory

def change_filter():
pass

def walk_dir(self):
movies = []
for root, dirs, files in os.walk(self.cwd):
self.change_dir(root)
list = self.list_files()
if list:
for item in list:
movies.append(os.path.join(root, item))

return movies

class MoviePlayer:
def __init__(self, movie_list, player='vlc'):
self.player =[player]
self.mlist = movie_list

def play(self):
command = self.player
for movie in self.mlist:
command.append(movie)

subprocess.call(command)

if __name__ == "__main__":
import os, subprocess, time
# replace the magic string below with the location you store your videos
dir = os.path.expanduser('/media/storage')
# you may wish to edit the list below to add filetypes you use
lister = FileLister(['.avi', '.mpg', '.flv', '.mkv'], dir)
movies = lister.walk_dir()
movies.sort()
# mplayer can be substituted with gmplayer or left out to use
# vlc by default
playme = MoviePlayer(movies, 'mplayer')
playme.play()

No comments: