static void Main(string[] args)
        {
            var options = new CommandLineOptions();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                // Display the default usage information
                //Console.WriteLine(options.GetUsage());
                return;
            }

            if (!File.Exists(options.InputFile))
            {
                Console.WriteLine("Source file not found: " + options.InputFile + ".");
                return;
            }

            VFPAnalyzer.SetLicenseKey(options.LicenseKey);

            var engine = VFSimplePlayerEngine.LAV;

            if (!string.IsNullOrEmpty(options.Engine))
            {
                switch (options.Engine.Trim())
                {
                case "directshow":
                    engine = VFSimplePlayerEngine.DirectShow;
                    break;

                case "ffmpeg":
                    engine = VFSimplePlayerEngine.FFMPEG;
                    break;

                case "lav":
                    engine = VFSimplePlayerEngine.LAV;
                    break;
                }
            }

            Console.WriteLine("Analyzing...");

            var time        = DateTime.Now;
            var mediaPlayer = new SimplePlayer(null)
            {
                Filename       = options.InputFile,
                Video_Renderer = VFVideoRendererInternal.None
            };

            if (Path.GetExtension(options.InputFile)?.ToLowerInvariant() == ".wmv")
            {
                engine = VFSimplePlayerEngine.DirectShow;
            }

            mediaPlayer.Engine = engine;
            mediaPlayer.MaximalSpeedPlayback = true;

            //if (!string.IsNullOrEmpty(DebugDir))
            //{
            //    mediaPlayer.Debug_Dir = DebugDir;
            //    mediaPlayer.Debug_Mode = true;
            //}

            var mediaInfo = new MediaInfoReader
            {
                Filename = options.InputFile
            };

            mediaInfo.ReadFileInfo(true);


            VFPSearchData[] dataList = new VFPSearchData[options.Count];

            string error1 = string.Empty;
            bool   inUse  = true;
            bool   error  = false;

            // check for hang or something
            long frameNumber = 0;

            int sourceWidth  = 0;
            int sourceHeight = 0;

            int  index     = 0;
            long minusTime = 0;

            mediaPlayer.OnVideoFrame += delegate(object sender, SampleGrabberBufferCBEventArgs e)
            {
                if (!inUse)
                {
                    return;
                }

                long timestamp = (long)(e.SampleTime * 1000);
                timestamp -= minusTime;

                if (timestamp >= options.Duration)
                {
                    Console.WriteLine($"Started new fragment at {minusTime}");
                    index++;
                    minusTime  = (long)(e.SampleTime * 1000);
                    timestamp -= minusTime;
                }

                if (index >= options.Count)
                {
                    inUse = false;
                    return;
                }

                if (e.SampleTime < options.Duration * options.Count)
                {
                    frameNumber++;

                    sourceWidth  = e.Width;
                    sourceHeight = e.Height;

                    if (dataList[index] == null)
                    {
                        dataList[index] = new VFPSearchData(TimeSpan.FromMilliseconds(options.Duration));
                    }

                    VFPSearch.Process(
                        e.Buffer,
                        e.Width,
                        e.Height,
                        ImageHelper.GetStrideRGB24(e.Width),
                        TimeSpan.FromMilliseconds(timestamp),
                        ref dataList[index]);
                }
                else
                {
                    inUse = false;
                }
            };

            mediaPlayer.OnStop += delegate
            {
                inUse = false;
            };

            mediaPlayer.OnError += delegate(object sender, ErrorsEventArgs e)
            {
                if (e.Message.Contains("FULL"))
                {
                    return;
                }

                if (e.Level == DebugLevel.Error)
                {
                    error = true;
                    inUse = false;

                    error1 = e.Message + " | " + e.CallSite;
                }
            };

            mediaPlayer.Start();

            while (inUse && !error)
            {
                Thread.Sleep(100);
                Application.DoEvents();
            }

            mediaPlayer.Stop();

            Debug.WriteLine($"Processed {frameNumber} frame for file {options.InputFile}.");

            if (!error)
            {
                for (var i = 0; i < dataList.Length; i++)
                {
                    var data = dataList[i];
                    if (data == null)
                    {
                        break;
                    }

                    IntPtr p = VFPSearch.Build(out var n, ref data);

                    VFPFingerPrint fvp = new VFPFingerPrint()
                    {
                        // ReSharper disable once ExceptionNotDocumented
                        Data             = new byte[n],
                        OriginalFilename = options.InputFile,
                        OriginalDuration = TimeSpan.FromMilliseconds(options.Duration),
                        Duration         = TimeSpan.FromMilliseconds(options.Duration),
                        ID        = Guid.NewGuid(),
                        Width     = sourceWidth,
                        Height    = sourceHeight,
                        FrameRate = mediaInfo.Video_FrameRate(0)
                    };

                    Marshal.Copy(p, fvp.Data, 0, (int)n);

                    data.Free();

                    var newFilename = $"{options.InputFile}_segment{i}.vfsigx";
                    fvp.Save(newFilename);
                }
            }
            else
            {
                Debug.WriteLine($"Error: {error1}.");
            }

            mediaPlayer.Dispose();

            var elapsed = DateTime.Now - time;

            Console.WriteLine("Analyze finished. Elapsed time: " + elapsed.ToString("g"));
        }
        private void btStart_Click(object sender, RoutedEventArgs e)
        {
            if ((string)btStart.Content == "Stop")
            {
                _stopFlag = true;

                Thread.Sleep(500);

                _videoCapture?.Stop();
                _videoPlayer?.Stop();

                Thread.Sleep(500);

                ProcessVideoDelegateMethod();

                btStart.Content = "Start";

                lbStatus.Content = string.Empty;

                if (_tempBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(_tempBuffer);
                    _tempBuffer = IntPtr.Zero;
                }

                pnScreen.BeginInit();
                pnScreen.Source = null;
                pnScreen.EndInit();
            }
            else
            {
                _stopFlag = false;

                btStart.IsEnabled = false;

                lbStatus.Content = "Step 1: Searching video files";

                _fragmentCount        = 0;
                _overlapFragmentCount = 0;

                var engine = VFSimplePlayerEngine.LAV;

                switch (cbEngine.SelectedIndex)
                {
                case 0:
                    engine = VFSimplePlayerEngine.DirectShow;
                    break;

                case 1:
                    engine = VFSimplePlayerEngine.FFMPEG;
                    break;

                case 2:
                    engine = VFSimplePlayerEngine.LAV;
                    break;
                }

                var adList = new List <string>();

                _adVFPList = new List <VFPFingerPrint>();

                foreach (string item in lbAdFiles.Items)
                {
                    adList.Add(item);
                }

                lbStatus.Content = "Step 2: Getting fingerprints for ads files";

                if (adList.Count == 0)
                {
                    btStart.Content   = "Start";
                    lbStatus.Content  = string.Empty;
                    btStart.IsEnabled = true;

                    MessageBox.Show("Ads list is empty!");

                    return;
                }

                int progress = 0;
                foreach (string filename in adList)
                {
                    pbProgress.Value = progress;
                    string         error = "";
                    VFPFingerPrint fp;

                    if (File.Exists(filename + ".vfsigx"))
                    {
                        fp = VFPFingerPrint.Load(filename + ".vfsigx");
                    }
                    else
                    {
                        var source = new VFPFingerprintSource(filename, engine);
                        foreach (var area in _ignoredAreas)
                        {
                            source.IgnoredAreas.Add(area);
                        }

                        fp = VFPAnalyzer.GetSearchFingerprintForVideoFile(source, ErrorCallback);
                    }

                    if (fp == null)
                    {
                        MessageBox.Show("Unable to get fingerprint for video file: " + filename + ". Error: " + error);
                    }
                    else
                    {
                        fp.Save(filename + ".vfsigx");
                        _adVFPList.Add(fp);
                    }

                    progress += 100 / adList.Count;
                }

                int fragmentDurationProperty = Convert.ToInt32(edFragmentDuration.Text);
                if (fragmentDurationProperty != 0)
                {
                    _fragmentDuration = fragmentDurationProperty * 1000;
                }
                else
                {
                    var  maxDuration         = _adVFPList.Max((print => print.Duration));
                    long minfragmentDuration = ((((long)maxDuration.TotalMilliseconds + 1000) / 1000) + 1) * 1000;
                    _fragmentDuration = minfragmentDuration * 2;
                }

                pbProgress.Value = 100;

                if (_tempBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(_tempBuffer);
                    _tempBuffer = IntPtr.Zero;
                }

                lbStatus.Content = "Step 3: Starting video preview";

                if (cbSource.SelectedIndex == 0)
                {
                    _videoCapture.Video_CaptureDevice_Name      = cbVideoSource.Text;
                    _videoCapture.Video_CaptureDevice_Format    = cbVideoFormat.Text;
                    _videoCapture.Video_CaptureDevice_FrameRate = Convert.ToDouble(cbVideoFrameRate.Text);

                    _videoCapture.Start();
                }
                else
                {
                    string url = edNetworkSourceURL.Text;
                    //var ip = new IPCameraSourceSettings
                    //             {
                    //                 URL =,
                    //                 Login = edNetworkSourceLogin.Text,
                    //                 Password = edNetworkSourcePassword.Text
                    //             };
                    _videoPlayer.Filename             = url;
                    _videoPlayer.MaximalSpeedPlayback = false;


                    switch (cbNetworkSourceEngine.SelectedIndex)
                    {
                    case 0:
                        _videoPlayer.Engine = VFSimplePlayerEngine.LAV;
                        break;

                    case 2:
                        _videoPlayer.Engine = VFSimplePlayerEngine.FFMPEG;
                        break;
                    }

                    _videoPlayer.Start();
                }

                lbStatus.Content = "Step 4: Getting data";

                pbProgress.Value = 0;

                lvResults.Items.Refresh();

                btStart.IsEnabled = true;
                btStart.Content   = "Stop";
            }
        }