Advertisement
Mirx

Sonarr subs extraction

Apr 12th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3.  
  4. ###################################################################################################
  5. ### SONARR POST-PROCESSING SCRIPT                                                               ###
  6. #                                                                                               ###
  7. # Script to rip srt subtitles out of .mkv files                                                 ###
  8. #                                                                                               ###
  9. # Used to extract English and Dutch subtitles.                                                  ###
  10. # To specify the languages extracted, you can alter the values for                              ###
  11. # StreamLang1Ids, StreamLang2Ids and Ext(.language tag.srt)                                     ###
  12. #                                                                                               ###
  13. # Processing files which contain multiple subtitle streams for the same language will only      ###
  14. # result in the last stream being decrypted. <-- Looking for better behaviour.                  ###
  15. # This is often the case with WEB-DL.                                                           ###
  16. #                                                                                               ###
  17. #                       !!! Make sure to alter your log file location !!!                       ###
  18. #                           !!! Make sure to alter your ffmpeg path !!!                         ###
  19. #                                                                                               ###
  20. # Credits to BenV for the basecode                                                              ###
  21. #                                                                                               ###
  22. # To do:                                                                                        ###
  23. # - Make the overwrite a variable (-y/-n)                                                       ###
  24. # - Make language tag / Ext input a variable .nl.srt                                            ###
  25. # - Add escape for additional languages with fake language tag                                  ###
  26. #                                                                                               ###
  27. #                                                                                               ###
  28. #                                                                                               ###
  29. #                                                                                               ###
  30. ###################################################################################################
  31.  
  32.  
  33. from re import findall
  34. from subprocess import Popen, PIPE
  35. from datetime import datetime
  36. from os import environ
  37. from sys import argv
  38.  
  39.     # Get the video file name from the environment variables
  40. filePath = environ.get('sonarr_episodefile_path')
  41.  
  42.     #Enable for troubleshooting. Enables parsing arguments to variable manually.
  43.     #Example command: python scriptname.py /volume1/Sonarr/Series/SeriesE01.mkv
  44. #filePath = argv[1]
  45.  
  46.     #Extracting the filename and extension
  47. fileName = filePath.split('/')[-1]
  48.  
  49.     #Logfile location and open it unbuffered so you can read it during execution.
  50. fp = open('/volume1/Sonarr/postprocess.log', 'a',0)
  51.  
  52.     # If the ffmpeg excutable is in the path use this
  53. #ffmpeg = 'D:\\aaa\\ffmpeg.exe'
  54.     # if you want to use an ffmpeg version from the SynoCommunity use this:
  55. ffmpeg = '/volume1/@appstore/ffmpeg/bin/ffmpeg'
  56.  
  57.     # This list contains possible subtitle stream identifiers for the first preferred language
  58. StreamLang1Ids = ['nl','nld','dut','dutch','qoq','qoq-team']
  59.  
  60.     # This list contains possible subtitle stream identifiers for the second preferred language
  61. StreamLang2Ids = ['en','eng','english']
  62.  
  63.  
  64.     #Timestamp variable for logfile
  65. sttime = datetime.now().strftime('%Y%m%d %H:%M:%S - ')
  66. if not filePath.lower().endswith('.mkv'):
  67.     fp.write(sttime + fileName + ' is not an .mkv file, processing stopped.\n\n')
  68.     fp.close()
  69.     raise SystemExit
  70. else:
  71.     fp.write(sttime + fileName + ' is an .mkv file, processing started.\n')
  72.  
  73.     # First read the information from the video with ffmpeg.
  74.     # ffmpeg writes this info to the stderr and not to stdout.
  75. Cmd = [ffmpeg,'-i', filePath]
  76. Merge = Popen(Cmd, stdout=PIPE, stderr=PIPE)
  77.     # After the command is prepared we execute it and wait for it to finish with the communicate command
  78. output,err = Merge.communicate()
  79.  
  80.     # ffmpeg writes to stderr
  81.     # make lines insted of one large buffer
  82. err= err.splitlines()
  83.  
  84.     # loop through the information lines, find the stream identifiers and count them
  85. NonSubStreamCount = 0
  86. for Line in err:
  87.     if 'Stream #' in Line:
  88.         NonSubStreamCount += 1
  89.             # if it is a subtitle stream, get the stream info
  90.         if 'Subtitle: subrip' in Line:
  91.             NonSubStreamCount -= 1
  92.                 # use some regexes to find the stream info
  93.             StreamNum = int(findall(r'\d+\.{0,1}\d*', Line)[1]) - NonSubStreamCount
  94.             StreamLang = findall(r'\((.*?)\)', Line)[0]
  95.                 # compose the map identifiers for the ffmpeg command.
  96.             Streamspec = '0:s:' + str(StreamNum)
  97.                 # try to identify the language and create the correct file extension belonging to that language.
  98.             Ext = ''
  99.                 # First we look to see if it is a sub matching preferred language 1
  100.             for Id in StreamLang1Ids:
  101.                 if Id == StreamLang.lower():
  102.                     Ext = '.nl.srt'
  103.                     break
  104.                 # If not, we look to see if it is a sub matching preferred language 2
  105.             if not Ext:
  106.                 for Id in StreamLang2Ids:
  107.                     if Id == StreamLang.lower():
  108.                         Ext = '.en.srt'
  109.                         break
  110.                 # We only process the files is their is a preferred language match
  111.             if Ext:
  112.                 OutputFile = filePath[:-4] + Ext
  113.                 Cmd =[ffmpeg, '-y', '-loglevel','-32','-i', filePath,'-map',Streamspec,OutputFile]
  114.                 Merge = Popen(Cmd, stdout=PIPE, stderr=PIPE)
  115.                 sttime = datetime.now().strftime('%Y%m%d_%H:%M:%S - ')
  116.                 fp.write(sttime + 'Extracting stream ' + Streamspec + ' language = ' + StreamLang + '\n')
  117.                 output,err = Merge.communicate()
  118.                 if err:
  119.                     fp.write('Problem extracting subtitle.\n')
  120.                     fp.write(err + '\n')
  121.  
  122. sttime = datetime.now().strftime('%Y%m%d_%H:%M:%S - ')
  123. fp.write(sttime + 'Processing finished.\n\n')
  124.                    
  125.     # close the logfile and exit back to Sonarr
  126. fp.close()
  127. raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement