From 1172e5e275b1d0de097d342a2e0dc0b712aa05bf Mon Sep 17 00:00:00 2001 From: girst Date: Tue, 21 Jul 2020 14:48:55 +0200 Subject: [PATCH] improve default subtitle handling was broken when autogenerated was available, due to sorted() --- app/common/common.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/common/common.py b/app/common/common.py index 648540d..e2f49a0 100644 --- a/app/common/common.py +++ b/app/common/common.py @@ -250,23 +250,23 @@ def prepare_metadata(metadata): # https://www.youtube.com/api/timedtext?lang=&v=&fmt={srv1|srv2|srv3|ttml|vtt}, # but that won't give us autogenerated subtitles (and is an extra request). # we can still add &fmt= to the extracted URLs below (first one takes precedence). + try: # find the native language captions (assuming there is only 1 audioTrack) (any level might not exist): + default_track = metadata.get('captions',{}).get('playerCaptionsTracklistRenderer',{}).get('defaultAudioTrackIndex', 0) + main_subtitle = metadata['captions']['playerCaptionsTracklistRenderer']['audioTracks'][default_track]['defaultCaptionTrackIndex'] + except: + main_subtitle = -1 subtitles = sorted([ {'url':cc['baseUrl'], 'code':cc['languageCode'], 'autogenerated':cc.get('kind')=="asr", 'name':cc['name']['simpleText'], + 'default':i==main_subtitle, 'query':"fmt=vtt&"+urlparse(cc['baseUrl']).query} # for our internal proxy - for cc in metadata.get('captions',{}) + for i,cc in enumerate(metadata.get('captions',{}) .get('playerCaptionsTracklistRenderer',{}) - .get('captionTracks',[]) - ], key=lambda cc: cc['autogenerated']) - - try: # any level might not exist - # find the native language captions (assuming there is only 1 audioTrack): - main_subtitle = metadata['captions']['playerCaptionsTracklistRenderer']['audioTracks'][0]['defaultCaptionTrackIndex'] - # and move it to the first place: - subtitles.insert(0, subtitles.pop(main_subtitle)) - except: pass + .get('captionTracks',[])) + # sort order: default lang gets weight 0 (first), other manually translated weight 1, autogenerated weight 2: + ], key=lambda cc: (not cc['default']) + cc['autogenerated']) def clean_url(url): # externals URLs are redirected through youtube.com/redirect, but we -- 2.39.3