From c8e6c7b1a5bfa353b423926494dbf4d46aa29e80 Mon Sep 17 00:00:00 2001 From: John Andrews Date: Fri, 25 Mar 2022 18:53:39 +1300 Subject: [PATCH] added filter to audio normalize node --- VideoNodes/Tests/AudioNormalizationTests.cs | 8 ++++--- VideoNodes/VideoNodes.en.json | 6 +++++- VideoNodes/VideoNodes/AudioNormalization.cs | 23 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/VideoNodes/Tests/AudioNormalizationTests.cs b/VideoNodes/Tests/AudioNormalizationTests.cs index b23212a2..3d3d4108 100644 --- a/VideoNodes/Tests/AudioNormalizationTests.cs +++ b/VideoNodes/Tests/AudioNormalizationTests.cs @@ -57,9 +57,9 @@ namespace VideoNodes.Tests } [TestMethod] - public void AudioNormalization_Test_TwoPass_2() + public void AudioNormalization_Pattern_Test() { - const string file = @"Z:\Boston Legal\Season 1\Boston.Legal.S01E06.1080p.mkv"; + const string file = @"D:\videos\unprocessed\Masters of the Universe (1987) Bluray-1080p.mkv"; var logger = new TestLogger(); var vi = new VideoInfoHelper(@"C:\utils\ffmpeg\ffmpeg.exe", logger); var vii = vi.Read(file); @@ -67,7 +67,9 @@ namespace VideoNodes.Tests const string ffmpeg = @"C:\utils\ffmpeg\ffmpeg.exe"; AudioNormalization node = new(); - node.TwoPass = true; + node.AllAudio = true; + node.Pattern = "commentary"; + node.NotMatching = true; //node.OutputFile = file + ".sup"; var args = new FileFlows.Plugin.NodeParameters(file, logger, false, string.Empty); args.GetToolPathActual = (string tool) => ffmpeg; diff --git a/VideoNodes/VideoNodes.en.json b/VideoNodes/VideoNodes.en.json index 4769b7a8..b3bc6690 100644 --- a/VideoNodes/VideoNodes.en.json +++ b/VideoNodes/VideoNodes.en.json @@ -37,7 +37,11 @@ "AllAudio": "All Audio Tracks", "AllAudio-Help": "If all audio tracks should be normalized or if just the first track should be", "TwoPass": "Two Pass", - "TwoPass-Help": "If the audio tracks should use two pass normalization. This improves the normalization but increases the processing time." + "TwoPass-Help": "If the audio tracks should use two pass normalization. This improves the normalization but increases the processing time.", + "Pattern": "Pattern", + "Pattern-Help": "An optional regular expression to filter out audio tracks to normalize. Will match against the title, codec and language", + "NotMatching": "Not Matching", + "NotMatching-Help": "If the pattern should be used to exclude audio tracks from normalization, otherwise if the audio track matches they will be normalized." } }, "AudioTrackRemover": { diff --git a/VideoNodes/VideoNodes/AudioNormalization.cs b/VideoNodes/VideoNodes/AudioNormalization.cs index d1c70b4e..e66fcc24 100644 --- a/VideoNodes/VideoNodes/AudioNormalization.cs +++ b/VideoNodes/VideoNodes/AudioNormalization.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Text; using System.Text.Json; + using System.Text.RegularExpressions; public class AudioNormalization: EncodingNode { @@ -22,6 +23,12 @@ [Boolean(2)] public bool TwoPass { get; set; } + [TextVariable(3)] + public string Pattern { get; set; } + + [Boolean(4)] + public bool NotMatching { get; set; } + const string LOUDNORM_TARGET = "I=-24:LRA=7:TP=-2.0"; public override int Execute(NodeParameters args) @@ -56,6 +63,20 @@ for (int j = 0; j < videoInfo.AudioStreams.Count;j++) { var audio = videoInfo.AudioStreams[j]; + + if(string.IsNullOrEmpty(Pattern) == false) + { + string audioString = audio.Title + ":" + audio.Language + ":" + audio.Codec; + bool match = new Regex(Pattern, RegexOptions.IgnoreCase).IsMatch(audioString); + if (NotMatching) + match = !match; + if (match == false) + { + ffArgs.AddRange(new[] { "-map", $"0:a:{j}" }); + continue; + } + } + if (AllAudio || j == 0) { int sampleRate = audio.SampleRate > 0 ? audio.SampleRate : 48_000; @@ -70,7 +91,7 @@ } } else - ffArgs.Add($"-map 0:a:{j}"); + ffArgs.AddRange(new[] { "-map", $"0:a:{j}" }); } if (videoInfo.SubtitleStreams?.Any() == true) ffArgs.AddRange(new[] { "-map", "0:s" });