public static CropDetect GetCropDetect(string file, TimeSpan duration) { const int numberOfChecks = 5; var totalSeconds = duration.TotalSeconds; var startEndPadding = totalSeconds / 10; var timeToCheck = startEndPadding; var timeBetweenChecks = (totalSeconds - (startEndPadding * 2)) / numberOfChecks; CropDetect bestCropDetect = null; try { while (timeToCheck < totalSeconds) { var output = RunFFmpegProcess(CropDetectArgs(file, timeToCheck)); var cropDetect = CropDetect.CreateFromString(output); if (cropDetect == null) { continue; } bestCropDetect = bestCropDetect != null?CropDetect.SelectSmallestCrop(bestCropDetect, cropDetect) : cropDetect; timeToCheck += timeBetweenChecks; } return(bestCropDetect); } catch { return(null); } }
public static CropDetect SelectSmallestCrop(CropDetect first, CropDetect second) { int width = Math.Min(first.Width, second.Width); int height = Math.Min(first.Height, second.Height); int x = Math.Min(first.X, second.X); int y = Math.Min(first.Y, second.Y); return(new CropDetect(width, height, x, y)); }