packages: subconv/subconv.py - reformat

arekm arekm at pld-linux.org
Sat Feb 20 23:05:05 CET 2010


Author: arekm                        Date: Sat Feb 20 22:05:05 2010 GMT
Module: packages                      Tag: HEAD
---- Log message:
- reformat

---- Files affected:
packages/subconv:
   subconv.py (1.4 -> 1.5) 

---- Diffs:

================================================================
Index: packages/subconv/subconv.py
diff -u packages/subconv/subconv.py:1.4 packages/subconv/subconv.py:1.5
--- packages/subconv/subconv.py:1.4	Sat Feb 20 22:52:02 2010
+++ packages/subconv/subconv.py	Sat Feb 20 23:05:00 2010
@@ -11,13 +11,13 @@
 import re, sys, getopt, string
 
 def usage():
-	sys.stderr.write("""
+    sys.stderr.write("""
  subconv v0.2.2 -- DivX subtitles converter
  (w)by Pawel Stolowski <yogin at linux.bydg.org>
        Julien Lerouge  <julien.lerouge at free.fr>
 
  Usage: subconv [-i fmt|-o fmt|-a sec|-s sec|-S h:m:s[,h:m:s,...]] input [output1, output2, ...]
-	
+
      -i fmt        input format (one of: srt, tmp, mdvd, sub2, mpl2, auto; auto by default)
      -o fmt        output format (one of: tmp, srt; srt by default)
      -f fps        adjust fps rate for microdvd input subtitles (auto by default)
@@ -26,334 +26,334 @@
      -S h:m:s,...  split subtitles in selected position(s); additional output file names must be specified
      -h            this help
 
-""")
+     """)
 
 
 
 def detect_fps(list):
-	"""
-	Detect the FPS for a given input file
-	input: contents of a file as list
-	returns: FPS
-	"""
-	sys.stderr.write("FPS guessing, here are approximate length of file for several FPS :\n")
-	most_current=[23.976,25.0,29.97]
-	
-	re_mdvd = re.compile("^[\{\[](\d+)[\}\]][\{\[](\d*)[\}\]]\s*(.*)")
-	count = len(list) - 1
-	m = re_mdvd.match(list[count])
-	while not m:
-		count = count - 1
-		m = re_mdvd.match(list[count])
-	last = int(m.group(2))
-		
-	for i in range(0,len(most_current)):
-		sys.stderr.write(str(i)+" "+str(most_current[i])+" Fps -> ")
-		tot_sec = int(last / most_current[i])
-		min = tot_sec / 60
-		sec = tot_sec % 60
-		sys.stderr.write(str(min)+" min "+str(sec)+"sec\n")
-	sys.stderr.write("Choice : ")
-	choice=int(sys.stdin.readline().strip())
-	if choice>=0 and choice<len(most_current):
-		return most_current[choice]
-	else:
-		sys.stderr.write("Bad choice\n")
-		sys.exit(1)
+    """
+    Detect the FPS for a given input file
+    input: contents of a file as list
+    returns: FPS
+    """
+    sys.stderr.write("FPS guessing, here are approximate length of file for several FPS :\n")
+    most_current=[23.976,25.0,29.97]
+
+    re_mdvd = re.compile("^[\{\[](\d+)[\}\]][\{\[](\d*)[\}\]]\s*(.*)")
+    count = len(list) - 1
+    m = re_mdvd.match(list[count])
+    while not m:
+        count = count - 1
+        m = re_mdvd.match(list[count])
+    last = int(m.group(2))
+
+    for i in range(0,len(most_current)):
+        sys.stderr.write(str(i)+" "+str(most_current[i])+" Fps -> ")
+        tot_sec = int(last / most_current[i])
+        min = tot_sec / 60
+        sec = tot_sec % 60
+        sys.stderr.write(str(min)+" min "+str(sec)+"sec\n")
+    sys.stderr.write("Choice : ")
+    choice=int(sys.stdin.readline().strip())
+    if choice>=0 and choice<len(most_current):
+        return most_current[choice]
+    else:
+        sys.stderr.write("Bad choice\n")
+        sys.exit(1)
 
 
 def detect_format(list):
-	"""
-	Detect the format of input subtitles file.
-	input: contents of a file as list
-	returns: format (srt, tmp, mdvd) or "" if unknown
-	"""
-	sys.stderr.write("Guessing subs format .")
-	re_mdvd = re.compile("^[\{\[](\d+)[\}\]][\{\[](\d*)[\}\]]\s*(.*)")
-	re_srt = re.compile("^(\d+):(\d+):(\d+),\d+\s*-->.*")
-	re_tmp = re.compile("^(\d+):(\d+):(\d+):(.*)")
-	re_sub2 = re.compile("^(\d+):(\d+):(\d+)\.\d+\s*\,.*")
-	re_mpl2 = re.compile("^\[(\d+)\]\[(\d+)\]\s*(.*)")
-	while len(list) > 0 :
-		sys.stderr.write(".")
-		line = list.pop(0)
-		if re_mdvd.match(line):
-			sys.stderr.write(" mdvd\n")
-			return "mdvd"
-		elif re_srt.match(line):
-			sys.stderr.write(" srt\n")
-			return "srt"
-		elif re_tmp.match(line):
-			sys.stderr.write(" tmp\n")
-			return "tmp"
-		elif re_sub2.match(line):
-			sys.stderr.write(" subviewer 2 format\n")
-			return "sub2"
-		elif re_mpl2.match(line):
-			sys.stderr.write(" mpl2\n")
-			return "mpl2"			
-	return ""
+    """
+    Detect the format of input subtitles file.
+    input: contents of a file as list
+    returns: format (srt, tmp, mdvd) or "" if unknown
+    """
+    sys.stderr.write("Guessing subs format .")
+    re_mdvd = re.compile("^[\{\[](\d+)[\}\]][\{\[](\d*)[\}\]]\s*(.*)")
+    re_srt = re.compile("^(\d+):(\d+):(\d+),\d+\s*-->.*")
+    re_tmp = re.compile("^(\d+):(\d+):(\d+):(.*)")
+    re_sub2 = re.compile("^(\d+):(\d+):(\d+)\.\d+\s*\,.*")
+    re_mpl2 = re.compile("^\[(\d+)\]\[(\d+)\]\s*(.*)")
+    while len(list) > 0 :
+        sys.stderr.write(".")
+        line = list.pop(0)
+        if re_mdvd.match(line):
+            sys.stderr.write(" mdvd\n")
+            return "mdvd"
+        elif re_srt.match(line):
+            sys.stderr.write(" srt\n")
+            return "srt"
+        elif re_tmp.match(line):
+            sys.stderr.write(" tmp\n")
+            return "tmp"
+        elif re_sub2.match(line):
+            sys.stderr.write(" subviewer 2 format\n")
+            return "sub2"
+        elif re_mpl2.match(line):
+            sys.stderr.write(" mpl2\n")
+            return "mpl2"			
+    return ""
 
 
 def read_mdvd(list, fps):
-	"""
-	Read micro-dvd subtitles.
-	input: contents of a file as list
-	returns: list of subtitles in form: [[time_start in secs, time_end in secs, line1, ...],....]
-	"""
-	re1 = re.compile("^[\{\[](\d+)[\}\]][\{\[](\d*)[\}\]]\s*(.*)")
-
-	subtitles = []
-	while len(list)>0:
-		x = list.pop(0)
-		m = re1.match(x, 0)
-		if m:
-			time1 = int(m.group(1))
-			subt = [int(time1) / float(fps)]
-			time2 = m.group(2)
-			if time2 == '':
-				time2 = int(time1) + 20
-			subt.append(int(time2) / float(fps))
-			texts = m.group(3).strip().split("|")
-			for i in range(len(texts)):
-				text = texts[i]
-				if text.lower().startswith('{c:') or text.lower().startswith('{y:'):
-					end_marker = text.index('}')
-					if end_marker:
-						text = text[end_marker + 1:]
-						texts[i] = text
-			subt.extend(texts)
-			subtitles.append(subt)
-	return subtitles
+    """
+    Read micro-dvd subtitles.
+    input: contents of a file as list
+    returns: list of subtitles in form: [[time_start in secs, time_end in secs, line1, ...],....]
+    """
+    re1 = re.compile("^[\{\[](\d+)[\}\]][\{\[](\d*)[\}\]]\s*(.*)")
+
+    subtitles = []
+    while len(list)>0:
+        x = list.pop(0)
+        m = re1.match(x, 0)
+        if m:
+            time1 = int(m.group(1))
+            subt = [int(time1) / float(fps)]
+            time2 = m.group(2)
+            if time2 == '':
+                time2 = int(time1) + 20
+            subt.append(int(time2) / float(fps))
+            texts = m.group(3).strip().split("|")
+            for i in range(len(texts)):
+                text = texts[i]
+                if text.lower().startswith('{c:') or text.lower().startswith('{y:'):
+                    end_marker = text.index('}')
+                    if end_marker:
+                        text = text[end_marker + 1:]
+                        texts[i] = text
+            subt.extend(texts)
+            subtitles.append(subt)
+    return subtitles
 
 def read_mpl2(list):
-	"""
-	Read mpl2 subtitles
-	input: contents of a file as list
-	returns: list of subtitles in form: [[time_start in secs, time_end is secs, line1, ...],.....]
-	"""
-	re1 = re.compile("^\[(\d+)\]\[(\d+)\]\s*(.*)")
-	subtitles = []
-	while len(list)>0:
-		m = re1.match(list.pop(0),0);
-		if m:
-			subt = [int(m.group(1))*0.1]
-			subt.append(int(m.group(2))*0.1)
-			subt.extend(m.group(3).strip().split("|"))
-			subtitles.append(subt)
-	return subtitles
-	
+    """
+    Read mpl2 subtitles
+    input: contents of a file as list
+    returns: list of subtitles in form: [[time_start in secs, time_end is secs, line1, ...],.....]
+    """
+    re1 = re.compile("^\[(\d+)\]\[(\d+)\]\s*(.*)")
+    subtitles = []
+    while len(list)>0:
+        m = re1.match(list.pop(0),0);
+        if m:
+            subt = [int(m.group(1))*0.1]
+            subt.append(int(m.group(2))*0.1)
+            subt.extend(m.group(3).strip().split("|"))
+            subtitles.append(subt)
+    return subtitles
+
 def read_sub2(list):
-	"""
-	Reads subviewer 2.0 format subtitles, e.g. :
-		00:01:54.75,00:01:58.54
-		You shall not pass!
-	input: contents of a file as list
-	returns: list of subtitles in form: [[time_dep, time_end, line1, ...],[time_dep, time_end, line1, ...],....]
-	"""
-	re1 = re.compile("^(\d+):(\d+):(\d+)\.(\d+)\s*\,\s*(\d+):(\d+):(\d+)\.(\d+).*$")
-	subtitles = []
-	try:
-		while len(list)>0:
-			m = re1.match(list.pop(0), 0)
-			if m:
-				subt = [int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3)) + int(m.group(4))/100.0]
-				subt.append(int(m.group(5))*3600 + int(m.group(6))*60 + int(m.group(7)) + int(m.group(8))/100.0)
-				l = list.pop(0).strip()
-				lines = l.split("[br]")
-				for i in range(0,len(lines)):
-					subt.append(lines[i])
-				subtitles.append(subt)
-	except IndexError:
-		sys.stderr.write("Warning: it seems like input file is damaged or too short.\n")
-	return subtitles
+    """
+    Reads subviewer 2.0 format subtitles, e.g. :
+        00:01:54.75,00:01:58.54
+        You shall not pass!
+    input: contents of a file as list
+    returns: list of subtitles in form: [[time_dep, time_end, line1, ...],[time_dep, time_end, line1, ...],....]
+    """
+    re1 = re.compile("^(\d+):(\d+):(\d+)\.(\d+)\s*\,\s*(\d+):(\d+):(\d+)\.(\d+).*$")
+    subtitles = []
+    try:
+        while len(list)>0:
+            m = re1.match(list.pop(0), 0)
+            if m:
+                subt = [int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3)) + int(m.group(4))/100.0]
+                subt.append(int(m.group(5))*3600 + int(m.group(6))*60 + int(m.group(7)) + int(m.group(8))/100.0)
+                l = list.pop(0).strip()
+                lines = l.split("[br]")
+                for i in range(0,len(lines)):
+                    subt.append(lines[i])
+                subtitles.append(subt)
+    except IndexError:
+        sys.stderr.write("Warning: it seems like input file is damaged or too short.\n")
+    return subtitles
 
 def read_srt(list):
-	"""
-	Reads srt subtitles.
-	input: contents of a file as list
-	returns: list of subtitles in form: [[time_dep, time_end, line1, ...],[time_dep, time_end, line1, ...],....]
-	"""
-	re1 = re.compile("^(\d+)\s*$")
-	re2 = re.compile("^(\d+):(\d+):(\d+),(\d+)\s*-->\s*(\d+):(\d+):(\d+),(\d+).*$")
-	re3 = re.compile("^\s*$")
-	subtitles = []
-	try:
-		while len(list)>0:
-			if re1.match(list.pop(0), 0):
-				m = re2.match(list.pop(0), 0)
-				if m:
-					subt = [int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3)) + int(m.group(4))/1000.0]
-					subt.append(int(m.group(5))*3600 + int(m.group(6))*60 + int(m.group(7)) + int(m.group(8))/1000.0)
-					l = list.pop(0)
-					while not re3.match(l, 0):
-						#subt.append(string.replace(l[:-1], "\r", ""))
-						subt.append(l.strip())
-						l = list.pop(0)
-					subtitles.append(subt)
-	except IndexError:
-		sys.stderr.write("Warning: it seems like input file is damaged or too short.\n")
-	return subtitles
+    """
+    Reads srt subtitles.
+    input: contents of a file as list
+    returns: list of subtitles in form: [[time_dep, time_end, line1, ...],[time_dep, time_end, line1, ...],....]
+    """
+    re1 = re.compile("^(\d+)\s*$")
+    re2 = re.compile("^(\d+):(\d+):(\d+),(\d+)\s*-->\s*(\d+):(\d+):(\d+),(\d+).*$")
+    re3 = re.compile("^\s*$")
+    subtitles = []
+    try:
+        while len(list)>0:
+            if re1.match(list.pop(0), 0):
+                m = re2.match(list.pop(0), 0)
+                if m:
+                    subt = [int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3)) + int(m.group(4))/1000.0]
+                    subt.append(int(m.group(5))*3600 + int(m.group(6))*60 + int(m.group(7)) + int(m.group(8))/1000.0)
+                    l = list.pop(0)
+                    while not re3.match(l, 0):
+                        #subt.append(string.replace(l[:-1], "\r", ""))
+                        subt.append(l.strip())
+                        l = list.pop(0)
+                    subtitles.append(subt)
+    except IndexError:
+        sys.stderr.write("Warning: it seems like input file is damaged or too short.\n")
+    return subtitles
 
 def read_tmp(list):
-	"""
-	Reads tmplayer (tmp) subtitles.
-	input: contents of a file as list
-	returns: list of subtitles in form: [[time_dep, time_end, line1, ...],[time_dep, time_end, line1, ...],....]
-	"""
-	re1 = re.compile("^(\d+):(\d+):(\d+):(.*)")
-	subtitles = []
-	subs={}
-	while len(list)>0:
-		m = re1.match(list.pop(0), 0)
-		if m:
-			time = int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3))
-			if subs.has_key(time) :
-				subs[time].extend(m.group(4).strip().split("|"))
-			else:
-				subs[time] = m.group(4).strip().split("|")
-
-	times = subs.keys()
-	times.sort()
-	for i in range(0,len(times)):
-		next_time = 1;
-		while not subs.has_key(times[i]+next_time) and next_time < 4 :
-		  	next_time = next_time + 1
-		subt = [ times[i] , times[i] + next_time]
-		subt.extend(subs[times[i]])
-		subtitles.append(subt)
-	return subtitles
+    """
+    Reads tmplayer (tmp) subtitles.
+    input: contents of a file as list
+    returns: list of subtitles in form: [[time_dep, time_end, line1, ...],[time_dep, time_end, line1, ...],....]
+    """
+    re1 = re.compile("^(\d+):(\d+):(\d+):(.*)")
+    subtitles = []
+    subs={}
+    while len(list)>0:
+        m = re1.match(list.pop(0), 0)
+        if m:
+            time = int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3))
+            if subs.has_key(time) :
+                subs[time].extend(m.group(4).strip().split("|"))
+            else:
+                subs[time] = m.group(4).strip().split("|")
+
+    times = subs.keys()
+    times.sort()
+    for i in range(0,len(times)):
+        next_time = 1;
+        while not subs.has_key(times[i]+next_time) and next_time < 4 :
+            next_time = next_time + 1
+        subt = [ times[i] , times[i] + next_time]
+        subt.extend(subs[times[i]])
+        subtitles.append(subt)
+    return subtitles
 
 def to_tmp(list):
-	"""
-	Converts list of subtitles (internal format) to tmp format
-	"""
-	outl = []
-	for l in list:
-		secs = l[0]
-		h = int(secs/3600)
-		m = int(int(secs%3600)/60)
-		s = int(secs%60)
-		outl.append("%.2d:%.2d:%.2d:%s\n" % (h,m,s,"|".join(l[2:])))
-	return outl
+    """
+    Converts list of subtitles (internal format) to tmp format
+    """
+    outl = []
+    for l in list:
+        secs = l[0]
+        h = int(secs/3600)
+        m = int(int(secs%3600)/60)
+        s = int(secs%60)
+        outl.append("%.2d:%.2d:%.2d:%s\n" % (h,m,s,"|".join(l[2:])))
+    return outl
 
 
 def to_srt(list):
-	"""
-	Converts list of subtitles (internal format) to srt format
-	"""
-	outl = []
-	count = 1
-	for l in list:
-		secs1 = l[0]
-		h1 = int(secs1/3600)
-		m1 = int(int(secs1%3600)/60)
-		s1 = int(secs1%60)
-		f1 = (secs1 - int(secs1))*1000
-		secs2 = l[1]
-		h2 = int(secs2/3600)
-		m2 = int(int(secs2%3600)/60)
-		s2 = int(secs2%60)
-		f2 = (secs2 - int(secs2))*1000
-		outl.append("%d\n%.2d:%.2d:%.2d,%.3d --> %.2d:%.2d:%.2d,%.3d\n%s\n\n" % (count,h1,m1,s1,f1,h2,m2,s2,f2,"\n".join(l[2:])))
-		count = count + 1
-	return outl
+    """
+    Converts list of subtitles (internal format) to srt format
+    """
+    outl = []
+    count = 1
+    for l in list:
+        secs1 = l[0]
+        h1 = int(secs1/3600)
+        m1 = int(int(secs1%3600)/60)
+        s1 = int(secs1%60)
+        f1 = (secs1 - int(secs1))*1000
+        secs2 = l[1]
+        h2 = int(secs2/3600)
+        m2 = int(int(secs2%3600)/60)
+        s2 = int(secs2%60)
+        f2 = (secs2 - int(secs2))*1000
+        outl.append("%d\n%.2d:%.2d:%.2d,%.3d --> %.2d:%.2d:%.2d,%.3d\n%s\n\n" % (count,h1,m1,s1,f1,h2,m2,s2,f2,"\n".join(l[2:])))
+        count = count + 1
+    return outl
 
 
 def sub_add_offset(list, off):
-	"""
-	Adds an offset (in seconds, may be negative) to all subtitles in the list
-	input: subtitles (internal format)
-	returns: new subtitles (internal format)
-	"""
-	outl = []
-	for l in list:
-		l[0] += off
-		l[1] += off
-		if l[0] < 0:
-			sys.stderr.write("Warning, negative offset too high, subs beginning at 00:00:00\n")
-			l[0] = 0
-		if l[1] < 0:
-			sys.stderr.write("Warning, negative offset too high, subs beginning at 00:00:00\n")
-			l[1] = 0
-		outl.append(l)
-	return outl
+    """
+    Adds an offset (in seconds, may be negative) to all subtitles in the list
+    input: subtitles (internal format)
+    returns: new subtitles (internal format)
+    """
+    outl = []
+    for l in list:
+        l[0] += off
+        l[1] += off
+        if l[0] < 0:
+            sys.stderr.write("Warning, negative offset too high, subs beginning at 00:00:00\n")
+            l[0] = 0
+        if l[1] < 0:
+            sys.stderr.write("Warning, negative offset too high, subs beginning at 00:00:00\n")
+            l[1] = 0
+        outl.append(l)
+    return outl
 
 def sub_split(sub, times):
-	"""
-	Splits subtitles
-	input: subtitles (internal format) and split positions (in seconds)
-	returns: a list of lists with new subtitles
-	"""
-	pos = 0
-	num = len(sub)
-	
-	while pos<num and sub[pos][0]<times[0]:
-		pos += 1
-	
-	lists = [ sub[:pos] ]    # [subtitles1, subtitles2, ...]
-
-	times.append(99999999)
-	minussec = times.pop(0)
-	
-	for second in times:
-		outl = []
-		while pos<num and sub[pos][0]<second:
-			subline = [sub[pos][0]-minussec] + [sub[pos][1]-minussec] + sub[pos][2:]
-			if subline[0] < 0:
-				subline[0] = 0
-			if subline[1] < 0:
-				subline[1] = 0
-			outl.append(subline)
-			pos += 1
-		lists.append(outl)
-		minussec = second
-	return lists
+    """
+    Splits subtitles
+    input: subtitles (internal format) and split positions (in seconds)
+    returns: a list of lists with new subtitles
+    """
+    pos = 0
+    num = len(sub)
+
+    while pos<num and sub[pos][0]<times[0]:
+        pos += 1
+
+    lists = [ sub[:pos] ]    # [subtitles1, subtitles2, ...]
+
+    times.append(99999999)
+    minussec = times.pop(0)
+
+    for second in times:
+        outl = []
+        while pos<num and sub[pos][0]<second:
+            subline = [sub[pos][0]-minussec] + [sub[pos][1]-minussec] + sub[pos][2:]
+            if subline[0] < 0:
+                subline[0] = 0
+            if subline[1] < 0:
+                subline[1] = 0
+            outl.append(subline)
+            pos += 1
+        lists.append(outl)
+        minussec = second
+    return lists
 
 def get_split_times(str):
-	"""
-	Converts comma-separated string of "xx:yy:zz,xx:yy:zz,..." times to list of times (in seconds)
-	input: string of comma-separated xx:yy:zz time positions
-	returns: list of times
-	"""
-	tlist = str.split(",")
-	re1 = re.compile("^(\d+):(\d+):(\d+)")
-	times = []
-	for t in tlist:
-		m = re1.match(t, 0)
-		if not m:
-			sys.stderr.write("Unknown time format\n")
-			return []
-		times.append(int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3)))
-	return times		
+    """
+    Converts comma-separated string of "xx:yy:zz,xx:yy:zz,..." times to list of times (in seconds)
+    input: string of comma-separated xx:yy:zz time positions
+    returns: list of times
+    """
+    tlist = str.split(",")
+    re1 = re.compile("^(\d+):(\d+):(\d+)")
+    times = []
+    for t in tlist:
+        m = re1.match(t, 0)
+        if not m:
+            sys.stderr.write("Unknown time format\n")
+            return []
+        times.append(int(m.group(1))*3600 + int(m.group(2))*60 + int(m.group(3)))
+    return times		
 
<<Diff was trimmed, longer than 597 lines>>

---- CVS-web:
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/packages/subconv/subconv.py?r1=1.4&r2=1.5&f=u



More information about the pld-cvs-commit mailing list