private void ProcessVideo(string fileName) { VideoFileReader reader = new VideoFileReader(); reader.Open(fileName); System.Drawing.Color[] samples = new System.Drawing.Color[_captureRect.Width * _captureRect.Height]; FrameCaptureCollection frameSamples = new FrameCaptureCollection(); frameSamples.TotalFramesInVideo = (int)reader.FrameCount; frameSamples.DurationNumerator = reader.FrameCount * reader.FrameRate.Denominator; frameSamples.DurationDenominator = reader.FrameRate.Numerator; if (frameSamples.TotalFramesInVideo == 0) { frameSamples.DurationDenominator = 1000; frameSamples.DurationNumerator = (long)_backupDuration.TotalMilliseconds; frameSamples.TotalFramesInVideo = (int)(reader.FrameRate.Numerator * _backupDuration.TotalSeconds / reader.FrameRate.Denominator); } frameSamples.VideoFile = _videoFile; frameSamples.CaptureRect = _captureRect; long frame = 0; do { var current = reader.ReadVideoFrame(); if (current == null) { break; } frame++; for (int i = 0; i < samples.Length; i++) { samples[i] = current.GetPixel(_captureRect.X + i % _captureRect.Width, _captureRect.Y + i / _captureRect.Width); } frameSamples.Add(new FrameCapture(frame, samples)); if (frame % 25 == 0) { var hBitmap = current.GetHbitmap(); var capture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(current.Width, current.Height)); capture.Freeze(); DeleteObject(hBitmap); var progress01 = (double)frame / reader.FrameCount; string progress = $"{frame} / {reader.FrameCount} ({progress01:P})"; Dispatcher.Invoke(() => { if (_isClosing) { return; } Image = capture; txtProgress.Text = progress; TaskbarItemInfo.ProgressValue = progress01; }); } current.Dispose(); } while (_running); long framesSampled = frame; long expectedFrames = frameSamples.TotalFramesInVideo; long sampledFrames = frameSamples.Count; Dispatcher.BeginInvoke(new Action(() => { if (_isClosing) { return; } Result = frameSamples; DialogResult = true; })); }
public static FrameCaptureCollection FromStream(Stream stream) { using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, true)) { FrameCaptureCollection result = new FrameCaptureCollection(); byte[] header = reader.ReadBytes(3); //70,83,70 Debug.Assert(header[0] == 70); Debug.Assert(header[1] == 83); Debug.Assert(header[2] == 70); byte[] version = reader.ReadBytes(2); Debug.Assert(version[0] == 1); Debug.Assert(version[1] == 0); byte endianess = reader.ReadByte(); int payloadCount = reader.ReadInt32(); while (payloadCount-- > 0) { int payloadType = reader.ReadInt32(); int payloadSize = reader.ReadInt32(); switch (payloadType) { case 1: { byte[] fileNameBytes = reader.ReadBytes(payloadSize); result.VideoFile = Encoding.UTF8.GetString(fileNameBytes); break; } case 2: { Int32Rect captureRect = new Int32Rect(); captureRect.X = reader.ReadInt32(); captureRect.Y = reader.ReadInt32(); captureRect.Width = reader.ReadInt32(); captureRect.Height = reader.ReadInt32(); result.CaptureRect = captureRect; break; } case 3: { int frames = reader.ReadInt32(); int pixelSize = reader.ReadInt32(); int expectedPixelSize = result.CaptureRect.Width * result.CaptureRect.Height; int expectedPixelSize2 = ((payloadSize - sizeof(int) - sizeof(int)) / frames) - sizeof(long); while (frames-- > 0) { FrameCapture capture = new FrameCapture(); capture.FrameIndex = reader.ReadInt64(); capture.Capture = reader.ReadBytes(pixelSize); result.Add(capture); } break; } case 4: { result.TotalFramesInVideo = reader.ReadInt32(); break; } case 5: { result.DurationNumerator = reader.ReadInt64(); result.DurationDenominator = reader.ReadInt64(); break; } } } if (result.TotalFramesInVideo == 0) { result.TotalFramesInVideo = result.Count; } return(result); } }
private void ProcessVideo(string fileName) { VideoFileReader reader = new VideoFileReader(); reader.Open(fileName); System.Drawing.Color[] samples = new System.Drawing.Color[_captureRect.Width * _captureRect.Height]; FrameCaptureCollection frameSamples = new FrameCaptureCollection(); bool indeterminate = reader.FrameCount <= 0; if (!indeterminate) { frameSamples.TotalFramesInVideo = (int)reader.FrameCount; frameSamples.DurationNumerator = reader.FrameCount * reader.FrameRate.Denominator; frameSamples.DurationDenominator = reader.FrameRate.Numerator; } //List<byte> allAudio = new List<byte>(); frameSamples.VideoFile = _videoFile; frameSamples.CaptureRect = _captureRect; long frame = 0; DateTime start = DateTime.Now; int preview = 0; Dictionary <long, BitmapSource> thumbnails = new Dictionary <long, BitmapSource>(); do { //List<byte> audio = new List<byte>(); var current = reader.ReadVideoFrame(); if (current == null) { break; } frame++; //allAudio.AddRange(allAudio); for (int i = 0; i < samples.Length; i++) { samples[i] = current.GetPixel(_captureRect.X + i % _captureRect.Width, _captureRect.Y + i / _captureRect.Width); } frameSamples.Add(new FrameCapture(frame, samples)); if (frame % 25 == 0) { preview++; var hBitmap = current.GetHbitmap(); var capture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(current.Width, current.Height)); capture.Freeze(); if (preview % 10 == 0) { BitmapSource thumbnail = Resize(capture, 200, 130); thumbnails.Add(frame, thumbnail); } DeleteObject(hBitmap); double progressValue; string progressText; if (!indeterminate) { progressValue = (double)frame / reader.FrameCount; progressText = $"{frame} / {reader.FrameCount} ({progressValue:P})"; TimeSpan elapsed = DateTime.Now - start; TimeSpan averagePerFrame = elapsed.Divide(frame); long left = Math.Max(0, reader.FrameCount - frame - 1); TimeSpan timeLeft = averagePerFrame.Multiply(left); progressText += $" ETA {timeLeft:mm\\:ss}"; } else { progressValue = 0.0; progressText = $"{frame} / Unknown"; } Dispatcher.Invoke(() => { if (_isClosing) { return; } Image = capture; txtProgress.Text = progressText; TaskbarItemInfo.ProgressValue = progressValue; proTotal.Value = progressValue; if (indeterminate) { TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate; proTotal.IsIndeterminate = true; } }); } current.Dispose(); } while (_running); //byte[] a = allAudio.ToArray(); //Signal s = new Signal(a, 1, a.Length, reader.SampleRate, SampleFormat.Format8BitUnsigned); //for (int index = 0; index < frameSamples.Count; index++) //{ // var frameSample = frameSamples[index]; // frameSample.AudioLevel = s.GetSample(0, (int) frameSample.FrameIndex); //} long framesSampled = frame; long expectedFrames = frameSamples.TotalFramesInVideo; long sampledFrames = frameSamples.Count; if (frameSamples.TotalFramesInVideo == 0) { frameSamples.TotalFramesInVideo = (int)frame; frameSamples.DurationNumerator = frame * reader.FrameRate.Denominator; frameSamples.DurationDenominator = reader.FrameRate.Numerator; } Dispatcher.BeginInvoke(new Action(() => { if (_isClosing) { return; } Thumbnails = thumbnails; Result = frameSamples; DialogResult = true; })); }
private void ProcessVideo(string fileName) { VideoFileReader reader = new VideoFileReader(); reader.Open(fileName); System.Drawing.Color[] samples = new System.Drawing.Color[_captureRect.Width * _captureRect.Height]; FrameCaptureCollection frameSamples = new FrameCaptureCollection(); bool indeterminate = reader.FrameCount <= 0; if (!indeterminate) { frameSamples.TotalFramesInVideo = (int)reader.FrameCount; frameSamples.DurationNumerator = reader.FrameCount * reader.FrameRate.Denominator; frameSamples.DurationDenominator = reader.FrameRate.Numerator; } frameSamples.VideoFile = _videoFile; frameSamples.CaptureRect = _captureRect; long frame = 0; DateTime start = DateTime.Now; do { var current = reader.ReadVideoFrame(); if (current == null) { break; } frame++; for (int i = 0; i < samples.Length; i++) { samples[i] = current.GetPixel(_captureRect.X + i % _captureRect.Width, _captureRect.Y + i / _captureRect.Width); } frameSamples.Add(new FrameCapture(frame, samples)); if (frame % 25 == 0) { var hBitmap = current.GetHbitmap(); var capture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(current.Width, current.Height)); capture.Freeze(); DeleteObject(hBitmap); double progressValue; string progressText; if (!indeterminate) { progressValue = (double)frame / reader.FrameCount; progressText = $"{frame} / {reader.FrameCount} ({progressValue:P})"; TimeSpan elapsed = DateTime.Now - start; TimeSpan averagePerFrame = elapsed.Divide(frame); long left = Math.Max(0, reader.FrameCount - frame - 1); TimeSpan timeLeft = averagePerFrame.Multiply(left); progressText += $" ETA {timeLeft:mm\\:ss}"; } else { progressValue = 0.0; progressText = $"{frame} / Unknown"; } Dispatcher.Invoke(() => { if (_isClosing) { return; } Image = capture; txtProgress.Text = progressText; TaskbarItemInfo.ProgressValue = progressValue; proTotal.Value = progressValue; if (indeterminate) { TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate; proTotal.IsIndeterminate = true; } }); } current.Dispose(); } while (_running); long framesSampled = frame; long expectedFrames = frameSamples.TotalFramesInVideo; long sampledFrames = frameSamples.Count; if (frameSamples.TotalFramesInVideo == 0) { frameSamples.TotalFramesInVideo = (int)frame; frameSamples.DurationNumerator = frame * reader.FrameRate.Denominator; frameSamples.DurationDenominator = reader.FrameRate.Numerator; } Dispatcher.BeginInvoke(new Action(() => { if (_isClosing) { return; } Result = frameSamples; DialogResult = true; })); }