From c832c8e860a573396d59e3f082c2c335796c7c09 Mon Sep 17 00:00:00 2001 From: Jason Dove Date: Fri, 24 Feb 2023 15:45:30 -0600 Subject: [PATCH] prioritize default audio tracks (#1178) --- CHANGELOG.md | 4 ++- ErsatzTV.Core/FFmpeg/FFmpegStreamSelector.cs | 32 ++++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f5f844fc..fda5403f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ - Changelog +# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). @@ -17,6 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Streaming from disk is preferred, so every playback attempt will first check the local file system - Use libvpl instead of libmfx to provide intel acceleration in vaapi docker images - Search queries no longer remove duplicate results as this was causing incorrect behavior +- Prioritize audio streams that are flagged as "default" over number of audio channels + - For example, a video with a stereo commentary track and a mono "default" track will now prefer the "default" track ## [0.7.4-beta] - 2023-02-12 ### Added diff --git a/ErsatzTV.Core/FFmpeg/FFmpegStreamSelector.cs b/ErsatzTV.Core/FFmpeg/FFmpegStreamSelector.cs index bec6c14ea..e5f5e1a9b 100644 --- a/ErsatzTV.Core/FFmpeg/FFmpegStreamSelector.cs +++ b/ErsatzTV.Core/FFmpeg/FFmpegStreamSelector.cs @@ -199,7 +199,7 @@ public class FFmpegStreamSelector : IFFmpegStreamSelector private Option DefaultSelectAudioStream( MediaVersion version, - List preferredLanguageCodes, + IReadOnlyCollection preferredLanguageCodes, string preferredAudioTitle) { var audioStreams = version.Streams.Filter(s => s.MediaStreamKind == MediaStreamKind.Audio).ToList(); @@ -232,8 +232,7 @@ public class FFmpegStreamSelector : IFFmpegStreamSelector { if (string.IsNullOrWhiteSpace(title)) { - _logger.LogDebug("No audio title has been specified; selecting stream with most channels"); - return streams.OrderByDescending(s => s.Channels).ThenByDescending(s => s.Default).Head(); + return PrioritizeDefault(streams); } // prioritize matching titles @@ -243,18 +242,31 @@ public class FFmpegStreamSelector : IFFmpegStreamSelector if (matchingTitle.Any()) { _logger.LogDebug( - "Found {Count} audio streams with preferred title {Title}; selecting stream with most channels", + "Found {Count} audio streams with preferred title {Title}", matchingTitle.Count, title); - return matchingTitle.OrderByDescending(s => s.Channels).ThenByDescending(s => s.Default).Head(); + return PrioritizeDefault(streams); + } + + _logger.LogDebug("Unable to find audio stream with preferred title {Title}", title); + + return PrioritizeDefault(streams); + } + + private Option PrioritizeDefault(IReadOnlyCollection streams) + { + var sorted = streams.OrderByDescending(s => s.Channels).ToList(); + Option maybeDefault = Optional(sorted.Find(s => s.Default)); + foreach (MediaStream stream in maybeDefault) + { + _logger.LogDebug("Found audio stream flagged as default"); + return stream; } - _logger.LogDebug( - "Unable to find audio stream with preferred title {Title}; selecting stream with most channels", - title); + _logger.LogDebug("Unable to find default audio stream; selecting stream with most channels"); - return streams.OrderByDescending(s => s.Channels).ThenByDescending(s => s.Default).Head(); + return streams.HeadOrNone(); } private async Task> SelectEpisodeAudioStream( @@ -310,7 +322,7 @@ public class FFmpegStreamSelector : IFFmpegStreamSelector _logger.LogDebug("Checking for JS Script at {Path}", jsScriptPath); if (!_localFileSystem.FileExists(jsScriptPath)) { - _logger.LogWarning("Unable to locate movie audio stream selector script; falling back to built-in logic"); + _logger.LogInformation("Unable to locate movie audio stream selector script; falling back to built-in logic"); return Option.None; }