Add subs renaming for radarr/sonarr (#1824)

* Re-added rename_subs #1823 #768
This commit is contained in:
Clinton Hall 2021-04-10 19:37:32 +12:00 committed by GitHub
commit e3efbdbaee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 2 deletions

View file

@ -21,7 +21,7 @@ from core.auto_process.common import (
completed_download_handling, completed_download_handling,
) )
from core.plugins.downloaders.nzb.utils import report_nzb 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.scene_exceptions import process_all_exceptions
from core.utils import ( from core.utils import (
convert_to_ascii, convert_to_ascii,
@ -130,6 +130,7 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual',
num_files += 1 num_files += 1
if transcoder.is_video_good(video, status): if transcoder.is_video_good(video, status):
import_subs(video) import_subs(video)
rename_subs(dir_name)
good_files += 1 good_files += 1
if num_files and good_files == num_files: if num_files and good_files == num_files:
if status: if status:

View file

@ -26,7 +26,7 @@ from core.auto_process.common import (
) )
from core.auto_process.managers.sickbeard import InitSickBeard from core.auto_process.managers.sickbeard import InitSickBeard
from core.plugins.downloaders.nzb.utils import report_nzb 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.scene_exceptions import process_all_exceptions
from core.utils import ( from core.utils import (
convert_to_ascii, 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): if transcoder.is_video_good(video, status):
good_files += 1 good_files += 1
import_subs(video) import_subs(video)
rename_subs(dir_name)
if num_files > 0: if num_files > 0:
if good_files == num_files and not status == 0: if good_files == num_files and not status == 0:
logger.info('Found Valid Videos. Setting status Success') logger.info('Found Valid Videos. Setting status Success')

View file

@ -12,6 +12,7 @@ import core
from core import logger from core import logger
import os import os
import re
for provider in subliminal.provider_manager.internal_extensions: for provider in subliminal.provider_manager.internal_extensions:
if provider not in [str(x) for x in subliminal.provider_manager.list_entry_points()]: 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) os.chmod(subtitle_path, 0o644)
except Exception as e: except Exception as e:
logger.error('Failed to download subtitles for {0} due to: {1}'.format(filename, e), 'SUBTITLES') 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