Created
April 16, 2019 13:14
-
-
Save magauthority/0eb9f4440354c9f0087c265d4c4c174f to your computer and use it in GitHub Desktop.
Script to process newly added items to Plex. Removing Eia-608 subs from .mkv files and creating the srt files. Also setting auio and sub language correctly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
############################################################################## | |
import sys, os, datetime, subprocess, traceback, re, shutil, difflib, time, errno | |
from os.path import dirname | |
sys.path.insert(0, dirname(__file__) + '/lib') | |
from hurry.filesize import size | |
# Exit codes | |
POSTPROCESS_SUCCESS=0 | |
POSTPROCESS_NONE=0 | |
POSTPROCESS_ERROR=1 | |
Lockfile = 'C:\\Users\\Public\\SubProcessLock.txt' | |
# Check For Lock File, so only one instance of this script runs at a time | |
while os.path.isfile(Lockfile): | |
time.sleep(30) | |
fl = open(Lockfile, 'w') | |
# Init script config options - Set Your Local Directory Options. | |
hbrakeoutputpath=r'G:\TempStorage\\' # Keep the r at the front and the extra '\' at the end. (if you're on windows).. Python Requires This. | |
arg1 = '--Language=raw' | |
mkvm = r'C:\Program Files\MKVToolNix\mkvmerge.exe' | |
mediainfo = r'C:\ccextractor\MediaInfocmd.exe' | |
ffmpeg = r'C:\Program Files\MCEBuddy2x\ffmpeg\ffmpeg.exe' | |
ccextract = r'C:\ccextractor\ccextractor.0.85b-windows.binaries\ccextractorwin.exe' | |
eiasubs = '--Output=Text;%Format%::' | |
audiochk = '--Output=Audio;%Language/String%' | |
temp_file = 'C:\\Users\\Public\\SubProcesslog.txt' | |
original_stderr = sys.stderr | |
sys.stdout.flush() | |
f = open(temp_file, 'w') | |
#sys.stdout = f | |
sys.stderr = f | |
print('WARNING: SubProcessing Program Starting...') | |
def carefulremove(filename): | |
try: | |
os.remove(filename) | |
except OSError as e: | |
if e.errno != errno.ENOENT: | |
print ('No such file or directory') | |
raise # re-raise exception if a different error occurred | |
return; | |
temp_file_name = sys.argv[2].replace("\\", "/") | |
full_path = os.path.dirname(os.path.abspath(temp_file_name)) | |
# Check For English Audio Track | |
p = subprocess.Popen([mediainfo, audiochk, temp_file_name],stdout=subprocess.PIPE,stderr=subprocess.PIPE) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR MediaInfo Error! This Script will be terminated.') | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
Audio_Check = output.lower() | |
if 'english' not in Audio_Check: | |
print ('There is no AUDIO language set, setting language to English' ) | |
if temp_file_name.endswith('mkv'): | |
mkvpropedit_cmd = r'C:\Program Files\MKVToolNix\mkvpropedit.exe "'+temp_file_name+'" --edit track:a1 --set language=eng ' | |
p = subprocess.Popen(mkvpropedit_cmd,stdout=f, stderr=sys.stderr) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR Setting Audio Language on MKV File! This Script will be terminated.') | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
if temp_file_name.endswith('mp4'): | |
shutil.copy(temp_file_name, hbrakeoutputpath) | |
file_name = hbrakeoutputpath + sys.argv[3] | |
mkvpropedit_cmd = r'C:\Program Files\GPAC\mp4box.exe -lang 2=eng "'+file_name+'"' | |
p = subprocess.Popen(mkvpropedit_cmd,stdout=f, stderr=sys.stderr) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR Setting Audio Language on MP4 File! This Script will be terminated.') | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
carefulremove(temp_file_name) | |
shutil.move(file_name, temp_file_name) | |
# If It's A TV Program, Convert To MP4 For Extraction Of Subtitles | |
if sys.argv[1] == 'episode' : | |
shutil.copy(temp_file_name, hbrakeoutputpath) | |
file_name = hbrakeoutputpath + sys.argv[3] | |
if file_name.endswith('mkv'): | |
ffmpegtemp = hbrakeoutputpath + 'tempmp4file.mp4' | |
ffmpeg_command = ffmpeg+' -i "'+file_name+'" -codec copy '+ffmpegtemp | |
p = subprocess.Popen(ffmpeg_command,stdout=sys.stdout, stderr=sys.stderr) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR FFMpeg Error Converting to MP4! This Script will be terminated.') | |
carefulremove(file_name) | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
if file_name.endswith('mp4'): | |
ffmpegtemp = hbrakeoutputpath + file_name | |
else: | |
print('Not a TV Episode; This Script will be terminated.') | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_SUCCESS) | |
# Check For EIA_608 Subtitles | |
p = subprocess.Popen([mediainfo, eiasubs, ffmpegtemp],stdout=subprocess.PIPE,stderr=subprocess.PIPE) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR MediaInfo Error! This Script will be terminated.') | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
Subs_Check = output.lower().strip().split("::")[:-1] | |
# If EIA Is Found, Extract Them to Srt File, then delete mp4. | |
if 'eia-608' not in Subs_Check : | |
print ("EIA Subs Not Found! Cleaning Up And Exiting.") | |
carefulremove(ffmpegtemp) | |
carefulremove(file_name) | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_SUCCESS) | |
else: | |
print ("We Need To Rip Subs To File And Remove Them ") | |
extract = ccextract+' --gui_mode_reports -autoprogram -out=srt -bom -utf8 --nofontcolor '+ffmpegtemp | |
p = subprocess.Popen(extract,stdout=f, stderr=sys.stderr) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR Count Not Rip Sub Titles! This Script will be terminated.') | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
# Remove Mp4 File - No Longer Needed | |
carefulremove(ffmpegtemp) | |
# Remove EIA From MKV File | |
ffmpegtemp2 = hbrakeoutputpath + 'tempmkvfile.mkv' | |
ffmpeg_command = ffmpeg+' -i "'+file_name+'" -codec copy -bsf:v "filter_units=remove_types=6" '+ffmpegtemp2 | |
p = subprocess.Popen(ffmpeg_command,stdout=sys.stdout, stderr=sys.stderr) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR! FMpeg Error Remove EIA Subs From MKV File! This Script will be terminated.') | |
carefulremove(ffmpegtemp2) | |
carefulremove(file_name) | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
print('INFO: Remove Of EIA Subs From MKV File Completed Successfully.') | |
mergetemp = hbrakeoutputpath + 'newmkvfile.mkv' | |
srtfile = os.path.splitext(ffmpegtemp)[0]+'.srt' | |
mkvmerge_cmd = r'C:\Program Files\MKVToolNix\mkvmerge.exe -o '+mergetemp+' '+ffmpegtemp2+' --language 0:eng '+srtfile | |
p = subprocess.Popen(mkvmerge_cmd,stdout=f, stderr=sys.stderr) | |
output, errors = p.communicate() | |
if p.returncode != 0: | |
print('ERROR MkvMerge Error! This Script will be terminated.') | |
carefulremove(ffmpegtemp2) | |
carefulremove(file_name) | |
carefulremove(mergetemp) | |
carefulremove(srtfile) | |
fl.close() | |
carefulremove(Lockfile) | |
sys.exit(POSTPROCESS_ERROR) | |
carefulremove(srtfile) | |
carefulremove(ffmpegtemp2) | |
carefulremove(file_name) | |
os.rename(mergetemp, file_name) | |
carefulremove(temp_file_name) | |
shutil.copy(file_name, temp_file_name) | |
carefulremove(file_name) | |
print('SUCCESS: SubProcessing Completed Successfully.') | |
f.close() | |
fl.close() | |
carefulremove(Lockfile) | |
sys.stderr = original_stderr | |
sys.exit(POSTPROCESS_SUCCESS) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment