Archive for the 'files' Category

03
Mar
09

script for associating a subtitle to a movie file


Lately, I had to deal with a lot of subtitles and movie files. As far as I know, in order for a movie player to play a file and load subtitles, the name of the movie file and the name of the subtitle must be identical, minus the extensions. So, if I have a file named “Some movie.avi”, the subtitle file for it should be named “Some movie.srt” or “Some movie.sub”. Here is the code for the script that renames the subtitles to the name of the files:


#!/usr/bin/python

import os,sys,list_ops,futils,re,pdb

def tuple_sort(a,b):
	first = a[1]
	second = b[1]
	return cmp(first,second)

dir = sys.argv[1]
movie_ext = [".avi",".mpg",".mpeg"]
srt_ext = [".srt",".sub"]

os.chdir(dir)

srt_files = futils.listdir_by_extensions(dir,srt_ext)
movie_files = futils.listdir_by_extensions(dir,movie_ext)
split_on = "\.|,|_| "

for file in srt_files:	
	srt_components = re.split(split_on,file)
	movie = []	
	for movie_file in movie_files:
		movie_components = re.split(split_on,movie_file)		
		same = list_ops.same(srt_components,movie_components,case_sensitive=True)		
		number = len(same)
		movie.append((movie_file,number))		
	movie.sort(tuple_sort)
	best = movie[-1][0]
	ren_file,ext = os.path.splitext(best)
	ren_file += ".srt"
	os.rename(file,ren_file)
	print "renaming %s for %s to %s" %(file,best,ren_file)

Here is the code for list_ops


def uniq(array):
	""" returns an array containing unique elements from an array """
	n_array = []
	for item in array:
		if not item in n_array:
			n_array.append(item)
	return n_array

def same(array1,array2,case_sensitive=False):
	""" returns elements that exist in both arrays """	
	elements = []
	if not case_sensitive:	
		for item in array1:
			if item in array2:
				elements.append(item)
	else:
		ins_array1 = map(str.lower,array1)
		ins_array2 = map(str.lower,array2)
		for item in ins_array1:
			if item in ins_array2:
				elements.append(item)
	return elements

def selfish(array1,array2):
	""" returns elements that exist only in the first array """	
	elements = []
	for item in array1:
		if item not in array2:
			elements.append(item)
	return elements

and the code for futils:


import os,pdb

def listdir_by_extensions(dir,extensions):
	""" returns a list of files that end with the following extensions """	
	results = []
	files = os.listdir(dir)
	for file in files:
		extension = os.path.splitext(file)[1]
		if extension != "":		
			if extension in extensions:
				results.append(file)
	return results
		

It seems to work for the files I tested it on, so, until proven otherwise, I consider it functional 🙂

Advertisements
03
Mar
09

python recursive directory walker


I don’t like python’s os.walk, so every time I need to do some work with a lot of files/directories, I end up writing my own ( because I don’t save the code ). So, this time, I decided I’ll post the code here so that I don’t have to rewrite every time :


import os,sys

class DirWalker(object):

	def walk(self,dir,meth):
      """ walks a directory, and executes a callback on each file """
		dir = os.path.abspath(dir)
		for file in [file for file in os.listdir(dir) if not file in [".",".."]]:
			nfile = os.path.join(dir,file)
			meth(nfile)
			if os.path.isdir(nfile):
				self.walk(nfile,meth)

It’s simple, and gets the job done.




Blog Stats

  • 229,119 hits