-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor get_subtitle_sources #136
Open
zrose584
wants to merge
2
commits into
user234683:master
Choose a base branch
from
zrose584:multilingual
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,7 +168,7 @@ def video_rank(src): | |
|
||
|
||
|
||
def make_caption_src(info, lang, auto=False, trans_lang=None): | ||
def make_caption_src(info, lang, score, auto=False, trans_lang=None): | ||
label = lang | ||
if auto: | ||
label += ' (Automatic)' | ||
|
@@ -179,6 +179,9 @@ def make_caption_src(info, lang, auto=False, trans_lang=None): | |
'label': label, | ||
'srclang': trans_lang[0:2] if trans_lang else lang[0:2], | ||
'on': False, | ||
'score': score, | ||
'lang': lang, | ||
'auto': auto, | ||
} | ||
|
||
def lang_in(lang, sequence): | ||
|
@@ -195,83 +198,68 @@ def lang_eq(lang1, lang2): | |
return False | ||
return lang1[0:2] == lang2[0:2] | ||
|
||
def equiv_lang_in(lang, sequence): | ||
'''Extracts a language in sequence which is equivalent to lang. | ||
e.g. if lang is en, extracts en-GB from sequence. | ||
Necessary because if only a specific variant like en-GB is available, can't ask Youtube for simply en. Need to get the available variant.''' | ||
lang = lang[0:2] | ||
for l in sequence: | ||
if l[0:2] == lang: | ||
return l | ||
return None | ||
|
||
def get_subtitle_sources(info): | ||
'''Returns these sources, ordered from least to most intelligible: | ||
native_video_lang (Automatic) | ||
foreign_langs (Manual) | ||
|
||
native_video_lang (Automatic) -> pref_lang | ||
foreign_langs (Manual) -> pref_lang | ||
native_video_lang (Manual) -> pref_lang | ||
|
||
pref_lang (Automatic) | ||
pref_lang (Manual)''' | ||
sources = [] | ||
if not yt_data_extract.captions_available(info): | ||
return [] | ||
pref_lang = settings.subtitles_language | ||
|
||
user_langs = settings.subtitles_language.split(',') | ||
native_video_lang = None | ||
if info['automatic_caption_languages']: | ||
native_video_lang = info['automatic_caption_languages'][0] | ||
|
||
highest_fidelity_is_manual = False | ||
kManual, kNativeVideoLang, kUserSpeaksIt = 1, 2, 4 | ||
def score_lang(lang): | ||
score = 0 | ||
if lang_in(lang, user_langs): | ||
score |= kUserSpeaksIt | ||
if lang_eq(lang, native_video_lang): | ||
score |= kNativeVideoLang | ||
return score | ||
|
||
# Sources are added in very specific order outlined above | ||
# More intelligible sources are put further down to avoid browser bug when there are too many languages | ||
# (in firefox, it is impossible to select a language near the top of the list because it is cut off) | ||
for lang in info['manual_caption_languages']: | ||
score = score_lang(lang) | kManual | ||
sources.append(make_caption_src(info, lang, score)) | ||
|
||
# native_video_lang (Automatic) | ||
if native_video_lang and not lang_eq(native_video_lang, pref_lang): | ||
sources.append(make_caption_src(info, native_video_lang, auto=True)) | ||
for lang in info['automatic_caption_languages']: | ||
score = score_lang(lang) | ||
if score: | ||
sources.append(make_caption_src(info, lang, score, auto=True)) | ||
|
||
# foreign_langs (Manual) | ||
for lang in info['manual_caption_languages']: | ||
if not lang_eq(lang, pref_lang): | ||
sources.append(make_caption_src(info, lang)) | ||
|
||
if (lang_in(pref_lang, info['translation_languages']) | ||
and not lang_in(pref_lang, info['automatic_caption_languages']) | ||
and not lang_in(pref_lang, info['manual_caption_languages'])): | ||
# native_video_lang (Automatic) -> pref_lang | ||
if native_video_lang and not lang_eq(pref_lang, native_video_lang): | ||
sources.append(make_caption_src(info, native_video_lang, auto=True, trans_lang=pref_lang)) | ||
|
||
# foreign_langs (Manual) -> pref_lang | ||
for lang in info['manual_caption_languages']: | ||
if not lang_eq(lang, native_video_lang) and not lang_eq(lang, pref_lang): | ||
sources.append(make_caption_src(info, lang, trans_lang=pref_lang)) | ||
|
||
# native_video_lang (Manual) -> pref_lang | ||
if lang_in(native_video_lang, info['manual_caption_languages']): | ||
sources.append(make_caption_src(info, native_video_lang, trans_lang=pref_lang)) | ||
|
||
# pref_lang (Automatic) | ||
if lang_in(pref_lang, info['automatic_caption_languages']): | ||
sources.append(make_caption_src(info, equiv_lang_in(pref_lang, info['automatic_caption_languages']), auto=True)) | ||
|
||
# pref_lang (Manual) | ||
if lang_in(pref_lang, info['manual_caption_languages']): | ||
sources.append(make_caption_src(info, equiv_lang_in(pref_lang, info['manual_caption_languages']))) | ||
highest_fidelity_is_manual = True | ||
|
||
if sources and sources[-1]['srclang'] == pref_lang: | ||
# set as on by default since it's manual a default-on subtitles mode is in settings | ||
if highest_fidelity_is_manual and settings.subtitles_mode > 0: | ||
if not any(s['score'] & kUserSpeaksIt for s in sources): | ||
for user_lang in user_langs: | ||
if not lang in info['translation_languages']: | ||
continue | ||
|
||
for s in sources[:]: | ||
lang, score, auto = s['lang'], s['score'] | kUserSpeaksIt, s['auto'] | ||
sources.append(make_caption_src(info, lang, score, auto=auto, trans_lang=user_lang)) | ||
|
||
sources = sorted(sources, key=lambda x: x['score']) | ||
|
||
if len(sources) == 0: | ||
assert len(info['automatic_caption_languages']) == 0 and len(info['manual_caption_languages']) == 0 | ||
|
||
if sources and settings.subtitles_mode > 0 and sources[-1]['score'] & kUserSpeaksIt: | ||
if sources[-1]['score'] & kManual: | ||
# set as on by default since it's manual a default-on subtitles mode is in settings | ||
sources[-1]['on'] = True | ||
# set as on by default since settings indicate to set it as such even if it's not manual | ||
elif settings.subtitles_mode == 2: | ||
# set as on by default since settings indicate to set it as such even if it's not manual | ||
sources[-1]['on'] = True | ||
|
||
if len(sources) == 0: | ||
assert len(info['automatic_caption_languages']) == 0 and len(info['manual_caption_languages']) == 0 | ||
if settings.video_player == 1: # Plyr | ||
sources[-1]['on'] = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we always turning captions on for Plyr only? |
||
|
||
return sources | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
kNativeVideoLang, kManual, kUserSpeaksIt = 1, 2, 4
?Manually created subtitles in a different language than the video are always going to be higher quality than automatically generated ones. Comparing the current scores to the comment at the top of the code with the ideal ordering:
Swapping the last two bits will fix it
Additionally, these constants should be changed to
NATIVE_VIDEO_LANG, MANUAL
, etc. to match the convention in the rest of the code that constants are indicated with uppercase snakecase; we dont use hungarian notation in the code.