From 5ad1a3931afc26884e9a4d7f20dcc1777185b0b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=91=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=8F=D0=BD=D1=86=D0=B5=D0=B2?= Date: Sat, 2 May 2020 13:39:48 +0300 Subject: [PATCH] RawVideoPipeDataWriter updated --- .../FFMPEG/Pipes/RawVideoPipeDataWriter.cs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/FFMpegCore/FFMPEG/Pipes/RawVideoPipeDataWriter.cs b/FFMpegCore/FFMPEG/Pipes/RawVideoPipeDataWriter.cs index fd62f23..ce6bcdf 100644 --- a/FFMpegCore/FFMPEG/Pipes/RawVideoPipeDataWriter.cs +++ b/FFMpegCore/FFMPEG/Pipes/RawVideoPipeDataWriter.cs @@ -1,4 +1,5 @@ using FFMpegCore.FFMPEG.Argument; +using FFMpegCore.FFMPEG.Exceptions; using System; using System.Collections.Generic; using System.Text; @@ -15,6 +16,7 @@ public class RawVideoPipeDataWriter : IPipeDataWriter public int Width { get; private set; } public int Height { get; private set; } public int FrameRate { get; set; } = 25; + private bool formatInitialized = false; private IEnumerator framesEnumerator; public RawVideoPipeDataWriter(IEnumerator framesEnumerator) @@ -26,16 +28,20 @@ public RawVideoPipeDataWriter(IEnumerable framesEnumerator) : this( public string GetFormat() { - //see input format references https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-July/007742.html - if (framesEnumerator.Current == null) + if (!formatInitialized) { - if (!framesEnumerator.MoveNext()) - throw new InvalidOperationException("Enumerator is empty, unable to get frame"); - } - StreamFormat = framesEnumerator.Current.Format; - Width = framesEnumerator.Current.Width; - Height = framesEnumerator.Current.Height; + //see input format references https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-July/007742.html + if (framesEnumerator.Current == null) + { + if (!framesEnumerator.MoveNext()) + throw new InvalidOperationException("Enumerator is empty, unable to get frame"); + } + StreamFormat = framesEnumerator.Current.Format; + Width = framesEnumerator.Current.Width; + Height = framesEnumerator.Current.Height; + formatInitialized = true; + } return $"-f rawvideo -r {FrameRate} -pix_fmt {StreamFormat} -s {Width}x{Height}"; } @@ -44,11 +50,13 @@ public void WriteData(System.IO.Stream stream) { if (framesEnumerator.Current != null) { + CheckFrameAndThrow(framesEnumerator.Current); framesEnumerator.Current.Serialize(stream); } while (framesEnumerator.MoveNext()) { + CheckFrameAndThrow(framesEnumerator.Current); framesEnumerator.Current.Serialize(stream); } } @@ -66,5 +74,12 @@ public async Task WriteDataAsync(System.IO.Stream stream) } } + private void CheckFrameAndThrow(IVideoFrame frame) + { + if (frame.Width != Width || frame.Height != Height || frame.Format != StreamFormat) + throw new FFMpegException(FFMpegExceptionType.Operation, "Video frame is not the same format as created raw video stream\r\n" + + $"Frame format: {frame.Width}x{frame.Height} pix_fmt: {frame.Format}\r\n" + + $"Stream format: {Width}x{Height} pix_fmt: {StreamFormat}"); + } } }