From 8e2b146f95a0bd967ed5b88bc787904efdef1e81 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Mon, 7 Dec 2020 01:11:09 +0100 Subject: [PATCH] Partial revert --- FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs | 2 +- FFMpegCore/FFMpeg/Pipes/IPipeSource.cs | 2 +- FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs | 13 ++++++------- FFMpegCore/FFMpeg/Pipes/StreamPipeSource.cs | 6 ++++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs b/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs index 685a019..757b151 100644 --- a/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs @@ -17,7 +17,7 @@ public InputPipeArgument(IPipeSource writer) : base(PipeDirection.Out) Writer = writer; } - public override string Text => $"{(!string.IsNullOrEmpty(Writer.Format) ? $"-f {Writer.Format} " : string.Empty)}-i \"{PipePath}\""; + public override string Text => $"-y {Writer.GetStreamArguments()} -i \"{PipePath}\""; protected override async Task ProcessDataAsync(CancellationToken token) { diff --git a/FFMpegCore/FFMpeg/Pipes/IPipeSource.cs b/FFMpegCore/FFMpeg/Pipes/IPipeSource.cs index 5fde4ab..cdd5139 100644 --- a/FFMpegCore/FFMpeg/Pipes/IPipeSource.cs +++ b/FFMpegCore/FFMpeg/Pipes/IPipeSource.cs @@ -8,7 +8,7 @@ namespace FFMpegCore.Pipes /// public interface IPipeSource { - string Format { get; } + string GetStreamArguments(); Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken); } } diff --git a/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs b/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs index de3669e..f61bb7c 100644 --- a/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs +++ b/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs @@ -11,10 +11,9 @@ namespace FFMpegCore.Pipes /// public class RawVideoPipeSource : IPipeSource { + public string StreamFormat { get; private set; } = null!; public int Width { get; private set; } public int Height { get; private set; } - - public string Format { get; private set; } public int FrameRate { get; set; } = 25; private bool _formatInitialized; private readonly IEnumerator _framesEnumerator; @@ -26,7 +25,7 @@ public RawVideoPipeSource(IEnumerator framesEnumerator) public RawVideoPipeSource(IEnumerable framesEnumerator) : this(framesEnumerator.GetEnumerator()) { } - public string GetFormat() + public string GetStreamArguments() { if (!_formatInitialized) { @@ -36,14 +35,14 @@ public string GetFormat() if (!_framesEnumerator.MoveNext()) throw new InvalidOperationException("Enumerator is empty, unable to get frame"); } - Format = _framesEnumerator.Current!.Format; + StreamFormat = _framesEnumerator.Current!.Format; Width = _framesEnumerator.Current!.Width; Height = _framesEnumerator.Current!.Height; _formatInitialized = true; } - return $"-f rawvideo -r {FrameRate} -pix_fmt {Format} -s {Width}x{Height}"; + return $"-f rawvideo -r {FrameRate} -pix_fmt {StreamFormat} -s {Width}x{Height}"; } public async Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken) @@ -63,10 +62,10 @@ public async Task WriteAsync(System.IO.Stream outputStream, CancellationToken ca private void CheckFrameAndThrow(IVideoFrame frame) { - if (frame.Width != Width || frame.Height != Height || frame.Format != Format) + 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: {Format}"); + $"Stream format: {Width}x{Height} pix_fmt: {StreamFormat}"); } } } diff --git a/FFMpegCore/FFMpeg/Pipes/StreamPipeSource.cs b/FFMpegCore/FFMpeg/Pipes/StreamPipeSource.cs index 5d9e666..404029f 100644 --- a/FFMpegCore/FFMpeg/Pipes/StreamPipeSource.cs +++ b/FFMpegCore/FFMpeg/Pipes/StreamPipeSource.cs @@ -10,13 +10,15 @@ public class StreamPipeSource : IPipeSource { public System.IO.Stream Source { get; } public int BlockSize { get; } = 4096; - - public string Format { get; } + public string StreamFormat { get; } = string.Empty; public StreamPipeSource(System.IO.Stream source) { Source = source; } + + public string GetStreamArguments() => StreamFormat; + public Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken) => Source.CopyToAsync(outputStream, BlockSize, cancellationToken); } }