#!/usr/bin/python
import os
import sys
from subprocess import Popen, PIPE
def get_subtitles_track(file_path):

    sout, serr = Popen(['sh', 'FindSubtitleTrack.sh', file_path], stdout=PIPE, stderr=PIPE).communicate()
    # print(serr)
    return sout.strip()

def extract_srt(file_path):

    print('\nprocessing {}'.format(os.path.basename(file_path)))
    
    ass_path = os.path.splitext(file_path) [0] + ".ass"
    srt_path = os.path.splitext(file_path) [0] + ".srt"

    if not os.path.exists(ass_path):
        extract_ass(file_path, ass_path)

    if os.path.exists(ass_path) and not os.path.exists(srt_path):
        convert_ass_to_srt(ass_path, srt_path)
    	print('> srt extraction OK')
    
def extract_ass(file_path, ass_path):

    # print('extracting ASS/SSA subtitles')

    trackns = get_subtitles_track(file_path)
    if trackns == None or trackns == "":
        print('> no subtitles track found.')
        return
    
    try:    
        trackn = int(trackns) - 1
    except:
        print('> subtitles tracks incorrect: {}'.format(trackns))
        return
    
    print('> extract subtitles from track {}'.format(trackn))
    sout, serr = Popen(['mkvextract', 'tracks', file_path, "{}:{}".format(trackn, ass_path)], stdout=PIPE, stderr=PIPE).communicate()
    
    # print(serr)

def convert_ass_to_srt(ass_path, srt_path):
    
    sout, serr = Popen(['perl', 'ass2srt.pl', ass_path], stdout=PIPE, stderr=PIPE).communicate()
    print(serr)

    tx = ''
    with open(srt_path, 'rt') as fb:
        tx = fb.read().replace('\n\n','\n').replace('\\N', '\n')

    with open(srt_path, 'wt') as fb:
        fb.write(tx)

def main():

    if len(sys.argv) < 2:
        return

    import glob
    target = sys.argv[1]

    i = 0

    if os.path.isdir(target):
        for i, f in enumerate( [ os.path.join(target,x) for x in os.listdir(target) if x.endswith('.mkv') ] ):
            extract_srt(f)
        print('processed {} file(s)'.format(i))
    else:
        extract_srt(target)


if __name__ == '__main__':

    main()


@@ -293,18 +293,16 @@
  print('
-%5Cn
 processi
@@ -470,20 +470,16 @@
 %0A    if 
-not 
 os.path.
@@ -481,27 +481,27 @@
 path.exists(
-ass
+srt
 _path):%0A    
@@ -508,40 +508,52 @@
     
-extract_ass(file_path, ass_path)
+print('%3E srt already exists')%0A        return
 %0A%0A  
@@ -549,32 +549,36 @@
  return%0A%0A    if 
+not 
 os.path.exists(a
@@ -589,16 +589,58 @@
 ath)
- and not
+:%0A        extract_ass(file_path, ass_path)%0A%0A    if
  os.
@@ -643,35 +643,35 @@
  os.path.exists(
-srt
+ass
 _path):%0A        
@@ -705,24 +705,102 @@
 , srt_path)%0A
+        try:%0A            os.remove(ass_path)%0A        except:%0A            pass%0A
     %09print('
@@ -1056,25 +1056,83 @@
  return%0A    
-%0A
+track_list = trackns.split()%0A%0A    for t in track_list:%0A    
     try:    
@@ -1128,24 +1128,28 @@
    try:    %0A
+    
         trac
@@ -1162,20 +1162,18 @@
 nt(t
-rackns
 ) - 1%0A
+    
     
@@ -1180,32 +1180,36 @@
 except:%0A        
+    
 print('%3E subtitl
@@ -1250,12 +1250,15 @@
 ackn
-s
 ))%0A
+    
     
@@ -1269,24 +1269,28 @@
 return%0A    %0A
+    
     print('%3E
@@ -1335,24 +1335,28 @@
 at(trackn))%0A
+    
     sout, se

read multiple subtitles tracks