Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- ###################################################################################################
- ### SONARR POST-PROCESSING SCRIPT ###
- # ###
- # Script to rip srt subtitles out of .mkv files ###
- # ###
- # Used to extract English and Dutch subtitles. ###
- # To specify the languages extracted, you can alter the values for ###
- # StreamLang1Ids, StreamLang2Ids and Ext(.language tag.srt) ###
- # ###
- # Processing files which contain multiple subtitle streams for the same language will only ###
- # result in the last stream being decrypted. <-- Looking for better behaviour. ###
- # This is often the case with WEB-DL. ###
- # ###
- # !!! Make sure to alter your log file location !!! ###
- # !!! Make sure to alter your ffmpeg path !!! ###
- # ###
- # Credits to BenV for the basecode ###
- # ###
- # To do: ###
- # - Make the overwrite a variable (-y/-n) ###
- # - Make language tag / Ext input a variable .nl.srt ###
- # - Add escape for additional languages with fake language tag ###
- # ###
- # ###
- # ###
- # ###
- ###################################################################################################
- from re import findall
- from subprocess import Popen, PIPE
- from datetime import datetime
- from os import environ
- from sys import argv
- # Get the video file name from the environment variables
- filePath = environ.get('sonarr_episodefile_path')
- #Enable for troubleshooting. Enables parsing arguments to variable manually.
- #Example command: python scriptname.py /volume1/Sonarr/Series/SeriesE01.mkv
- #filePath = argv[1]
- #Extracting the filename and extension
- fileName = filePath.split('/')[-1]
- #Logfile location and open it unbuffered so you can read it during execution.
- fp = open('/volume1/Sonarr/postprocess.log', 'a',0)
- # If the ffmpeg excutable is in the path use this
- #ffmpeg = 'D:\\aaa\\ffmpeg.exe'
- # if you want to use an ffmpeg version from the SynoCommunity use this:
- ffmpeg = '/volume1/@appstore/ffmpeg/bin/ffmpeg'
- # This list contains possible subtitle stream identifiers for the first preferred language
- StreamLang1Ids = ['nl','nld','dut','dutch','qoq','qoq-team']
- # This list contains possible subtitle stream identifiers for the second preferred language
- StreamLang2Ids = ['en','eng','english']
- #Timestamp variable for logfile
- sttime = datetime.now().strftime('%Y%m%d %H:%M:%S - ')
- if not filePath.lower().endswith('.mkv'):
- fp.write(sttime + fileName + ' is not an .mkv file, processing stopped.\n\n')
- fp.close()
- raise SystemExit
- else:
- fp.write(sttime + fileName + ' is an .mkv file, processing started.\n')
- # First read the information from the video with ffmpeg.
- # ffmpeg writes this info to the stderr and not to stdout.
- Cmd = [ffmpeg,'-i', filePath]
- Merge = Popen(Cmd, stdout=PIPE, stderr=PIPE)
- # After the command is prepared we execute it and wait for it to finish with the communicate command
- output,err = Merge.communicate()
- # ffmpeg writes to stderr
- # make lines insted of one large buffer
- err= err.splitlines()
- # loop through the information lines, find the stream identifiers and count them
- NonSubStreamCount = 0
- for Line in err:
- if 'Stream #' in Line:
- NonSubStreamCount += 1
- # if it is a subtitle stream, get the stream info
- if 'Subtitle: subrip' in Line:
- NonSubStreamCount -= 1
- # use some regexes to find the stream info
- StreamNum = int(findall(r'\d+\.{0,1}\d*', Line)[1]) - NonSubStreamCount
- StreamLang = findall(r'\((.*?)\)', Line)[0]
- # compose the map identifiers for the ffmpeg command.
- Streamspec = '0:s:' + str(StreamNum)
- # try to identify the language and create the correct file extension belonging to that language.
- Ext = ''
- # First we look to see if it is a sub matching preferred language 1
- for Id in StreamLang1Ids:
- if Id == StreamLang.lower():
- Ext = '.nl.srt'
- break
- # If not, we look to see if it is a sub matching preferred language 2
- if not Ext:
- for Id in StreamLang2Ids:
- if Id == StreamLang.lower():
- Ext = '.en.srt'
- break
- # We only process the files is their is a preferred language match
- if Ext:
- OutputFile = filePath[:-4] + Ext
- Cmd =[ffmpeg, '-y', '-loglevel','-32','-i', filePath,'-map',Streamspec,OutputFile]
- Merge = Popen(Cmd, stdout=PIPE, stderr=PIPE)
- sttime = datetime.now().strftime('%Y%m%d_%H:%M:%S - ')
- fp.write(sttime + 'Extracting stream ' + Streamspec + ' language = ' + StreamLang + '\n')
- output,err = Merge.communicate()
- if err:
- fp.write('Problem extracting subtitle.\n')
- fp.write(err + '\n')
- sttime = datetime.now().strftime('%Y%m%d_%H:%M:%S - ')
- fp.write(sttime + 'Processing finished.\n\n')
- # close the logfile and exit back to Sonarr
- fp.close()
- raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement