FF-1020 - added sample rate to add audio track

This commit is contained in:
John Andrews
2023-07-21 23:25:48 +12:00
parent 63ca139fc3
commit 9a813b5085
5 changed files with 130 additions and 81 deletions
+36 -38
View File
@@ -1,47 +1,45 @@
namespace FileFlows.BasicNodes.File
namespace FileFlows.BasicNodes.File;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public class FileExists: Node
{
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public override int Inputs => 1;
public override int Outputs => 2;
public class FileExists: Node
public override FlowElementType Type => FlowElementType.Logic;
public override string Icon => "fas fa-question-circle";
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/file-exists";
[TextVariable(1)]
public string FileName { get; set; }
public override int Execute(NodeParameters args)
{
public override int Inputs => 1;
public override int Outputs => 2;
public override FlowElementType Type => FlowElementType.Logic;
public override string Icon => "fas fa-question-circle";
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/file-exists";
[TextVariable(1)]
public string FileName { get; set; }
public override int Execute(NodeParameters args)
string file = args.ReplaceVariables(FileName ?? string.Empty, true);
if(string.IsNullOrWhiteSpace(file))
{
string file = args.ReplaceVariables(FileName ?? string.Empty, true);
if(string.IsNullOrWhiteSpace(file))
args.Logger?.ELog("FileName not set");
return -1;
}
try
{
file = args.MapPath(file);
var fileInfo = new FileInfo(file);
if (fileInfo.Exists)
{
args.Logger?.ELog("FileName not set");
return -1;
}
try
{
file = args.MapPath(file);
var fileInfo = new FileInfo(file);
if (fileInfo.Exists)
{
args.Logger?.ILog("File does exist: " + file);
return 1;
}
args.Logger?.ILog("File does NOT exist: " + file);
return 2;
}
catch (Exception ex)
{
args.Logger?.ELog($"Failed testing if file '{file}' exists: " + ex.Message);
return -1;
args.Logger?.ILog("File does exist: " + file);
return 1;
}
args.Logger?.ILog("File does NOT exist: " + file);
return 2;
}
catch (Exception ex)
{
args.Logger?.ELog($"Failed testing if file '{file}' exists: " + ex.Message);
return -1;
}
}
}
+23
View File
@@ -0,0 +1,23 @@
#if(DEBUG)
using FileFlows.BasicNodes.File;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace BasicNodes.Tests;
[TestClass]
public class FileExistsTests
{
[TestMethod]
public void BasicTest()
{
var logger = new TestLogger();
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\testfile.mkv", logger, false, string.Empty);
var element = new FileExists();
element.FileName = "{folder.Orig.FullName}/{file.Orig.FileNameNoExtension}.en.srt";
element.Execute(args);
}
}
#endif
@@ -108,21 +108,56 @@ public class FfmpegBuilderAudioAddTrack : FfmpegBuilderNode
return _BitrateOptions;
}
}
/// <summary>
/// Gets or sets the sample rate
/// </summary>
[DefaultValue(0)]
[Select(nameof(SampleRateOptions), 4)]
public int SampleRate { get; set; }
private static List<ListOption> _SampleRateOptions;
/// <summary>
/// Gets the sample rate options
/// </summary>
public static List<ListOption> SampleRateOptions
{
get
{
if (_SampleRateOptions == null)
{
_SampleRateOptions = new List<ListOption>
{
new () { Label = "Automatic", Value = 0},
new () { Label = "Same as source", Value = 1},
new () { Label = "44.1Khz", Value = 44100 },
new () { Label = "48Khz", Value = 48000 },
new () { Label = "88.2Khz", Value = 88200 },
new () { Label = "96Khz", Value = 96000 },
new () { Label = "176.4Khz", Value = 176400 },
new () { Label = "192Khz", Value = 192000 }
};
}
return _SampleRateOptions;
}
}
/// <summary>
/// Gets or sets the language of the nee track
/// </summary>
[DefaultValue("eng")]
[TextVariable(4)]
[TextVariable(5)]
public string Language { get; set; }
/// <summary>
/// Gets or sets if the title of the new track should be removed
/// </summary>
[Boolean(5)]
[Boolean(6)]
public bool RemoveTitle { get; set; }
/// <summary>
/// Gets or sets the title of the new track
/// </summary>
[TextVariable(6)]
[TextVariable(7)]
[ConditionEquals(nameof(RemoveTitle), false)]
public string NewTitle { get; set; }
@@ -165,7 +200,8 @@ public class FfmpegBuilderAudioAddTrack : FfmpegBuilderNode
}
else
{
audio.EncodingParameters.AddRange(GetNewAudioTrackParameters(Codec, Channels, Bitrate));
int sampleRate = SampleRate == 1 ? audio.Stream.SampleRate : SampleRate;
audio.EncodingParameters.AddRange(GetNewAudioTrackParameters(Codec, Channels, Bitrate, sampleRate));
if (this.Channels > 0)
audio.Channels = this.Channels;
}
@@ -252,52 +288,42 @@ public class FfmpegBuilderAudioAddTrack : FfmpegBuilderNode
/// <param name="codec">the codec of the new track</param>
/// <param name="channels">the channels of the new track</param>
/// <param name="bitrate">the bitrate of the new track</param>
/// <param name="sampleRate">the sample rate</param>
/// <returns>the new track parameters</returns>
internal static string[] GetNewAudioTrackParameters(string codec, float channels, int bitrate)
internal static string[] GetNewAudioTrackParameters(string codec, float channels, int bitrate, int sampleRate)
{
if (codec == "opus")
codec = "libopus";
// Prepare the options list
var options = new List<string>
{
"-map", "0:a:{sourceTypeIndex}",
"-c:a:{index}",
codec
};
if (channels == 0)
// Handle channels
if (channels > 0)
{
// same as source
if (bitrate == 0)
{
return new[]
{
"-map", "0:a:{sourceTypeIndex}",
"-c:a:{index}",
codec
};
}
return new[]
{
"-map", "0:a:{sourceTypeIndex}",
"-c:a:{index}",
codec,
"-b:a:{index}", bitrate + "k"
};
options.Add("-ac:a:{index}");
options.Add(channels.ToString());
}
else
// Handle bitrate
if (bitrate > 0)
{
if (bitrate == 0)
{
return new[]
{
"-map", "0:a:{sourceTypeIndex}",
"-c:a:{index}",
codec,
"-ac:a:{index}", channels.ToString()
};
}
return new[]
{
"-map", "0:a:{sourceTypeIndex}",
"-c:a:{index}",
codec,
"-ac:a:{index}", channels.ToString(),
"-b:a:{index}", bitrate + "k"
};
options.Add("-b:a:{index}");
options.Add(bitrate + "k");
}
// Handle sample rate
if (sampleRate > 0)
{
options.Add("-ar:a:{index}");
options.Add(sampleRate.ToString());
}
return options.ToArray();
}
}
@@ -136,7 +136,7 @@ public class FfmpegBuilderAudioConverter : FfmpegBuilderNode
if (codecSame && channelsSame && bitrateSame)
return false;
stream.EncodingParameters.AddRange(FfmpegBuilderAudioAddTrack.GetNewAudioTrackParameters(Codec, Channels, Bitrate));
stream.EncodingParameters.AddRange(FfmpegBuilderAudioAddTrack.GetNewAudioTrackParameters(Codec, Channels, Bitrate, 0));
return true;
}
}
+2
View File
@@ -95,6 +95,8 @@
"Bitrate-Help": "Bitrate of the new audio track",
"Codec": "Codec",
"Codec-Help": "The codec to use to encode the audio",
"SampleRate": "Sample Rate",
"SampleRate-Help": "The sample rate to use for the new audio track",
"Language": "Language",
"Language-Help": "Optional [ISO 639-2](https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes) language code to use. Will attempt to find an audio track with this language code if not the best audio track will be used.",
"NewTitle": "New Title",