RawVideoPipeDataWriter updated

Former-commit-id: 5ad1a3931a
This commit is contained in:
Максим Багрянцев 2020-05-02 13:39:48 +03:00
parent b9b13052b9
commit c7047b6a5f

View file

@ -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<IVideoFrame> framesEnumerator;
public RawVideoPipeDataWriter(IEnumerator<IVideoFrame> framesEnumerator)
@ -25,6 +27,8 @@ public RawVideoPipeDataWriter(IEnumerator<IVideoFrame> framesEnumerator)
public RawVideoPipeDataWriter(IEnumerable<IVideoFrame> framesEnumerator) : this(framesEnumerator.GetEnumerator()) { }
public string GetFormat()
{
if (!formatInitialized)
{
//see input format references https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-July/007742.html
if (framesEnumerator.Current == null)
@ -36,6 +40,8 @@ public string GetFormat()
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}");
}
}
}