private void ScanWorkerHost_DoWork(object sender, DoWorkEventArgs e) { // Authorization check happens in ScanWorker Logger.Info("ScanWorkerHost started"); InputFilesTotalSizeInKBytes = CalculateInputFilesTotalSize(); InputFilesCompletedInKBytes = 0; ScanStartTime = DateTime.Now; MainModel.HighlightObjects.Clear(); ActiveInputFileObject = null; Logger.Info("DetectionThreshold: " + MainModel.DetectionThreshold.ToString()); foreach (var inputFileObject in MainModel.InputFileObjects) { if (CancellationPending) break; using (inputFileObject.ScanWorker = new ScanWorker(inputFileObject)) { inputFileObject.ScanWorker.UseCaptureOffset = MainModel.UseCaptureOffset; ActiveInputFileObject = inputFileObject; inputFileObject.ScanWorker.ProgressChanged += scanWorker_ProgressChanged; ReportStatusChanged(); try { inputFileObject.ScanWorker.RunWorkerAsync(); while (inputFileObject.ScanWorker.IsBusy) { // wait for the scanworker to finish if (CancellationPending) inputFileObject.ScanWorker.CancelScan(); System.Threading.Thread.Sleep(500); } inputFileObject.ScanWorkerResult = inputFileObject.ScanWorker.ScanWorkerResult; } catch (Exception ex) { Logger.Error("Error starting ScanWorker! " + ex.ToString()); } } CompletedScanWorkerCount++; } ActiveInputFileObject = null; Logger.Info("Highlights found: " + MainModel.HighlightObjects.Count); TrackAnalytics(); IsCancelled = CancellationPending; // we use this instead of e.Cancel because if we set e.Cancel, we can't read e.Result (http://bytes.com/topic/c-sharp/answers/519073-asynch-crash-when-e-cancel-set) if (!IsCancelled) { // in case we scan really fast, wait a few seconds so the user sees what happens var minScanTime = TimeSpan.FromSeconds(4); while (DateTime.Now.Subtract(ScanStartTime) < minScanTime) Thread.Sleep(200); } }
/// <summary> /// This goes through a number of files that have an expected hand /// </summary> public TestResult FindDarkFramesTest(string filename) { FileInfo inputFile = AvailableFiles[filename]; var inputFileObject = new InputFileObject(AvailableFiles[filename]); Logger.Info("-------------"); Logger.Info("Running dark frame test on " + inputFile.FullName); Logger.Info("Processing: " + inputFile.FullName); DateTime startScanTime = DateTime.Now; MainModel.IgnoreEarlyHighlights = false; MainModel.UseCaptureOffset = false; MainModel.HighlightObjects.Clear(); var scanWorker = new ScanWorker(inputFileObject); scanWorker.RunWorkerAsync(); while (scanWorker.IsBusy) { System.Threading.Thread.Sleep(500); } Logger.Info("Results for " + inputFile.FullName); DateTime endScanTime = DateTime.Now; TimeSpan scanTimeSpan = endScanTime - startScanTime; Logger.Info("Video duration: " + Math.Round(inputFileObject.VideoDurationInSeconds, 2) + "s"); Logger.Info("Scan time: " + Math.Round(scanTimeSpan.TotalSeconds, 2) + "s"); Debug.Assert(inputFileObject.FramesPerSecond > 0); Debug.Assert(inputFileObject.VideoDurationInSeconds > 0); var tr = new TestResult { InputFile = inputFile, ActualScore = 0, DarkTimesFound = new List<string>(), DarkTimesExpected = new List<string>(), FalsePositives = new List<string>(), Matched = new List<string>(), Missing = new List<string>(), ScanTime = scanTimeSpan }; #region Look up expected dark times string expectedDarkTimesFilePath = inputFile.FullName + ".txt"; var expectedDarkTimes = new string[] { }; if (File.Exists(expectedDarkTimesFilePath)) { var sr = new StreamReader(expectedDarkTimesFilePath); expectedDarkTimes = sr.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); } tr.MaxScore = expectedDarkTimes.Length * (int)ScanResultScore.Found; tr.DarkTimesExpected = expectedDarkTimes.ToList<string>(); #endregion #region Convert dark times to dark spots var expectedMarkTimeSpans = new List<TimeSpan>(); foreach (string s in expectedDarkTimes) { TimeSpan ts = TimeSpan.Parse(s); // ex. 0:06 //expectedMarkTimeSpans.Add((long)(ts.TotalSeconds * inputFileObject.FramesPerSecond)); expectedMarkTimeSpans.Add(ts); } Logger.Info("Expected dark times and frames:"); foreach (var ts in expectedMarkTimeSpans) { var frame = Math.Round(ts.TotalSeconds * inputFileObject.FramesPerSecond, 0); Logger.Info("- " + ts.ToString("c") + " => " + frame); } #endregion Logger.Info("Found dark times and frames:"); foreach (var highlightObject in MainModel.HighlightObjects) { tr.DarkTimesFound.Add(highlightObject.BookmarkTime.ToString("c")); var frame = Math.Round(highlightObject.BookmarkTime.TotalSeconds * inputFileObject.FramesPerSecond, 0); Logger.Info("- " + highlightObject.BookmarkTime.ToString("c") + " => " + frame); } // If we have any failures at all, the entire test fails. But when debugging, we'll want to know where and how it failed. bool testFail = false; #region Make sure expected dark spots are near actual dark frames Logger.Info("Comparing expected spots with actual spots"); var scanSummary = new SortedDictionary<TimeSpan, ScanResultScore>(); const int toleranceInSeconds = 4; // our expected dark frame must be within 3 seconds of actual dark frames //var toleranceInFrames = (int)(toleranceInSeconds * inputFileObject.FramesPerSecond); foreach (var expectedMarkTimeSpan in expectedMarkTimeSpans) { var isFound = false; foreach (var highlightObject in MainModel.HighlightObjects) { if (Math.Abs(highlightObject.BookmarkTime.TotalSeconds - expectedMarkTimeSpan.TotalSeconds) <= toleranceInSeconds) { if (scanSummary.ContainsKey(expectedMarkTimeSpan) == false) { var frame = Math.Round(expectedMarkTimeSpan.TotalSeconds * inputFileObject.FramesPerSecond, 0); Logger.Info("- Success: Found dark spot near " + expectedMarkTimeSpan.ToString("c") + " => frame " + frame); scanSummary.Add(expectedMarkTimeSpan, ScanResultScore.Found); tr.Matched.Add(expectedMarkTimeSpan.ToString("c")); isFound = true; } else { // there's a false positive here. two found bookmarks were near an expected bookmark testFail = true; scanSummary.Add(highlightObject.BookmarkTime, ScanResultScore.FalsePositive); tr.FalsePositives.Add(highlightObject.BookmarkTime.ToString("c")); var frame = Math.Round(highlightObject.BookmarkTime.TotalSeconds * inputFileObject.FramesPerSecond, 0); Logger.Info("- Found false positive at " + highlightObject.BookmarkTime.ToString("c") + " => frame " + frame); } break; } } if (isFound == false) { scanSummary.Add(expectedMarkTimeSpan, ScanResultScore.Missing); testFail = true; tr.Missing.Add(expectedMarkTimeSpan.ToString("c")); var frame = Math.Round(expectedMarkTimeSpan.TotalSeconds * inputFileObject.FramesPerSecond, 0); Logger.Info("- Error! Could not find dark spot near " + expectedMarkTimeSpan.ToString("c") + " => frame " + frame); } } // find the false positives foreach (var highlightObject in MainModel.HighlightObjects) { bool isFound = false; foreach (var expectedMarkTimeSpan in expectedMarkTimeSpans) { if (Math.Abs(highlightObject.BookmarkTime.TotalSeconds - expectedMarkTimeSpan.TotalSeconds) <= toleranceInSeconds) { isFound = true; break; } } if (isFound == false) { testFail = true; scanSummary.Add(highlightObject.BookmarkTime, ScanResultScore.FalsePositive); tr.FalsePositives.Add(highlightObject.BookmarkTime.ToString("c")); var frame = Math.Round(highlightObject.BookmarkTime.TotalSeconds * inputFileObject.FramesPerSecond, 0); Logger.Info("- Found false positive at " + highlightObject.BookmarkTime.ToString("c") + " => frame " + frame); } } #endregion #region Print summary Logger.Info("-------------"); Logger.Info("Test results summary:"); foreach (var kvp in scanSummary) { var frame = Math.Round(kvp.Key.TotalSeconds * inputFileObject.FramesPerSecond, 0); Logger.Info("- Time " + kvp.Key.ToString("c") + " (frame " + frame + "): " + kvp.Value.ToString()); tr.ActualScore += (int)kvp.Value; } Logger.Info("Max score: " + tr.MaxScore); Logger.Info("Actual score: " + tr.ActualScore); Logger.Info("Found count: " + tr.Matched.Count); Logger.Info("False positive count: " + tr.FalsePositives.Count); Logger.Info("Missing count: " + tr.Missing.Count); #endregion if (testFail) { tr.Failed = true; Logger.Info("Test failed. See debug log for details."); } return tr; }
private void MainForm_DoubleClick(object sender, EventArgs e) { #if DEBUG using (new HourGlass()) { /* for (var i = 1; i <= 1000; i++) { var activationState = Protection.GetLicenseStatus(true); } MessageBox.Show("done"); return; */ var inputFileObject = new InputFileObject() { SourceFileInfo = new FileInfo(MainModel.GetPathToSampleVideo()), }; using (var scanWorker = new ScanWorker(inputFileObject)) { scanWorker.SetBitrate(); scanWorker.SetFramesPerSecond(); scanWorker.SetTotalFrames(); scanWorker.SetVideoDimensions(); scanWorker.SetVideoDuration(); } MainModel.InputFileObjects.Add(inputFileObject); var newHighlight1 = (new HighlightObject() { InputFileObject = inputFileObject, StartTime = TimeSpan.FromSeconds(10), BookmarkTime = TimeSpan.FromSeconds(27), EndTime = TimeSpan.FromSeconds(25), }); newHighlight1.GenerateHighlightTitle(); MainModel.HighlightObjects.Add(newHighlight1); var sampleFileDir = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\sample-files"); var inputFileObject2 = new InputFileObject() { SourceFileInfo = new FileInfo(Path.Combine(sampleFileDir, @"Long videos\GO021100.MP4")), }; using (var scanWorker = new ScanWorker(inputFileObject2)) { scanWorker.SetBitrate(); scanWorker.SetFramesPerSecond(); scanWorker.SetTotalFrames(); scanWorker.SetVideoDimensions(); scanWorker.SetVideoDuration(); } MainModel.InputFileObjects.Add(inputFileObject2); var newHighlight2 = (new HighlightObject() { InputFileObject = inputFileObject2, StartTime = TimeSpan.FromSeconds(60 * 20 + 4), BookmarkTime = TimeSpan.FromSeconds(60 * 20 + 19), EndTime = TimeSpan.FromSeconds(60 * 20 + 17), }); newHighlight2.GenerateHighlightTitle(); MainModel.HighlightObjects.Add(newHighlight2); var newHighlight3 = new HighlightObject() { InputFileObject = inputFileObject2, StartTime = TimeSpan.FromSeconds(60 * 10 + 4), BookmarkTime = TimeSpan.FromSeconds(60 * 10 + 19), EndTime = TimeSpan.FromSeconds(60 * 10 + 17) }; newHighlight3.GenerateHighlightTitle(); MainModel.HighlightObjects.Add(newHighlight3); var newHighlight4 = new HighlightObject() { InputFileObject = inputFileObject2, StartTime = TimeSpan.FromSeconds(60 * 5 + 4), BookmarkTime = TimeSpan.FromSeconds(60 * 5 + 19), EndTime = TimeSpan.FromSeconds(60 * 5 + 17), }; newHighlight4.GenerateHighlightTitle(); MainModel.HighlightObjects.Add(newHighlight4); var inputFileObject3 = new InputFileObject() { SourceFileInfo = new FileInfo(Path.Combine(sampleFileDir, "00002.MTS")), }; using (var scanWorker = new ScanWorker(inputFileObject3)) { scanWorker.SetBitrate(); scanWorker.SetFramesPerSecond(); scanWorker.SetTotalFrames(); scanWorker.SetVideoDimensions(); scanWorker.SetVideoDuration(); } MainModel.InputFileObjects.Add(inputFileObject3); var newHighlight5 = (new HighlightObject() { InputFileObject = inputFileObject3, StartTime = TimeSpan.FromSeconds(30), BookmarkTime = TimeSpan.FromSeconds(47), EndTime = TimeSpan.FromSeconds(45), }); newHighlight5.GenerateHighlightTitle(); MainModel.HighlightObjects.Add(newHighlight5); SwitchToStep(Steps.Scan); scanControl_ScanCompletedWithHighlights(sender, e); } #endif }
private void TrimButton_Click(object sender, EventArgs e) { MainModel.Initialize(); OpenFileDialog.CheckFileExists = true; OpenFileDialog.FileName = ""; string supportedExtensionsAsSingleString = ""; foreach (string supportedExtension in MainModel.GetVideoExtensions()) { supportedExtensionsAsSingleString += "*" + supportedExtension + ";"; } if (supportedExtensionsAsSingleString.Length > 0) supportedExtensionsAsSingleString = supportedExtensionsAsSingleString.Substring(0, supportedExtensionsAsSingleString.Length - 1); OpenFileDialog.Multiselect = false; OpenFileDialog.Title = "Tell me which video you'd like to trim."; OpenFileDialog.Filter = "Supported Files (" + supportedExtensionsAsSingleString + ")|" + supportedExtensionsAsSingleString + "|All Files (*.*)|*.*"; OpenFileDialog.ShowDialog(); if (File.Exists(OpenFileDialog.FileName)) { var inputFileObject = new InputFileObject(new FileInfo(OpenFileDialog.FileName)); string ss = "0:00:00", end = "0:00:00"; if (InputBox("Start time", "What is the starting time that you'd like to trim from? (e.g. 0:00:20)", ref ss) == DialogResult.OK) { if (InputBox("End time", "What is the ending time that you'd like to trim to? (e.g. 0:00:35)", ref end) == DialogResult.OK) { var saveFileDialog = new SaveFileDialog(); saveFileDialog.InitialDirectory = inputFileObject.SourceFileInfo.DirectoryName; saveFileDialog.FileName = inputFileObject.SourceFileInfo.Name + " - " + ss + " to " + end + inputFileObject.SourceFileInfo.Extension; saveFileDialog.Title = "Where should we save the new file?"; if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (var scanWorker = new ScanWorker(inputFileObject)) { scanWorker.SetFramesPerSecond(); scanWorker.SetBitrate(); var highlightObject = new HighlightObject(); highlightObject.InputFileObject = inputFileObject; highlightObject.StartTime = TimeSpan.Parse(ss); // ex. 0:06 highlightObject.EndTime = TimeSpan.Parse(end); var saveWorker = new SaveWorker(highlightObject); saveWorker.RunWorkerAsync(saveFileDialog.FileName); while (saveWorker.PublishWorkerResult == PublishWorker.PublishWorkerResults.NotFinished) Application.DoEvents(); if (saveWorker.PublishWorkerResult == PublishWorker.PublishWorkerResults.Success) while (saveWorker.OutputFileInfo == null) Application.DoEvents(); var files = new List<FileInfo> { saveWorker.OutputFileInfo }; MainModel.LaunchExplorerWithFilesSelected(files); } } } } } MainModel.Dispose(); }