Guest User

bootylift.py

a guest
Jan 2nd, 2022
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. '''
  2. bootylift: it fixes ASS
  3. -iterates recursively through given directory
  4. -checks every mkv file for SubStationAlpha subtitles
  5. -extracts all ass/ssa subtitles
  6. -converts them to srt
  7. -creates new mkv file without the ass subs replaced by the new srt subs
  8. -cleans up after itself
  9. '''
  10.  
  11. import os
  12. import re
  13. import sys
  14. import subprocess
  15. import pymediainfo
  16.  
  17. def cleanup(file, sub_ids):
  18.     subprocess.call(["rm", file])
  19.     subprocess.call(["mv", file[:-4]+'-temp.mkv', file])
  20.     for elem in sub_ids:
  21.         subprocess.call(["rm", file[:-4]+'-'+str(elem[0])+'.srt'])
  22.     print("Finished handling", file, "–", str(len(sub_ids)), "subtitles tracks converted from ASS/SSA to SRT.")
  23.  
  24. def remux(file, sub_ids):
  25.     command = ["mkvmerge", "-o", file[:-4]+'-temp.mkv', '--no-subtitles', file]
  26.     for elem in sub_ids:
  27.         elem_id = elem[0]
  28.         lang = elem[1]
  29.         if lang == None:
  30.             lang = 'und'
  31.         srt_filename = file[:-4]+'-'+str(elem_id)+'.srt'
  32.         command.append("--language")
  33.         command.append('0:'+lang)
  34.         command.append(srt_filename)
  35.     subprocess.call(command)
  36.  
  37. def convert_ass(file, sub_ids, root):
  38.     for elem in sub_ids:
  39.         ass_filename = file[:-4]+'-'+str(elem[0])+'.ass'
  40.         subprocess.call(["asstosrt", ass_filename, "--output-dir", root])
  41.         subprocess.call(["rm", file[:-4]+'-'+str(elem[0])+'.ass'])
  42.  
  43. def extract_ass(file, sub_ids):
  44.     for elem in sub_ids:
  45.         ass_filename = file[:-4]+'-'+str(elem[0])+'.ass'
  46.         subprocess.call(["mkvextract", "tracks", file, str(elem[0])+':'+ass_filename])
  47.  
  48. def fix_mkv(root, file, sub_ids):
  49.     extract_ass(file, sub_ids)
  50.     convert_ass(file, sub_ids, root)
  51.     remux(file, sub_ids)
  52.     cleanup(file, sub_ids)
  53.  
  54. def inspect_mkv(root, file):
  55.     '''
  56.     Runs an mkvmerge analysis on the mkv and finds all ASS/SSA subs, collecting their track IDs and language tags
  57.     '''
  58.     proc = subprocess.Popen(["mkvmerge", "-i", file], stdout=subprocess.PIPE)
  59.     output = proc.stdout.read().decode()
  60.     ass_subs = re.findall('Track ID \d: subtitles \(SubStationAlpha\)', output)
  61.     sub_ids = list()
  62.     for elem in ass_subs:
  63.         track_id = int(re.search('Track ID (\d)', elem).group(1))
  64.         lang = pymediainfo.MediaInfo.parse(file).tracks[track_id].language
  65.         sub_ids.append((track_id, lang))
  66.     if len(sub_ids) > 0:
  67.         fix_mkv(root, file, sub_ids)
  68.        
  69.  
  70. def main():
  71.     dir = sys.argv[1]
  72.     for root, dirs, files in os.walk(dir):
  73.         for file in files:
  74.             if file.endswith(".mkv"):
  75.                 filepath = os.path.join(root, file)
  76.                 inspect_mkv(root, filepath)
  77.  
  78. if __name__ == "__main__":
  79.     main()
Advertisement
Add Comment
Please, Sign In to add comment