namespace FileFlows.AudioNodes; /// /// Checks if a audio files bitrate matches the condition /// public class AudioBitrateMatches : AudioNode { /// public override int Inputs => 1; /// public override int Outputs => 2; /// public override string HelpUrl => "https://fileflows.com/docs/plugins/audio-nodes/audio-bitrate-matches"; /// public override FlowElementType Type => FlowElementType.Logic; /// public override string Icon => "fas fa-question"; internal const string MATCH_GREATER_THAN = ">"; internal const string MATCH_LESS_THAN = "<"; internal const string MATCH_EQUALS = "="; internal const string MATCH_NOT_EQUALS = "!="; internal const string MATCH_GREATER_THAN_OR_EQUAL = ">="; internal const string MATCH_LESS_THAN_OR_EQUAL = "<="; /// /// Gets or sets the method to match /// [Select(nameof(MatchOptions), 1)] public string Match { get; set; } private static List _MatchOptions; /// /// Gets the match options /// public static List MatchOptions { get { if (_MatchOptions == null) { _MatchOptions = new List { new () { Label = "Equals", Value = MATCH_EQUALS}, new () { Label = "Not Equals", Value = MATCH_NOT_EQUALS}, new () { Label = "Less Than", Value = MATCH_LESS_THAN}, new () { Label = "Less Than Or Equal", Value = MATCH_LESS_THAN_OR_EQUAL}, new () { Label = "Greater Than", Value = MATCH_GREATER_THAN}, new () { Label = "Greater Than Or Equal", Value = MATCH_GREATER_THAN_OR_EQUAL}, }; } return _MatchOptions; } } /// /// Gets or sets the bitrate value to check /// [NumberInt(2)] public int BitrateKilobytes { get; set; } /// public override int Execute(NodeParameters args) { var audioInfoResult = GetAudioInfo(args); if (audioInfoResult.Failed(out string error)) { args.Logger?.ELog(error); args.FailureReason = error; return -1; } var audioInfo = audioInfoResult.Value; var bitrate = audioInfo.Bitrate; long expected = BitrateKilobytes * 1000; return DoMatch(args.Logger, Match, bitrate, expected) ? 1 : 2; } /// /// Executes the match check /// /// the logger /// the match to test /// the actual bitrate /// the expected bitrate /// true if matches, otherwise false internal static bool DoMatch(ILogger logger, string match, long bitrate, long expected) { bool matches = false; switch (match) { case MATCH_EQUALS: matches = bitrate == expected; break; case MATCH_NOT_EQUALS: matches = bitrate != expected; break; case MATCH_LESS_THAN: matches = bitrate < expected; break; case MATCH_LESS_THAN_OR_EQUAL: matches = bitrate <= expected; break; case MATCH_GREATER_THAN: matches = bitrate > expected; break; case MATCH_GREATER_THAN_OR_EQUAL: matches = bitrate >= expected; break; } if(matches) logger?.ILog($"Bitrate matches expected: {bitrate} {match} {expected}"); else logger?.ILog($"Bitrate does not match expected: {bitrate} {match} {expected}"); return matches; } }