diff --git a/VideoNodes/LogicalNodes/DetectBlackBars.cs b/VideoNodes/LogicalNodes/DetectBlackBars.cs index 37a57c02..36d041bf 100644 --- a/VideoNodes/LogicalNodes/DetectBlackBars.cs +++ b/VideoNodes/LogicalNodes/DetectBlackBars.cs @@ -37,82 +37,77 @@ namespace FileFlows.VideoNodes public string Execute(string ffplay, string file, string tempDir, NodeParameters args) { - string tempFile = Path.Combine(tempDir, Guid.NewGuid().ToString() + ".mkv"); try { - using (var process = new Process()) + int x = int.MaxValue; + int y = int.MaxValue; + int width = 0; + int height = 0; + int vidWidth = 0; + int vidHeight = 0; + foreach (int ss in new int[] { 60, 120, 240, 360 }) // check at multiple times { - process.StartInfo = new ProcessStartInfo(); - process.StartInfo.FileName = ffplay; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.RedirectStandardError = true; - process.StartInfo.CreateNoWindow = true; - process.StartInfo.Arguments = $"-i \"{file}\" -hide_banner -t 10 -ss 60 -vf cropdetect {tempFile}"; - args.Logger?.DLog("Exectuing ffmpeg " + process.StartInfo.Arguments); - process.Start(); - string output = process.StandardError.ReadToEnd(); - Console.WriteLine(output); - string error = process.StandardError.ReadToEnd(); - process.WaitForExit(); - - var dimMatch = Regex.Match(output, @"Stream #[\d]+:[\d]+(.*?)Video:(.*?)([\d]+)x([\d]+)", RegexOptions.Multiline); - if (dimMatch.Success == false) + using (var process = new Process()) { - args.Logger?.WLog("Can't find dimensions for video"); - return String.Empty; // cant find dimensions + process.StartInfo = new ProcessStartInfo(); + process.StartInfo.FileName = ffplay; + process.StartInfo.UseShellExecute = false; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.RedirectStandardError = true; + process.StartInfo.CreateNoWindow = true; + process.StartInfo.Arguments = $" -ss {ss} -i \"{file}\" -hide_banner -vframes 25 -vf cropdetect=24:16:0 -f null -"; + args.Logger?.DLog("Executing ffmpeg " + process.StartInfo.Arguments); + process.Start(); + string output = process.StandardError.ReadToEnd(); + Console.WriteLine(output); + string error = process.StandardError.ReadToEnd(); + process.WaitForExit(); + + var dimMatch = Regex.Match(output, @"Stream #[\d]+:[\d]+(.*?)Video:(.*?)([\d]+)x([\d]+)", RegexOptions.Multiline); + if (dimMatch.Success == false) + { + args.Logger?.WLog("Can't find dimensions for video"); + continue; + } + + if(vidWidth == 0) + vidWidth = int.Parse(dimMatch.Groups[3].Value); + if(vidHeight == 0) + vidHeight = int.Parse(dimMatch.Groups[4].Value); + + var matches = Regex.Matches(output, @"(?<=(crop=))([\d]+:){3}[\d]+"); + foreach (Match match in matches) + { + int[] parts = match.Value.Split(':').Select(x => int.Parse(x)).ToArray(); + x = Math.Min(x, parts[2]); + y = Math.Min(y, parts[3]); + width = Math.Max(width, parts[0]); + height = Math.Max(height, parts[1]); + } } - - int vidWidth = int.Parse(dimMatch.Groups[3].Value); - int vidHeight = int.Parse(dimMatch.Groups[4].Value); - - args.Logger?.DLog($"Video dimensions: {vidWidth}x{vidHeight}"); - - var matches = Regex.Matches(output, @"(?<=(crop=))([\d]+:){3}[\d]+"); - int x = int.MaxValue; - int y = int.MaxValue; - int width = 0; - int height = 0; - foreach (Match match in matches) - { - int[] parts = match.Value.Split(':').Select(x => int.Parse(x)).ToArray(); - x = Math.Min(x, parts[2]); - y = Math.Min(y, parts[3]); - width = Math.Max(width, parts[0]); - height = Math.Max(height, parts[1]); - } - - if (x == int.MaxValue) - x = 0; - if (y == int.MaxValue) - y = 0; - - if(CroppingThreshold < 0) - CroppingThreshold = 0; - - int diff = x + y + (vidWidth - width) + (vidHeight - height); - - bool willCrop = diff > CroppingThreshold; - args.Logger?.ILog($"Crop detection, x:{x}, y:{y}, width: {width}, height: {height}, total:{diff}, threshold:{CroppingThreshold}, above threshold: {willCrop}"); - - return willCrop ? $"{width}:{height}:{x}:{y}" : string.Empty; } + + if (x == int.MaxValue) + x = 0; + if (y == int.MaxValue) + y = 0; + + if (CroppingThreshold < 0) + CroppingThreshold = 0; + + args.Logger?.DLog($"Video dimensions: {vidWidth}x{vidHeight}"); + + int diff = x + y + (vidWidth - width) + (vidHeight - height); + + bool willCrop = diff > CroppingThreshold; + args.Logger?.ILog($"Crop detection, x:{x}, y:{y}, width: {width}, height: {height}, total:{diff}, threshold:{CroppingThreshold}, above threshold: {willCrop}"); + + return willCrop ? $"{width}:{height}:{x}:{y}" : string.Empty; } catch (Exception) { return string.Empty; } - finally - { - if (System.IO.File.Exists(tempFile)) - { - try - { - System.IO.File.Delete(tempFile); - } - catch (Exception) { } - } - } } } } \ No newline at end of file