Advertisement
samicrusader

Untitled

Jun 20th, 2023 (edited)
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.27 KB | None | 0 0
  1. import os
  2. import shutil
  3. import subprocess
  4. from pynput import keyboard as keyboard2
  5. from pywinauto.application import Application
  6. from pywinauto import keyboard
  7. from time import sleep
  8. skipsplit = ['Arthur.S11E19.Big.Brother.Binky.Parts.1.and.2.WEB-DL.AAC2.0.H.264-BTN.mkv']
  9. subskip = []
  10.  
  11. path = 'D:\\Arthur.S11.WEB-DL.AAC2.0.H.264-BTN\\Arthur.S11.WEB-DL.AAC2.0.H.264-BTN'
  12. outdir = 'D:\\Arthur\\Season 11\\'
  13. s = 11
  14. e = 1
  15.  
  16. root = Application(backend='win32')
  17.  
  18. # Set up event for pressing a key
  19. def on_release(key):
  20.     if key == keyboard2.Key.pause: # If the pause key is pressed
  21.         return False # Stop the pynput listener
  22.     else:
  23.         return True
  24.  
  25. def split(where:str, fn:str):
  26.     if where == 'first': # >[
  27.         vrdapp.VideoReDoClass6.Button2.set_focus()
  28.         vrdapp.VideoReDoClass6.Button2.click()
  29.     elif where == 'second': # ]<
  30.         vrdapp.VideoReDoClass6.Button1.set_focus()
  31.         vrdapp.VideoReDoClass6.Button1.click()
  32.     vrdapp.VideoReDoClass6.TrimUnselected.set_focus()
  33.     vrdapp.VideoReDoClass6.TrimUnselected.click()
  34.     vrdapp.VideoReDoClass6.SaveVideo.set_focus()
  35.     vrdapp.VideoReDoClass6.SaveVideo.click()
  36.     vrdapp.SaveVideo.wait('visible', timeout=20)
  37.     vrdapp.SaveVideo.Edit.set_focus()
  38.     vrdapp.SaveVideo.Edit.set_edit_text(fn)
  39.     vrdapp.SaveVideo.Save.wait('ready', timeout=20)
  40.     vrdapp.SaveVideo.Save.set_focus()
  41.     vrdapp.SaveVideo.Save.click()
  42.     # Wait for completion window to exist then close it
  43.     vrdapp.OutputProcessingComplete.wait('exists', timeout=240)
  44.     vrdapp.OutputProcessingComplete.OK.set_focus()
  45.     vrdapp.OutputProcessingComplete.OK.click()
  46.     vrdapp.VideoReDoClass6.wait('visible', timeout=20)
  47.  
  48. # Extract subtitles from source material
  49. # VideoReDo doesn't handle subs, I have to do so manually
  50. for file in os.listdir(path):
  51.     if file.endswith('.mkv') and file not in skipsplit:
  52.         fullpath = os.path.join(path, file)
  53.         try:
  54.             print(subprocess.check_output(['C:\\Users\\samicrusader\\Downloads\\ffmpeg-shared-06e8eed-9b0e37c-win64-nonfree\\ffmpeg.exe', '-i', fullpath, '-map', '0:s', '-c:s', 'copy', os.path.join(outdir, file.replace('.mkv', '.srt'))]))
  55.         except:
  56.             subskip.append(file)
  57.  
  58. # Start VideoReDo
  59. for file in os.listdir(path):
  60.     fullpath = os.path.join(path, file)
  61.     if file.endswith('.mkv') and file not in skipsplit:
  62.         vrdapp = root.start('C:\\Program Files (x86)\\VideoReDoPro6\\VideoReDoPro6.exe')
  63.         vrdapp.VideoReDoProfessional.wait('ready', timeout=20)
  64.         # Open video file in VideoReDo
  65.         vrdapp.VideoReDoProfessional['Open\nVideo'].click()
  66.         vrdapp.Open.wait('visible', timeout=20)
  67.         vrdapp.Open.FileNameEdit.set_focus()
  68.         vrdapp.Open.FileNameEdit.set_edit_text(fullpath)
  69.         vrdapp.Open.Open.wait('ready', timeout=20)
  70.         vrdapp.Open.Open.set_focus()
  71.         vrdapp.Open.Open.click()
  72.  
  73.         # Set up keyboard listener
  74.         with keyboard2.Listener(on_release=on_release) as listener:
  75.             listener.join() # This hangs until on_release returns False (e.g. when the Pause/SysRq key is pressed)
  76.  
  77.         # Split
  78.         split('first', os.path.join(outdir, f'Arthur - S{s:>02}E{e:>02}_src.mkv'))
  79.         e += 1
  80.         split('second', os.path.join(outdir, f'Arthur - S{s:>02}E{e:>02}_src.mkv'))
  81.         e += 1
  82.         sleep(3)
  83.         vrdapp.VideoReDoClass6.menu_select("File -> Exit")
  84.     else: # some videos that dont need to be split
  85.         shutil.copy(fullpath, os.path.join(outdir, f'Arthur - S{s:>02}E{e:>02}_src.mkv'))
  86.         e += 1
  87. e = 1
  88.  
  89. # Start Subtitle Edit
  90. seapp = root.start('C:\\Program Files\\Subtitle Edit\\SubtitleEdit.exe')
  91.  
  92. # Split subtitles
  93. for file in os.listdir(path):
  94.     if file.endswith('.mkv'):
  95.         fullpath = os.path.join(outdir, file.replace('.mkv', '.srt'))
  96.         if file not in skipsplit and file in subskip:
  97.             e += 2
  98.         elif file in skipsplit:
  99.             #shutil.copy(fullpath, os.path.join(outdir, f'Arthur - S{s:>02}E{e:>02}.srt'))
  100.             e += 1
  101.         else:
  102.             seapp.top_window().wait('ready', timeout=20)
  103.             seapp.top_window().set_focus()
  104.             keyboard.send_keys('^o')
  105.             seapp.Opensubtitle.wait('visible', timeout=20)
  106.             seapp.Opensubtitle.FileNameEdit.set_focus()
  107.             seapp.Opensubtitle.FileNameEdit.set_edit_text(fullpath)
  108.             seapp.Opensubtitle.Open.wait('ready', timeout=20)
  109.             seapp.Opensubtitle.Open.set_focus()
  110.             seapp.Opensubtitle.Open.click()
  111.             sleep(3)
  112.             seapp.top_window().wait('ready', timeout=20)
  113.             seapp.top_window().set_focus()
  114.             keyboard.send_keys('%tss{ENTER}')
  115.             seapp.Splitsubtitle.wait('ready', timeout=20)
  116.             seapp.Splitsubtitle['...'].set_focus()
  117.             seapp.Splitsubtitle['...'].click()
  118.             sleep(2)
  119.             seapp.Openvideofile.wait('visible', timeout=20)
  120.             seapp.Openvideofile.FileNameEdit.set_focus()
  121.             seapp.Openvideofile.FileNameEdit.set_edit_text(os.path.join(outdir, f'Arthur - S{s:>02}E{e:>02}_src.mkv'))
  122.             seapp.Openvideofile.Open.wait('ready', timeout=20)
  123.             seapp.Openvideofile.Open.set_focus()
  124.             seapp.Openvideofile.Open.click()
  125.             seapp.Splitsubtitle.wait('ready', timeout=20)
  126.             seapp.Splitsubtitle['Split'].set_focus()
  127.             seapp.Splitsubtitle['Split'].click()
  128.             sleep(2)
  129.             seapp.Savepart1as.wait('ready', timeout=20)
  130.             seapp.Savepart1as.Edit.set_focus()
  131.             seapp.Savepart1as.Edit.set_edit_text(os.path.join(outdir, f'Arthur - S{s:>02}E{e:>02}.srt'))
  132.             seapp.Savepart1as.Save.wait('ready', timeout=20)
  133.             seapp.Savepart1as.Save.set_focus()
  134.             seapp.Savepart1as.Save.click()
  135.             e += 1
  136.             seapp.Splitsubtitle.wait('active', timeout=20)
  137.             seapp.Savepart2as.wait('ready', timeout=20)
  138.             seapp.Savepart2as.Edit.set_focus()
  139.             seapp.Savepart2as.Edit.set_edit_text(os.path.join(outdir, f'Arthur - S{s:>02}E{e:>02}.srt'))
  140.             seapp.Savepart2as.Save.wait('ready', timeout=20)
  141.             seapp.Savepart2as.Save.set_focus()
  142.             seapp.Savepart2as.Save.click()
  143.             e += 1
  144.             sleep(2)
  145.         try:
  146.             os.remove(fullpath)
  147.         except:
  148.             pass
  149. keyboard.send_keys('%fx')
  150. e = 1
  151.  
  152. # finally, mux.
  153. for file in os.listdir(outdir):
  154.     if file.startswith('Arthur - ') and file.endswith('_src.mkv'):
  155.         basename = file.rstrip('_src.mkv')
  156.         srcpath = os.path.join(outdir, file)
  157.         mkvpath = os.path.join(outdir, basename+'.mkv')
  158.         srtpath = os.path.join(outdir, basename+'.srt')
  159.         cmdline = ["C:\\Program Files\\MKVToolNix\\mkvmerge.exe", "--ui-language", "en", "--output", mkvpath, "--no-track-tags", "--no-global-tags", "--no-chapters", "--language", "0:en", "--language", "1:en", "--track-name", "1:English", "(", srcpath, ")"]
  160.         if os.path.exists(srtpath):
  161.             cmdline += ["--language", "0:en", "--track-name", "0:English", "(", srtpath, ")"]
  162.         cmdline += ["--title", basename, "--track-order", "0:0,0:1,1:0"]
  163.         print(subprocess.check_output(cmdline).decode())
  164.         os.remove(srcpath)
  165.         if os.path.exists(srtpath):
  166.             os.remove(srtpath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement