From 4a7144cd92d92faee6a11743cf7bdb960bf9b60a Mon Sep 17 00:00:00 2001 From: John Andrews Date: Wed, 26 Jul 2023 13:55:34 +1200 Subject: [PATCH] FF-1016 - fixing opus side by side --- .../Audio/FfmpegBuilderAudioAddTrack.cs | 40 +++++++++++++++++-- .../Audio/FfmpegBuilderAudioConvert.cs | 16 +++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioAddTrack.cs b/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioAddTrack.cs index 10198708..8a8cb008 100644 --- a/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioAddTrack.cs +++ b/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioAddTrack.cs @@ -201,7 +201,7 @@ public class FfmpegBuilderAudioAddTrack : FfmpegBuilderNode else { int sampleRate = SampleRate == 1 ? audio.Stream.SampleRate : SampleRate; - audio.EncodingParameters.AddRange(GetNewAudioTrackParameters(Codec, Channels, Bitrate, sampleRate)); + audio.EncodingParameters.AddRange(GetNewAudioTrackParameters(args, Codec, Channels, Bitrate, sampleRate)); if (this.Channels > 0) audio.Channels = this.Channels; } @@ -285,14 +285,16 @@ public class FfmpegBuilderAudioAddTrack : FfmpegBuilderNode /// /// Gets hte new audio track parameters /// + /// the node parameters /// the codec of the new track /// the channels of the new track /// the bitrate of the new track /// the sample rate /// the new track parameters - internal static string[] GetNewAudioTrackParameters(string codec, float channels, int bitrate, int sampleRate) + internal static string[] GetNewAudioTrackParameters(NodeParameters args, string codec, float channels, int bitrate, int sampleRate) { - if (codec == "opus") + bool opus = codec == "opus"; + if (opus) codec = "libopus"; // Prepare the options list @@ -309,6 +311,36 @@ public class FfmpegBuilderAudioAddTrack : FfmpegBuilderNode options.Add("-ac:a:{index}"); options.Add(channels.ToString()); } + else if (opus) + { + // FF-1016: Opus needs this for side by side channel layout + args.Logger?.ILog("OPUS Audio"); + if (Math.Abs(channels - 61) < 1) + { + args.Logger?.ILog("Channels 61 detected setting to 8"); + options.AddRange(new[] { "-ac:a:{index}", "8" }); + } + else if (channels is > 5 and <= 6 || Math.Abs(channels - 51) < 1) + { + args.Logger?.ILog("Channels between 5 and 6 or 50/51 detected setting to 6"); + options.AddRange(new[] { "-ac:a:{index}", "6" }); + } + else if (channels is >= 4 and <= 5 || Math.Abs(channels - 40) < 2) + { + args.Logger?.ILog("Channels between 4 and 5 or 40/41 detected setting to 4"); + options.AddRange(new[] { "-ac:a:{index}", "4" }); + } + else if (channels == 0) + { + args.Logger?.ILog("No channels detected setting to 4"); + options.AddRange(new[] { "-ac:a:{index}", "2" }); + } + else if (Math.Abs(channels - 5) < 0.5) + { + args.Logger?.ILog("Channels 5 detected setting to 5"); + options.AddRange(new[] { "-ac:a:{index}", "5" }); + } + } // Handle bitrate if (bitrate > 0) @@ -323,6 +355,8 @@ public class FfmpegBuilderAudioAddTrack : FfmpegBuilderNode options.Add("-ar:a:{index}"); options.Add(sampleRate.ToString()); } + + args.Logger.ILog("New Audo Arguments: " + string.Join(" ", options)); return options.ToArray(); } diff --git a/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioConvert.cs b/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioConvert.cs index fbcad09f..edd8ba08 100644 --- a/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioConvert.cs +++ b/VideoNodes/FfmpegBuilderNodes/Audio/FfmpegBuilderAudioConvert.cs @@ -104,7 +104,7 @@ public class FfmpegBuilderAudioConverter : FfmpegBuilderNode if (string.IsNullOrEmpty(this.Pattern)) { - converting |= ConvertTrack(track); + converting |= ConvertTrack(args, track); continue; } @@ -120,14 +120,20 @@ public class FfmpegBuilderAudioConverter : FfmpegBuilderNode matches = !matches; if (matches) { - converting |= ConvertTrack(track); + converting |= ConvertTrack(args, track); } } } return converting ? 1: 2; } - - private bool ConvertTrack(FfmpegAudioStream stream) + + /// + /// Converts and audio track + /// + /// the node arguments + /// teh stream to convert + /// if the stream had to be converted or not + private bool ConvertTrack(NodeParameters args, FfmpegAudioStream stream) { bool codecSame = stream.Stream.Codec?.ToLower() == Codec?.ToLower(); bool channelsSame = Channels == 0 || Channels == stream.Stream.Channels; @@ -136,7 +142,7 @@ public class FfmpegBuilderAudioConverter : FfmpegBuilderNode if (codecSame && channelsSame && bitrateSame) return false; - stream.EncodingParameters.AddRange(FfmpegBuilderAudioAddTrack.GetNewAudioTrackParameters(Codec, Channels, Bitrate, 0)); + stream.EncodingParameters.AddRange(FfmpegBuilderAudioAddTrack.GetNewAudioTrackParameters(args, Codec, Channels, Bitrate, 0)); return true; } }