FF-1554: Fixed bitrate issue in convert audio

This commit is contained in:
John Andrews
2024-05-21 07:58:01 +12:00
parent 9b31e54272
commit f60f2b4340
8 changed files with 149 additions and 72 deletions
@@ -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
/// <returns>the output to call next</returns>
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);
+3 -17
View File
@@ -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;
}
+28 -10
View File
@@ -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);
+1 -1
View File
@@ -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);
+3 -3
View File
@@ -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,
+70
View File
@@ -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
{
/// <summary>
/// The test context instance
/// </summary>
private TestContext testContextInstance;
internal TestLogger Logger = new();
/// <summary>
/// Gets or sets the test context
/// </summary>
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
Binary file not shown.
Binary file not shown.