From 394fe00b2549a08c58e8d9f44a30a492077dd0fc15ccd3350af12436df8dcf37 Mon Sep 17 00:00:00 2001 From: copecone Date: Tue, 6 Oct 2026 13:51:52 +0900 Subject: [PATCH] Refactor Audio Codec & Sample Rate Detection --- Core/RenderManager.cs | 16 ++++------------ UI/Window/RenderConfigWindow.cs | 19 +++++++++++++++++-- Util/FFmpeg/Codec.cs | 22 +++++++++++++++++++++- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Core/RenderManager.cs b/Core/RenderManager.cs index c531035..b491f1c 100644 --- a/Core/RenderManager.cs +++ b/Core/RenderManager.cs @@ -364,9 +364,10 @@ internal class RenderManager : MonoBehaviour private IEnumerator SetupPlay() { - if (Configuration.AudioConfig.RenderAudio && Configuration.VideoConfig.Codec is Codec.VP9 or Codec.AV1 or Codec.VVC) + if (Configuration.AudioConfig.RenderAudio) { - if (Configuration.AudioConfig.AudioSampleRate == 44100) Configuration.AudioConfig.AudioSampleRate = 48000; + if (!CodecUtil.AvailableSampleRate[CodecUtil.AudioCodecMatch[Configuration.VideoConfig.Codec]].Contains(Configuration.AudioConfig.AudioSampleRate)) + Configuration.AudioConfig.AudioSampleRate = 48000; // most compatible value } for (; countdown > 0; countdown--) @@ -463,7 +464,7 @@ internal class RenderManager : MonoBehaviour try { var effectiveVideoFps = Math.Min(videoFPS, renderFPS); - var codecId = AudioCodecMatch[Configuration.VideoConfig.Codec]; + var codecId = CodecUtil.AudioCodecMatch[Configuration.VideoConfig.Codec]; var settings = new FFmpegNativeEncoder.EncoderSettings { Width = Configuration.RenderWidth, @@ -816,13 +817,4 @@ internal class RenderManager : MonoBehaviour Melon.Logger.Error($"OnLastUpdate callback caused error: {err}"); } } - - private static readonly Dictionary AudioCodecMatch = new() - { - [Codec.H264] = AVCodecID.AV_CODEC_ID_AAC, - [Codec.H265] = AVCodecID.AV_CODEC_ID_AAC, - [Codec.VP9] = AVCodecID.AV_CODEC_ID_OPUS, - [Codec.AV1] = AVCodecID.AV_CODEC_ID_OPUS, - [Codec.VVC] = AVCodecID.AV_CODEC_ID_OPUS - }; } \ No newline at end of file diff --git a/UI/Window/RenderConfigWindow.cs b/UI/Window/RenderConfigWindow.cs index e803a68..835995b 100644 --- a/UI/Window/RenderConfigWindow.cs +++ b/UI/Window/RenderConfigWindow.cs @@ -188,8 +188,12 @@ internal static class RenderConfigWindow Codec.H264, config.VideoConfig.Codec, codecs, c => c.ToString(), "dd_video_codec", - c => config.VideoConfig.Codec = c - ); + c => + { + config.VideoConfig.Codec = c; + UpdateVideoCodec(c); + }); + _bitrateInput = CreateInputField( ctx, CreateFieldSlot(videoLeft), LocalizationManager.Get("ui.render_config.bitrate"), @@ -198,6 +202,7 @@ internal static class RenderConfigWindow TMPro.TMP_InputField.CharacterValidation.Integer, val => { if (int.TryParse(val, out var b) && b > 0) config.VideoConfig.Bitrate = b; } ); + _qpInput = CreateInputField( ctx, CreateFieldSlot(videoLeft), LocalizationManager.Get("ui.render_config.qp"), @@ -216,6 +221,7 @@ internal static class RenderConfigWindow "dd_rate_control", m => config.VideoConfig.RateControlMode = m ); + _maxBitrateInput = CreateInputField( ctx, CreateFieldSlot(videoRight), LocalizationManager.Get("ui.render_config.max_bitrate"), @@ -384,6 +390,7 @@ internal static class RenderConfigWindow "dd_adv_file_extension", e => { if (!string.IsNullOrEmpty(e)) config.AdvancedConfig.FileExtension = e; } ); + _processAudioToggle = CreateToggleField( ctx, CreateFieldSlot(advLeft), "", true, config.AdvancedConfig.ProcessAudio, @@ -405,6 +412,7 @@ internal static class RenderConfigWindow else if (string.IsNullOrWhiteSpace(val)) config.AdvancedConfig.RandomSeed = null; } ); + _nonTileToggle = CreateToggleField( ctx, CreateFieldSlot(advRight), "", false, config.AdvancedConfig.NonTile, @@ -422,6 +430,13 @@ internal static class RenderConfigWindow "btn_go_render", height: 48f ); + + return; + + void UpdateVideoCodec(Codec codec) + { + _sampleRateDropdown?.SetValues(CodecUtil.AvailableSampleRate[CodecUtil.AudioCodecMatch[codec]]); + } } private static void EnsureDefaultConfig(RenderConfig config) diff --git a/Util/FFmpeg/Codec.cs b/Util/FFmpeg/Codec.cs index e74de90..42d93a4 100644 --- a/Util/FFmpeg/Codec.cs +++ b/Util/FFmpeg/Codec.cs @@ -1,4 +1,6 @@ -using System.Diagnostics.CodeAnalysis; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using FFmpeg.AutoGen; namespace ADOFAIRenderer.Util.FFmpeg { @@ -7,4 +9,22 @@ namespace ADOFAIRenderer.Util.FFmpeg { H264, H265, VP9, AV1, VVC } + + internal static class CodecUtil + { + public static readonly Dictionary AudioCodecMatch = new() + { + [Codec.H264] = AVCodecID.AV_CODEC_ID_AAC, + [Codec.H265] = AVCodecID.AV_CODEC_ID_AAC, + [Codec.VP9] = AVCodecID.AV_CODEC_ID_OPUS, + [Codec.AV1] = AVCodecID.AV_CODEC_ID_OPUS, + [Codec.VVC] = AVCodecID.AV_CODEC_ID_OPUS + }; + + public static readonly Dictionary> AvailableSampleRate = new() + { + [AVCodecID.AV_CODEC_ID_AAC] = new List { 44100, 48000, 96000 }, + [AVCodecID.AV_CODEC_ID_OPUS] = new List { 48000, 96000 } + }; + } } \ No newline at end of file