document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python
  2. #coding:utf8
  3.  
  4. input_file=None
  5. version=\'0.1\'
  6. progname=\'subconvert\'
  7. from optparse import OptionParser
  8. usage = u"%prog [options]"
  9. parser = OptionParser(usage=usage,version="%s %s" % (progname,version) )
  10. parser.add_option("-f", dest="input_file",help=u"Путь к файлу", metavar="INPUT_FILE",default=input_file)
  11. opts,args = parser.parse_args()
  12. import sys,os
  13.  
  14. if not opts.input_file:
  15.     print "Не указан обрабатываемый файл"
  16.     exit(0)
  17. else:
  18.     input_file=opts.input_file
  19.     try:
  20.         file_name=\'\'.join(input_file.split(\'.\')[:-1])
  21.         file_ext=\'\'.join(input_file.split(\'.\')[-1:])
  22.         ass=file(input_file,\'r\')
  23.     except:
  24.         print "Не удалось открыть файл %s" % input_file
  25.         os.system("notify-send \'Не удалось открыть файл %s\'" % input_file)
  26.         exit(0)
  27.  
  28. import re
  29. line=re.compile(r\'\\d+,(\\d+:\\d+:\\d+.\\d+),(\\d+:\\d+:\\d+.\\d+),[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,(.*)\')
  30. strip=re.compile(r\'{[^}]*}\')
  31.  
  32. def timeto(time_string):
  33.     try:
  34.         h,m,s=time_string.replace(\',\',\'.\').split(\':\')
  35.         if len(s.split(\'.\'))==2: s,ms=s.split(\'.\')
  36.         else: ms=\'000\'
  37.         if len(ms)==1: ms+=\'00\'
  38.         if len(ms)==2: ms+=\'0\'
  39.         return "%02d:%02d:%02d,%s" % ( int(h),int(m),int(s), ms )
  40.     except:
  41.         print "Не удалось парсить строку времени %s" % time_string
  42.         exit(1)
  43.  
  44. srt=list()
  45. for ln in ass:
  46.     if line.findall(ln):
  47.         start=line.findall(ln)[0][0]
  48.         finish=line.findall(ln)[0][1]
  49.         text=strip.sub(\'\', line.findall(ln)[0][2])
  50.         srt.append("%s --> %s\\n%s" % (timeto(start) ,timeto(finish), text.strip()))
  51. srt.sort()
  52.  
  53. f=open(file_name+\'.srt\' ,\'w\')
  54. for n,i in enumerate(srt):
  55.     f.write("%d\\n%s\\n\\n" % (n+1,i))
  56. f.close()
');