From e704b7a2a66d8153c57bb476a51d79dfe062130c Mon Sep 17 00:00:00 2001 From: John Andrews Date: Thu, 17 Feb 2022 10:12:20 +1300 Subject: [PATCH] fixing issue with videocrop being saved in parameters and not variables --- BasicNodes/Tests/FunctionTests.cs | 38 ++++++++++++++++++++++ VideoNodes/LogicalNodes/DetectBlackBars.cs | 18 ++++++++-- VideoNodes/Tests/DetectBlackBarsTests.cs | 5 +-- VideoNodes/VideoNodes/VideoEncode.cs | 2 +- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/BasicNodes/Tests/FunctionTests.cs b/BasicNodes/Tests/FunctionTests.cs index 545647fe..a5c2efc8 100644 --- a/BasicNodes/Tests/FunctionTests.cs +++ b/BasicNodes/Tests/FunctionTests.cs @@ -428,6 +428,44 @@ return 1; Assert.AreEqual(1, result); Assert.AreEqual("movie h265", args.Variables["NewName"]); } + + + [TestMethod] + public void Function_CropVariable() + { + Function pm = new Function(); + var logger = new TestLogger(); + var args = new FileFlows.Plugin.NodeParameters(@"D:\videos\unprocessed\movie h264.mkv", logger, false, string.Empty); + pm.Code = @" +let quality = Variables.VideoCrop ? 17 : 19; +Variables.VideoCodecParameters = `hevc_qsv -preset slow -tune film -global_quality ${quality} -look_ahead 1`; +Variables.VideoCodec = 'h265'; +Variables.Extension = 'mkv'; +return 1; + ; "; + args.Variables["VideoCrop"] = "1920:1000:40:40"; + var result = pm.Execute(args); + Assert.AreEqual(1, result); + Assert.AreEqual("hevc_qsv -preset slow -tune film -global_quality 17 -look_ahead 1", args.Variables["VideoCodecParameters"]); + } + + [TestMethod] + public void Function_CropVariable_Missing() + { + Function pm = new Function(); + var logger = new TestLogger(); + var args = new FileFlows.Plugin.NodeParameters(@"D:\videos\unprocessed\movie h264.mkv", logger, false, string.Empty); + pm.Code = @" +let quality = Variables.VideoCrop ? 17 : 19; +Variables.VideoCodecParameters = `hevc_qsv -preset slow -tune film -global_quality ${quality} -look_ahead 1`; +Variables.VideoCodec = 'h265'; +Variables.Extension = 'mkv'; +return 1; + ; "; + var result = pm.Execute(args); + Assert.AreEqual(1, result); + Assert.AreEqual("hevc_qsv -preset slow -tune film -global_quality 19 -look_ahead 1", args.Variables["VideoCodecParameters"]); + } } } diff --git a/VideoNodes/LogicalNodes/DetectBlackBars.cs b/VideoNodes/LogicalNodes/DetectBlackBars.cs index 2c6d611c..75654e64 100644 --- a/VideoNodes/LogicalNodes/DetectBlackBars.cs +++ b/VideoNodes/LogicalNodes/DetectBlackBars.cs @@ -16,9 +16,20 @@ namespace FileFlows.VideoNodes internal const string CROP_KEY = "VideoCrop"; + private Dictionary _Variables; + public override Dictionary Variables => _Variables; + [NumberInt(1)] public int CroppingThreshold { get; set; } + public DetectBlackBars() + { + _Variables = new Dictionary() + { + { CROP_KEY, "1920:1000:0:40" } + }; + } + public override int Execute(NodeParameters args) { string ffmpeg = GetFFMpegExe(args); @@ -31,7 +42,7 @@ namespace FileFlows.VideoNodes int vidWidth = videoInfo.VideoStreams[0].Width; int vidHeight = videoInfo.VideoStreams[0].Height; - if(vidWidth < 1) + if (vidWidth < 1) { args.Logger?.ELog("Failed to find video width"); return -1; @@ -47,7 +58,10 @@ namespace FileFlows.VideoNodes return 2; args.Logger?.ILog("Black bars detected, crop: " + crop); - args.Parameters.Add(CROP_KEY, crop); + args.UpdateVariables(new Dictionary + { + { CROP_KEY, crop } + }); return 1; } diff --git a/VideoNodes/Tests/DetectBlackBarsTests.cs b/VideoNodes/Tests/DetectBlackBarsTests.cs index 8ffbfece..7452863d 100644 --- a/VideoNodes/Tests/DetectBlackBarsTests.cs +++ b/VideoNodes/Tests/DetectBlackBarsTests.cs @@ -30,18 +30,19 @@ namespace VideoNodes.Tests int output = node.Execute(args); - string crop = args.Parameters[DetectBlackBars.CROP_KEY] as string; + string crop = args.Variables[DetectBlackBars.CROP_KEY] as string; Assert.IsFalse(string.IsNullOrWhiteSpace(crop)); Assert.AreEqual(1, output); } + [TestMethod] public void DetectBlackBars_Test_02() { var crop = DetectBlackBars.TestAboveThreshold(1920, 1080, 1920, 1072, 20); Assert.IsFalse(crop.crop); } - } + } } diff --git a/VideoNodes/VideoNodes/VideoEncode.cs b/VideoNodes/VideoNodes/VideoEncode.cs index 2fa16224..c18390d3 100644 --- a/VideoNodes/VideoNodes/VideoEncode.cs +++ b/VideoNodes/VideoNodes/VideoEncode.cs @@ -82,7 +82,7 @@ namespace FileFlows.VideoNodes var videoTrack = videoIsRightCodec ?? videoInfo.VideoStreams[0]; args.Logger?.ILog("Video: ", videoTrack); - string crop = args.GetParameter(DetectBlackBars.CROP_KEY) ?? ""; + string crop = (args.Variables.ContainsKey(DetectBlackBars.CROP_KEY) ? args.Variables[DetectBlackBars.CROP_KEY] as string : string.Empty) ?? string.Empty; if (crop != string.Empty) crop = " -vf crop=" + crop;