added filter to audio normalize node

This commit is contained in:
John Andrews
2022-03-25 18:53:39 +13:00
parent 9b1a8e0768
commit c8e6c7b1a5
3 changed files with 32 additions and 5 deletions

View File

@@ -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;

View File

@@ -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": {

View File

@@ -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" });