def _get_current_audio_language(self):
     # Get current audio language
     audio_list = self.player_state.get(STREAMS['audio']['list'])
     audio_language = common.get_kodi_audio_language(
         iso_format=xbmc.ISO_639_2)
     if audio_language == 'mediadefault':
         # Netflix do not have a "Media default" track then we rely on the language of current nf profile,
         # although due to current Kodi locale problems could be not always accurate.
         profile_language_code = G.LOCAL_DB.get_profile_config('language')
         audio_language = common.convert_language_iso(
             profile_language_code[0:2], xbmc.ISO_639_2)
     if audio_language == 'original':
         # Find the language of the original audio track
         stream = next(
             (audio_track
              for audio_track in audio_list if audio_track['isoriginal']),
             None)
         audio_language = stream['language']
     elif G.ADDON.getSettingBool('prefer_alternative_lang'):
         # Get the alternative language code
         # Here we have only the language code without country code, we do not know the country code to be used,
         # usually there are only two tracks with the same language and different countries,
         # then we try to find the language with the country code
         two_letter_lang_code = common.convert_language_iso(audio_language)
         stream = next(
             (audio_track for audio_track in audio_list
              if audio_track['language'].startswith(two_letter_lang_code +
                                                    '-')), None)
         if stream:
             audio_language = stream['language']
     return audio_language
 def _get_preferred_subtitle_language(self):
     """
     Get the language code of the preferred subtitle as set in Kodi Player setting
     :return: The language code (as ISO with 2 letters) or 'None' if disabled
     """
     subtitle_language = common.get_kodi_subtitle_language()
     if subtitle_language == 'forced_only':
         # Then match the audio language
         subtitle_language = self._get_preferred_audio_language()
     elif subtitle_language == 'original':
         # Get current audio languages
         audio_list = self.player_state.get(STREAMS['audio']['list'])
         # Find the language of the original audio track
         stream = next(
             (audio_track
              for audio_track in audio_list if audio_track['isoriginal']),
             None)
         # stream['language'] can be ISO 3 letters or with country code (pt-BR) / converted with LOCALE_CONV_TABLE
         if stream is None:
             subtitle_language = None
         else:
             if '-' in stream['language']:
                 subtitle_language = stream['language'][:2]
             else:
                 subtitle_language = common.convert_language_iso(
                     stream['language'])
     elif subtitle_language == 'default':
         # Get the Kodi UI language
         subtitle_language = common.get_kodi_ui_language()
     elif subtitle_language == 'none':
         # Subtitles are disabled
         subtitle_language = None
     return subtitle_language
 def _get_preferred_audio_language(self):
     """
     Get the language code of the preferred audio as set in Kodi Player setting
     :return: The language code (as ISO with 2 letters)
     """
     audio_language = common.get_kodi_audio_language()
     if audio_language == 'mediadefault':
         # Netflix do not have a "Media default" track then we rely on the language of current nf profile,
         # although due to current Kodi locale problems could be not always accurate.
         profile_language_code = G.LOCAL_DB.get_profile_config('language')
         audio_language = profile_language_code[:2]
     if audio_language == 'original':
         # Get current audio languages
         audio_list = self.player_state.get(STREAMS['audio']['list'])
         # Find the language of the original audio track
         stream = next(
             (audio_track
              for audio_track in audio_list if audio_track['isoriginal']),
             None)
         # stream['language'] can be ISO 3 letters or with country code (pt-BR) / converted with LOCALE_CONV_TABLE
         if stream is None:  # Means some problem, let the code break
             audio_language = None
         else:
             if '-' in stream['language']:
                 audio_language = stream['language'][:2]
             else:
                 audio_language = common.convert_language_iso(
                     stream['language'])
     return audio_language
 def _ensure_subtitles_no_audio_available(self):
     """Ensure in any case to show the regular subtitles when the preferred audio language is not available"""
     # Check if there are subtitles
     subtitles_list = self.player_state.get(STREAMS['subtitle']['list'])
     if not subtitles_list:
         return
     # Get the preferred audio language
     pref_audio_language = self._get_preferred_audio_language()
     audio_list = self.player_state.get(STREAMS['audio']['list'])
     # Check if there is an audio track available in the preferred audio language,
     # can also happen that in list there are languages with country code only
     accepted_lang_codes = [
         common.convert_language_iso(pref_audio_language, xbmc.ISO_639_2)
     ]
     if self.is_prefer_alternative_lang:
         lang_code = _find_lang_with_country_code(audio_list,
                                                  pref_audio_language)
         if lang_code:
             accepted_lang_codes.append(lang_code)
     stream = None
     if not any(audio_track['language'] in accepted_lang_codes
                for audio_track in audio_list):
         # No audio available in the preferred audio languages,
         # then try find a regular subtitle in the preferred audio language
         if len(accepted_lang_codes) == 2:
             # Try find with country code
             stream = self._find_subtitle_stream(accepted_lang_codes[-1])
         if not stream:
             stream = self._find_subtitle_stream(accepted_lang_codes[0])
     if stream:
         self.sc_settings.update({'subtitleenabled': True})
         self.sc_settings.update({'subtitle': stream})
예제 #5
0
def _find_audio_data(player_state, manifest):
    """
    Find the audio downloadable id and the audio track id
    """
    language = common.convert_language_iso(player_state['currentaudiostream']['language'])
    channels = AUDIO_CHANNELS_CONV[player_state['currentaudiostream']['channels']]

    for audio_track in manifest['audio_tracks']:
        if audio_track['language'] == language and audio_track['channels'] == channels:
            # Get the stream dict with the highest bitrate
            stream = max(audio_track['streams'], key=lambda x: x['bitrate'])
            return stream['downloadable_id'], audio_track['new_track_id']
    # Not found?
    raise Exception('build_media_tag: unable to find audio data with language: {}, channels: {}'
                    .format(language, channels))
예제 #6
0
def _find_audio_data(player_state, manifest):
    """
    Find the audio downloadable id and the audio track id
    """
    language = common.convert_language_iso(
        player_state['currentaudiostream']['language'])
    if not language:  # If there is no language, means that is a fixed locale (fix_locale_languages in kodi_ops.py)
        language = player_state['currentaudiostream']['language']
    channels = AUDIO_CHANNELS_CONV[player_state['currentaudiostream']
                                   ['channels']]
    for audio_track in manifest['audio_tracks']:
        if audio_track['language'] == language and audio_track[
                'channels'] == channels:
            # Get the stream dict with the highest bitrate
            stream = max(audio_track['streams'], key=lambda x: x['bitrate'])
            return stream['downloadable_id'], audio_track['new_track_id']
    # Not found?
    raise Exception(
        f'build_media_tag: unable to find audio data with language: {language}, channels: {channels}'
    )
 def _ensure_forced_subtitle_only(self):
     """Ensures the display of forced subtitles only with the preferred audio language set"""
     # When the audio language in Kodi player is set e.g. to 'Italian', and you try to play a video
     # without Italian audio language, Kodi choose another language available e.g. English,
     # this will also be reflected on the subtitles that which will be shown in English language,
     # but the subtitles may be available in Italian or the user may not want to view them in other languages.
     # Get current subtitle stream set (could be also changed by _select_lang_with_country_code)
     sub_stream = self.player_state.get(STREAMS['subtitle']['current'])
     if not sub_stream:
         return
     # Get the preferred audio language
     pref_audio_language = self._get_preferred_audio_language()
     # Get current audio languages
     audio_list = self.player_state.get(STREAMS['audio']['list'])
     if self.is_prefer_alternative_lang:
         lang_code = _find_lang_with_country_code(audio_list,
                                                  pref_audio_language)
         if lang_code:
             pref_audio_language = lang_code
     if '-' not in pref_audio_language:
         pref_audio_language = common.convert_language_iso(
             pref_audio_language, xbmc.ISO_639_2)
     if sub_stream['isforced'] and sub_stream[
             'language'] == pref_audio_language:
         return
     subtitles_list = self.player_state.get(STREAMS['subtitle']['list'])
     if not sub_stream['language'] == pref_audio_language:
         # The current subtitle is not forced or forced but not in the preferred audio language
         # Try find a forced subtitle in the preferred audio language
         stream = next((subtitle_track for subtitle_track in subtitles_list
                        if subtitle_track['language'] == pref_audio_language
                        and subtitle_track['isforced']), None)
         if stream:
             # Set the forced subtitle
             self.sc_settings.update({'subtitleenabled': True})
             self.sc_settings.update({'subtitle': stream})
         else:
             # Disable the subtitles
             self.sc_settings.update({'subtitleenabled': False})