diff --git a/AudioNodes/Nodes/ConvertFlowElements/ConvertNode.cs b/AudioNodes/Nodes/ConvertFlowElements/ConvertNode.cs index ef27cc05..852edc83 100644 --- a/AudioNodes/Nodes/ConvertFlowElements/ConvertNode.cs +++ b/AudioNodes/Nodes/ConvertFlowElements/ConvertNode.cs @@ -63,47 +63,44 @@ namespace FileFlows.AudioNodes codec, }; - if (Codec.ToLowerInvariant() == "mp3" || ogg || Codec.ToLowerInvariant() == "aac") + if(bitrate is > 10 and <= 20) { - if (bitrate is >= 10 and <= 20) + bool mp3 = Codec.Equals("mp3", StringComparison.InvariantCultureIgnoreCase); + bool aac = Codec.Equals("aac", StringComparison.InvariantCultureIgnoreCase); + if(mp3 == false && aac == false && ogg == false) + throw new Exception("Variable bitrate not supported in codec: " + Codec); + + bitrate = (Bitrate - 10); + if (mp3) { - bitrate = (Bitrate - 10); - if (Codec.ToLowerInvariant() == "mp3") - { - // ogg is reversed - bitrate = 10 - bitrate; - } - - - args.Logger?.ILog($"Using variable bitrate setting '{bitrate}' for codec '{Codec}'"); - - if (codec == "libfdk_aac") - { - ffArgs.AddRange(new[] - { - "-vbr", - Math.Min(Math.Max(1, bitrate / 2), 5).ToString() - }); - } - else - { - ffArgs.AddRange(new[] - { - "-qscale:a", - bitrate.ToString() - }); - } - - if (Codec == "aac" && HighEfficiency) - { - extension = "m4a"; - ffArgs.AddRange(new[] { "-profile:a", "aac_he_v2" }); - } + // ogg is reversed + bitrate = 10 - bitrate; + } + + args.Logger?.ILog($"Using variable bitrate setting '{bitrate}' for codec '{Codec}'"); + + if (codec == "libfdk_aac") + { + ffArgs.AddRange(new[] + { + "-vbr", + Math.Min(Math.Max(1, bitrate / 2), 5).ToString() + }); + } + else + { + ffArgs.AddRange(new[] + { + "-qscale:a", + bitrate.ToString() + }); + } + + if (Codec == "aac" && HighEfficiency) + { + extension = "m4a"; + ffArgs.AddRange(new[] { "-profile:a", "aac_he_v2" }); } - } - else if(bitrate is > 10 and <= 20) - { - throw new Exception("Variable bitrate not supported in codec: " + Codec); } else if (bitrate != 0) { @@ -275,9 +272,15 @@ namespace FileFlows.AudioNodes /// the output to call next public override int Execute(NodeParameters args) { - AudioInfo AudioInfo = GetAudioInfo(args); - if (AudioInfo == null) + var aiResult = GetAudioInfo(args); + if (aiResult.Failed(out string error)) + { + args.FailureReason = error; + args.Logger.ELog(error); return -1; + } + + AudioInfo AudioInfo = aiResult.Value; var ffmpegExeResult = GetFFmpeg(args); if (ffmpegExeResult.Failed(out string ffmpegError)) @@ -335,7 +338,7 @@ namespace FileFlows.AudioNodes ffArgs.Add(twoPass.Normalization); } } - + var metadata = MetadataHelper.GetMetadataParameters(AudioInfo); if (metadata?.Any() == true) ffArgs.AddRange(metadata); diff --git a/AudioNodes/Tests/AudioTestBase.cs b/AudioNodes/Tests/AudioTestBase.cs index 4a1fc745..a33569ed 100644 --- a/AudioNodes/Tests/AudioTestBase.cs +++ b/AudioNodes/Tests/AudioTestBase.cs @@ -5,25 +5,11 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace AudioNodes.Tests; -public abstract class AudioTestBase +public abstract class AudioTestBase : TestBase { - private TestContext testContextInstance; - - internal TestLogger logger = new (); - - public TestContext TestContext - { - get { return testContextInstance; } - set { testContextInstance = value; } - } - - protected readonly string ffmpeg = (OperatingSystem.IsLinux() ? "/usr/local/bin/ffmpeg" : @"C:\utils\ffmpeg\ffmpeg.exe"); - protected readonly string ffprobe = (OperatingSystem.IsLinux() ? "/usr/local/bin/ffprobe" : @"C:\utils\ffmpeg\ffprobe.exe"); - - protected NodeParameters GetNodeParameters(string file, bool isDirectory = false) { - var args = new FileFlows.Plugin.NodeParameters(file, logger, isDirectory, string.Empty, new LocalFileService()); + var args = new FileFlows.Plugin.NodeParameters(file, Logger, isDirectory, string.Empty, new LocalFileService()); args.GetToolPathActual = (string tool) => { @@ -33,7 +19,7 @@ public abstract class AudioTestBase return ffprobe; return null; }; - args.TempPath = @"/home/john/Music/temp"; + args.TempPath = TempPath; return args; } diff --git a/AudioNodes/Tests/ConvertTests.cs b/AudioNodes/Tests/ConvertTests.cs index b4afe8a5..5401ef60 100644 --- a/AudioNodes/Tests/ConvertTests.cs +++ b/AudioNodes/Tests/ConvertTests.cs @@ -3,6 +3,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System.IO; using AudioNodes.Tests; +using FileFlows.Plugin.Services; namespace FileFlows.AudioNodes.Tests; @@ -112,17 +113,34 @@ public class ConvertTests : AudioTestBase [TestMethod] - public void Convert_AacToMp3() + public void Convert_Mp3ToMp3_Bitrate() { + var args = GetNodeParameters(TestFile_Mp3); + var af = new AudioFile(); + af.PreExecute(args); + af.Execute(args); // need to read the Audio info and set it + + ConvertToMP3 ele = new(); + ele.PreExecute(args); + ele.Bitrate = 64; + int output = ele.Execute(args); + TestContext.WriteLine(Logger.ToString()); - const string file = @"D:\music\temp\37f315a0-4afc-4a72-a0b4-eb7eb681b9b3.aac"; - - ConvertToMP3 node = new(); - var args = new FileFlows.Plugin.NodeParameters(file, new TestLogger(), false, string.Empty, null);; - args.GetToolPathActual = (string tool) => @"C:\utils\ffmpeg\ffmpeg.exe"; - args.TempPath = @"D:\music\temp"; - new AudioFile().Execute(args); // need to read the Audio info and set it - int output = node.Execute(args); + Assert.AreEqual(1, output); + } + [TestMethod] + public void Convert_Mp3ToMp3_Bitrate_Variable() + { + var args = GetNodeParameters(TestFile_Mp3); + var af = new AudioFile(); + af.PreExecute(args); + af.Execute(args); // need to read the Audio info and set it + + ConvertToMP3 ele = new(); + ele.PreExecute(args); + ele.Bitrate = 3; + int output = ele.Execute(args); + TestContext.WriteLine(Logger.ToString()); Assert.AreEqual(1, output); } @@ -217,7 +235,7 @@ public class ConvertTests : AudioTestBase node.PreExecute(args); int output = node.Execute(args); - string log = logger.ToString(); + string log = Logger.ToString(); TestContext.WriteLine(log); Assert.AreEqual(1, output); diff --git a/AudioNodes/Tests/CreateAudioBookTests.cs b/AudioNodes/Tests/CreateAudioBookTests.cs index 0121d090..81b5300d 100644 --- a/AudioNodes/Tests/CreateAudioBookTests.cs +++ b/AudioNodes/Tests/CreateAudioBookTests.cs @@ -62,7 +62,7 @@ public class CreateAudioBookTests : AudioTestBase int output = node.Execute(args); - var log = logger.ToString(); + var log = Logger.ToString(); TestContext.WriteLine(log); Assert.AreEqual(expected, output); diff --git a/AudioNodes/Tests/EmbedArtworkTests.cs b/AudioNodes/Tests/EmbedArtworkTests.cs index 9eef57a1..9520c6a6 100644 --- a/AudioNodes/Tests/EmbedArtworkTests.cs +++ b/AudioNodes/Tests/EmbedArtworkTests.cs @@ -76,14 +76,14 @@ public class EmbedArtworkTests : AudioTestBase convertNode.PreExecute(args); var result = convertNode.Execute(args); - var log = logger.ToString(); + var log = Logger.ToString(); Assert.AreEqual(1, result); - logger.Clear(); + Logger.Clear(); var ele = new EmbedArtwork(); var output = ele.Execute(args); - log = logger.ToString(); + log = Logger.ToString(); TestContext.WriteLine(log); Assert.AreEqual(1, output); System.IO.File.Move(args.WorkingFile, diff --git a/AudioNodes/Tests/_TestBase.cs b/AudioNodes/Tests/_TestBase.cs new file mode 100644 index 00000000..cffa168e --- /dev/null +++ b/AudioNodes/Tests/_TestBase.cs @@ -0,0 +1,70 @@ +#if(DEBUG) + +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.IO; + +namespace FileFlows.AudioNodes.Tests; + +[TestClass] +public abstract class TestBase +{ + /// + /// The test context instance + /// + private TestContext testContextInstance; + + internal TestLogger Logger = new(); + + /// + /// Gets or sets the test context + /// + public TestContext TestContext + { + get => testContextInstance; + set => testContextInstance = value; + } + + public string TestPath { get; private set; } + public string TempPath { get; private set; } + public string FfmpegPath { get; private set; } + + public readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + public readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + + protected readonly string ffmpeg = (OperatingSystem.IsLinux() ? "/usr/local/bin/ffmpeg" : @"C:\utils\ffmpeg\ffmpeg.exe"); + protected readonly string ffprobe = (OperatingSystem.IsLinux() ? "/usr/local/bin/ffprobe" : @"C:\utils\ffmpeg\ffprobe.exe"); + + + [TestInitialize] + public void TestInitialize() + { + this.TestPath = this.TestPath?.EmptyAsNull() ?? (IsLinux ? "~/src/ff-files/test-files/audio" : @"d:\audio\testfiles"); + this.TempPath = this.TempPath?.EmptyAsNull() ?? (IsLinux ? "~/src/ff-files/temp" : @"d:\audio\temp"); + this.FfmpegPath = this.FfmpegPath?.EmptyAsNull() ?? (IsLinux ? "/usr/local/bin/ffmpeg" : @"C:\utils\ffmpeg\ffmpeg.exe"); + + this.TestPath = this.TestPath.Replace("~/", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/"); + this.TempPath = this.TempPath.Replace("~/", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/"); + this.FfmpegPath = this.FfmpegPath.Replace("~/", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/"); + + if (Directory.Exists(this.TempPath) == false) + Directory.CreateDirectory(this.TempPath); + } + + [TestCleanup] + public void CleanUp() + { + TestContext.WriteLine(Logger.ToString()); + } + + protected virtual void TestStarting() + { + + } + + protected string TestFile_Mp3 => Path.Combine(TestPath, "mp3.mp3"); + protected string TestFile_Flac => Path.Combine(TestPath, "flac.flac"); + protected string TestFile_Wav => Path.Combine(TestPath, "wav.wav"); +} + +#endif \ No newline at end of file diff --git a/FileFlows.Plugin.dll b/FileFlows.Plugin.dll index b517791c..29c46d2f 100644 Binary files a/FileFlows.Plugin.dll and b/FileFlows.Plugin.dll differ diff --git a/FileFlows.Plugin.pdb b/FileFlows.Plugin.pdb index 221f945e..41f02912 100644 Binary files a/FileFlows.Plugin.pdb and b/FileFlows.Plugin.pdb differ