/// <summary> /// Calculates remaining and elapsed time. /// </summary> /// <param name="currentFrame">Frame number.</param> public Tuple <TimeSpan, TimeSpan> GetTimeElapsed(int currentFrame) { var timeLeft = new TimeSpan(); var now = DateTime.Now.Ticks; if (_previousTime != 0) { if (_averageTicks == 0) { _startTime = now; _averageTicks = now - _previousTime; timeLeft = new TimeSpan(_averageTicks * (_numberOfFrames - currentFrame - 1)); } else { var delta = FastUtils.FastAbs((int)(_averageTicks - (now - _previousTime))); if (delta > 250000 && delta < 1500000) { _averageTicks = (_averageTicks + (now - _previousTime)) / 2; } timeLeft = new TimeSpan(_averageTicks * (_numberOfFrames - currentFrame - 1)); } } _previousTime = now; return(new Tuple <TimeSpan, TimeSpan>(timeLeft, new TimeSpan(now - _startTime))); }
public void ReportFastTime(int currentFrame) { var timeLeft = new TimeSpan(); var now = DateTime.Now.Ticks; if (_previousTime != 0) { if (_averageTicks == 0) { _startTime = now; _averageTicks = now - _previousTime; timeLeft = new TimeSpan(_averageTicks * (_numberOfFrames - currentFrame - 1)); } else { var delta = FastUtils.FastAbs((int)(_averageTicks - (now - _previousTime))); if (delta > 250000 && delta < 1500000) { _averageTicks = (_averageTicks + (now - _previousTime)) / 2; } timeLeft = new TimeSpan(_averageTicks * (_numberOfFrames - currentFrame - 1)); } } Console.Write($"\rProgress: {Math.Round((double)currentFrame / (_numberOfFrames - 1) * 100, 2)}%, Time left: {timeLeft}, Frame: {currentFrame + 1}/{_numberOfFrames} "); if (currentFrame + 1 == _numberOfFrames) { Console.WriteLine($"\nDone. Total time elapsed: {new TimeSpan(now - _startTime)}"); ResetAverageTime(); } _previousTime = now; }
private static void DrawImage(PictureBox pictureBox, Bitmap image) { var res = new Bitmap(pictureBox.Width, pictureBox.Height); var graphics = Graphics.FromImage(res); graphics.DrawImage(FastUtils.ScaleImage(image, pictureBox.Width, pictureBox.Height, false, true), 0, 0); pictureBox.Image = res; }
private static void PhotoFinish(VideoFileReader reader, VideoFileWriter writer, BackgroundWorker renderWorker) { var numberOfFrames = (int)reader.FrameCount; var width = reader.Width; var height = reader.Height; if (numberOfFrames > width) //if user want -> fit to original width { numberOfFrames = width; } //writer.Width = numberOfFrames; //rewrite for change resolution, open file in method for (var x = 0; x < width; x++) { var convertedBitmap = new FastBitmap(new Bitmap(numberOfFrames, height)); //photofinish одновременная обработка нескольких кадров convertedBitmap.LockBits(); for (var f = 0; f < numberOfFrames; f++) { FastBitmap currentBitmap; try { currentBitmap = new FastBitmap(reader.ReadVideoFrame(f)); } catch (Exception ignored) { break; } currentBitmap.LockBits(); for (var y = 0; y < height; y++) { convertedBitmap.SetPixel(f, y, currentBitmap.GetPixel(x, y)); } currentBitmap.DisposeSource(); } convertedBitmap.UnlockBits(); writer.WriteVideoFrame(convertedBitmap.GetSource()); AppForm.PreviewBitmap = (Bitmap)convertedBitmap.GetSource().Clone(); convertedBitmap.DisposeSource(); renderWorker.ReportProgress(FastUtils.FastRoundInt(x * 1000.0 / width)); } }
private static void OpenFileButtonHandler() { if (OpenFile.ShowDialog() != DialogResult.OK) { return; } Reader.Close(); // close prev videos Reader.Dispose(); Reader = new VideoFileReader(); Writer.Close(); Writer.Dispose(); Writer = new VideoFileWriter(); VideoName = ("" + OpenFile.SafeFileName).Replace("" + Path.GetExtension(OpenFile.FileName), ""); VideoPath = Path.GetDirectoryName(OpenFile.FileName) + @"\";//задаем название и путь к файлу VideoExtension = Path.GetExtension(OpenFile.FileName); ChosenFile.Text = OpenFile.FileName; Reader.Open(OpenFile.FileName); //открываем файл Writer.BitRate = Reader.BitRate; //конфигурируем writer Writer.VideoCodec = VideoCodec.Mpeg4; //Reader videoCodec Writer.FrameRate = Reader.FrameRate; Writer.Height = Reader.Height; Writer.Width = Reader.Width; Writer.Open(GetNameToSave(VideoPath, VideoName, VideoExtension)); TimeLine.Value = 0; //настраиваем временную линию TimeLine.Maximum = (int)Reader.FrameCount; TimeLine.TickFrequency = FastUtils.FastRoundInt((double)Reader.FrameRate); TimeLineEnd.Text = TimeSpan.FromSeconds(Reader.FrameCount / (double)Reader.FrameRate).ToString(@"hh\:mm\:ss\.ff"); Distorter.ResetMaxMinValues(); DrawImage(PreviewPictureBox, //if enable start button? because multiplierFrom can be bad Distorter.RunOneFrame(Reader, 0, MultiplierFrom, MultiplierTo, Function , PreviewPictureBox.Width, PreviewPictureBox.Height)); //рисуем 0 кадр EnableStartButton(StartButton, TimeLine); //пробуем включить кнопку старт }
public static void Run(string videoPath, string videoName) { var reader = new VideoFileReader(); reader.Open(videoPath + videoName); var writer = new VideoFileWriter(); var numberOfFrames = (int)reader.FrameCount; var probeBitmap = new FastBitmap(reader.ReadVideoFrame(0)); probeBitmap.LockBits(); var maxValues = CalcMaxValues(probeBitmap.Width, probeBitmap.Height); var maxX = maxValues.Item1; var maxY = maxValues.Item2; //Console.WriteLine($"{maxX}:{maxY}"); writer.Height = probeBitmap.Height; writer.Width = probeBitmap.Width; writer.FrameRate = 30; writer.VideoCodec = VideoCodec.Mpeg4; writer.BitRate = reader.BitRate; writer.Open(videoPath + "out.avi"); for (var t = 70; t < numberOfFrames; t++) { var convertedBitmap = new FastBitmap(new Bitmap(probeBitmap.Width, probeBitmap.Height)); FastBitmap currentBitmap; try { currentBitmap = new FastBitmap(reader.ReadVideoFrame(t)); } catch (Exception ignored) { break; } convertedBitmap.LockBits(); currentBitmap.LockBits(); for (var x = 0; x < probeBitmap.Width; x++) { for (var y = 0; y < probeBitmap.Height; y++) { var pixel = ConvertToPolar(x - probeBitmap.Width / 2, y - probeBitmap.Height / 2); var convX = FastUtils.FastAbs((int)(pixel.Item2 / maxY * (probeBitmap.Width - 1))); var convY = FastUtils.FastAbs((int)(pixel.Item1 / maxX * (probeBitmap.Height - 1))); convertedBitmap.SetPixel(convX, convY, currentBitmap.GetPixel(x, y)); } } Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 4; i++) { convertedBitmap = CompleteBitmap(convertedBitmap); } sw.Stop(); Console.WriteLine(sw.Elapsed); currentBitmap.UnlockBits(); currentBitmap.DisposeSource(); convertedBitmap.GetSource().Save(videoPath + "test.png"); break; //convertedBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); writer.WriteVideoFrame(convertedBitmap.GetSource()); convertedBitmap.DisposeSource(); } probeBitmap.UnlockBits(); writer.Flush(); }