diff --git a/resources/lib/play_utils.py b/resources/lib/play_utils.py index 3d90482..955fd7a 100644 --- a/resources/lib/play_utils.py +++ b/resources/lib/play_utils.py @@ -858,24 +858,57 @@ def audio_subs_pref(url, list_item, media_source, item_id, audio_stream_index, s # Auto-select audio track based on preferences if select_audio_index is None and len(audio_streams_data) > 0: auto_selected = None + best_score = -1 - # Try to match preferred language + # Try to match preferred language with scoring if preferred_audio_lang: for stream in audio_streams_data: + score = 0 stream_lang = stream.get('Language', '').lower() + # Match against common variations (ger, deu, de, german) if preferred_audio_lang in stream_lang or stream_lang.startswith(preferred_audio_lang[:3]): - auto_selected = stream['Index'] - log.debug("Auto-selected audio by language: {0} (index {1})".format(stream_lang, auto_selected)) - break + score += 100 # Language match - # Fall back to default audio if enabled + # Bonus for default track + if stream.get('IsDefault', False): + score += 50 + + # Bonus based on channel count (more channels = better quality) + channels = stream.get('Channels', 2) + score += min(channels * 5, 40) # Max +40 for 8 channels + + # Bonus for high-quality codecs + codec = stream.get('Codec', '').lower() + if 'truehd' in codec or 'dts-hd' in codec or 'dts-ma' in codec: + score += 30 + elif 'dts' in codec: + score += 20 + elif 'ac3' in codec or 'eac3' in codec: + score += 10 + + # Penalty for commentary tracks + title = stream.get('Title', '').lower() + display_title = stream.get('DisplayTitle', '').lower() + if 'commentary' in title or 'kommentar' in title or 'commentary' in display_title: + score -= 100 # Effectively exclude commentary + + log.debug("Audio score for {0} ({1}): {2} (channels={3}, codec={4}, default={5})".format( + stream_lang, stream['Index'], score, channels, codec, stream.get('IsDefault', False))) + + if score > best_score: + best_score = score + auto_selected = stream['Index'] + + # Fall back to default audio if enabled and no language match if auto_selected is None and auto_select_default_audio: auto_selected = default_audio log.debug("Auto-selected default audio (index {0})".format(auto_selected)) if auto_selected is not None: select_audio_index = auto_selected + if best_score > 0: + log.debug("Auto-selected audio (index {0}, score {1})".format(auto_selected, best_score)) # Auto-select subtitle track based on preferences if select_subs_index is None and len(subtitle_streams_data) > 0: