diff --git a/core/auto_process/movies.py b/core/auto_process/movies.py index 40e64a96..160248f9 100644 --- a/core/auto_process/movies.py +++ b/core/auto_process/movies.py @@ -21,7 +21,7 @@ from core.auto_process.common import ( completed_download_handling, ) from core.plugins.downloaders.nzb.utils import report_nzb -from core.plugins.subtitles import import_subs +from core.plugins.subtitles import import_subs, rename_subs from core.scene_exceptions import process_all_exceptions from core.utils import ( convert_to_ascii, @@ -130,6 +130,7 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual', num_files += 1 if transcoder.is_video_good(video, status): import_subs(video) + rename_subs(dir_name) good_files += 1 if num_files and good_files == num_files: if status: diff --git a/core/auto_process/tv.py b/core/auto_process/tv.py index 179e51b5..40d64cc1 100644 --- a/core/auto_process/tv.py +++ b/core/auto_process/tv.py @@ -26,7 +26,7 @@ from core.auto_process.common import ( ) from core.auto_process.managers.sickbeard import InitSickBeard from core.plugins.downloaders.nzb.utils import report_nzb -from core.plugins.subtitles import import_subs +from core.plugins.subtitles import import_subs, rename_subs from core.scene_exceptions import process_all_exceptions from core.utils import ( convert_to_ascii, @@ -137,6 +137,7 @@ def process(section, dir_name, input_name=None, failed=False, client_agent='manu if transcoder.is_video_good(video, status): good_files += 1 import_subs(video) + rename_subs(dir_name) if num_files > 0: if good_files == num_files and not status == 0: logger.info('Found Valid Videos. Setting status Success') diff --git a/core/plugins/subtitles.py b/core/plugins/subtitles.py index c7d561bc..9c236aa1 100644 --- a/core/plugins/subtitles.py +++ b/core/plugins/subtitles.py @@ -12,6 +12,7 @@ import core from core import logger import os +import re for provider in subliminal.provider_manager.internal_extensions: if provider not in [str(x) for x in subliminal.provider_manager.list_entry_points()]: @@ -46,3 +47,61 @@ def import_subs(filename): os.chmod(subtitle_path, 0o644) except Exception as e: logger.error('Failed to download subtitles for {0} due to: {1}'.format(filename, e), 'SUBTITLES') + +def rename_subs(path): + filepaths = [] + sub_ext = ['.srt', '.sub', '.idx'] + vidfiles = core.list_media_files(path, media=True, audio=False, meta=False, archives=False) + if not vidfiles or len(vidfiles) > 1: # If there is more than 1 video file, or no video files, we can't rename subs. + return + name = os.path.splitext(os.path.split(vidfiles[0])[1])[0] + for directory, _, filenames in os.walk(path): + for filename in filenames: + filepaths.extend([os.path.join(directory, filename)]) + subfiles = [item for item in filepaths if os.path.splitext(item)[1] in sub_ext] + subfiles.sort() #This should sort subtitle names by language (alpha) and Number (where multiple) + renamed = [] + for sub in subfiles: + subname, ext = os.path.splitext(os.path.basename(sub)) + if name in subname: # The sub file name already includes the video name. + continue + words = re.findall('[a-zA-Z]+',str(subname)) # find whole words in string + # parse the words for language descriptors. + lan = None + for word in words: + try: + if len(word) == 2: + lan = Language.fromalpha2(word.lower()) + elif len(word) == 3: + lan = Language(word.lower()) + elif len(word) > 3: + lan = Language.fromname(word.lower()) + if lan: + break + except: #if we didn't find a language, try next word. + continue + # rename the sub file as name.lan.ext + if not lan: + # could call ffprobe to parse the sub information and get language if lan unknown here. + new_sub_name = name + else: + new_sub_name = '{name}.{lan}'.format(name=name, lan=str(lan)) + new_sub = os.path.join(directory, new_sub_name) # full path and name less ext + if '{new_sub}{ext}'.format(new_sub=new_sub, ext=ext) in renamed: # If duplicate names, add unique number before ext. + for i in range(1,len(renamed)+1): + if '{new_sub}.{i}{ext}'.format(new_sub=new_sub, i=i, ext=ext) in renamed: + continue + new_sub = '{new_sub}.{i}'.format(new_sub=new_sub, i=i) + break + new_sub = '{new_sub}{ext}'.format(new_sub=new_sub, ext=ext) # add extension now + if os.path.isfile(new_sub): # Don't copy over existing - final check. + logger.debug('Unable to rename sub file {old} as destination {new} already exists'.format(old=sub, new=new_sub)) + continue + logger.debug('Renaming sub file from {old} to {new}'.format + (old=sub, new=new_sub)) + renamed.append(new_sub) + try: + os.rename(sub, new_sub) + except Exception as error: + logger.error('Unable to rename sub file due to: {error}'.format(error=error)) + return