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

0 Responses to “script for associating a subtitle to a movie file”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 221,399 hits

%d bloggers like this: