This commit is contained in:
Malte Rosenbjerg 2020-12-03 20:47:20 +01:00
parent 96e0a03630
commit 8b45a6b680
10 changed files with 36 additions and 17 deletions

View file

@ -312,6 +312,23 @@ await FFMpegArguments
.ProcessAsynchronously(); .ProcessAsynchronously();
}); });
} }
[TestMethod, Timeout(10000)]
public void Video_StreamFile_OutputToMemoryStream()
{
// using var input = File.OpenRead(VideoLibrary.LocalVideo.FullName);
var output = new MemoryStream();
FFMpegArguments
// .FromFileInput(VideoLibrary.LocalVideo.FullName)
.FromPipeInput(new StreamPipeSource(File.OpenRead(VideoLibrary.LocalVideoWebm.FullName)), options => options.ForceFormat("webm"))
.OutputToPipe(new StreamPipeSink(output), options => options
.ForceFormat("mp4"))
.ProcessSynchronously();
output.Position = 0;
var result = FFProbe.Analyse(output);
Console.WriteLine(result.Duration);
}
[TestMethod, Timeout(10000)] [TestMethod, Timeout(10000)]
public void Video_ToMP4_Args_StreamOutputPipe_Failure() public void Video_ToMP4_Args_StreamOutputPipe_Failure()

View file

@ -17,7 +17,7 @@ public InputPipeArgument(IPipeSource writer) : base(PipeDirection.Out)
Writer = writer; Writer = writer;
} }
public override string Text => $"-y {Writer.GetFormat()} -i \"{PipePath}\""; public override string Text => $"{(!string.IsNullOrEmpty(Writer.Format) ? $"-f {Writer.Format} " : string.Empty)}-i \"{PipePath}\"";
protected override async Task ProcessDataAsync(CancellationToken token) protected override async Task ProcessDataAsync(CancellationToken token)
{ {
@ -25,6 +25,7 @@ protected override async Task ProcessDataAsync(CancellationToken token)
if (!Pipe.IsConnected) if (!Pipe.IsConnected)
throw new TaskCanceledException(); throw new TaskCanceledException();
await Writer.WriteAsync(Pipe, token).ConfigureAwait(false); await Writer.WriteAsync(Pipe, token).ConfigureAwait(false);
Pipe.Disconnect();
} }
} }
} }

View file

@ -22,6 +22,7 @@ protected override async Task ProcessDataAsync(CancellationToken token)
if (!Pipe.IsConnected) if (!Pipe.IsConnected)
throw new TaskCanceledException(); throw new TaskCanceledException();
await Reader.ReadAsync(Pipe, token).ConfigureAwait(false); await Reader.ReadAsync(Pipe, token).ConfigureAwait(false);
Pipe.Disconnect();
} }
} }
} }

View file

@ -43,7 +43,6 @@ public async Task During(CancellationToken cancellationToken = default)
catch (TaskCanceledException) catch (TaskCanceledException)
{ {
} }
Pipe.Disconnect();
} }
protected abstract Task ProcessDataAsync(CancellationToken token); protected abstract Task ProcessDataAsync(CancellationToken token);

View file

@ -65,8 +65,8 @@ void OnCancelEvent(object sender, EventArgs args)
{ {
errorCode = t.Result; errorCode = t.Result;
cancellationTokenSource.Cancel(); cancellationTokenSource.Cancel();
_ffMpegArguments.Post(); // _ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token)); }), _ffMpegArguments.During(cancellationTokenSource.Token).ContinueWith(t => _ffMpegArguments.Post()));
} }
catch (Exception e) catch (Exception e)
{ {
@ -111,8 +111,8 @@ await Task.WhenAll(instance.FinishedRunning().ContinueWith(t =>
{ {
errorCode = t.Result; errorCode = t.Result;
cancellationTokenSource.Cancel(); cancellationTokenSource.Cancel();
_ffMpegArguments.Post(); // _ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false); }), _ffMpegArguments.During(cancellationTokenSource.Token).ContinueWith(t => _ffMpegArguments.Post())).ConfigureAwait(false);
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -8,7 +8,7 @@ namespace FFMpegCore.Pipes
/// </summary> /// </summary>
public interface IPipeSource public interface IPipeSource
{ {
string GetFormat(); string Format { get; }
Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken); Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken);
} }
} }

View file

@ -11,9 +11,10 @@ namespace FFMpegCore.Pipes
/// </summary> /// </summary>
public class RawVideoPipeSource : IPipeSource public class RawVideoPipeSource : IPipeSource
{ {
public string StreamFormat { get; private set; } = null!;
public int Width { get; private set; } public int Width { get; private set; }
public int Height { get; private set; } public int Height { get; private set; }
public string Format { get; private set; }
public int FrameRate { get; set; } = 25; public int FrameRate { get; set; } = 25;
private bool _formatInitialized; private bool _formatInitialized;
private readonly IEnumerator<IVideoFrame> _framesEnumerator; private readonly IEnumerator<IVideoFrame> _framesEnumerator;
@ -35,14 +36,14 @@ public string GetFormat()
if (!_framesEnumerator.MoveNext()) if (!_framesEnumerator.MoveNext())
throw new InvalidOperationException("Enumerator is empty, unable to get frame"); throw new InvalidOperationException("Enumerator is empty, unable to get frame");
} }
StreamFormat = _framesEnumerator.Current!.Format; Format = _framesEnumerator.Current!.Format;
Width = _framesEnumerator.Current!.Width; Width = _framesEnumerator.Current!.Width;
Height = _framesEnumerator.Current!.Height; Height = _framesEnumerator.Current!.Height;
_formatInitialized = true; _formatInitialized = true;
} }
return $"-f rawvideo -r {FrameRate} -pix_fmt {StreamFormat} -s {Width}x{Height}"; return $"-f rawvideo -r {FrameRate} -pix_fmt {Format} -s {Width}x{Height}";
} }
public async Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken) public async Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken)
@ -62,10 +63,10 @@ public async Task WriteAsync(System.IO.Stream outputStream, CancellationToken ca
private void CheckFrameAndThrow(IVideoFrame frame) private void CheckFrameAndThrow(IVideoFrame frame)
{ {
if (frame.Width != Width || frame.Height != Height || frame.Format != StreamFormat) if (frame.Width != Width || frame.Height != Height || frame.Format != Format)
throw new FFMpegException(FFMpegExceptionType.Operation, "Video frame is not the same format as created raw video stream\r\n" + 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" + $"Frame format: {frame.Width}x{frame.Height} pix_fmt: {frame.Format}\r\n" +
$"Stream format: {Width}x{Height} pix_fmt: {StreamFormat}"); $"Stream format: {Width}x{Height} pix_fmt: {Format}");
} }
} }
} }

View file

@ -10,15 +10,13 @@ public class StreamPipeSource : IPipeSource
{ {
public System.IO.Stream Source { get; } public System.IO.Stream Source { get; }
public int BlockSize { get; } = 4096; public int BlockSize { get; } = 4096;
public string StreamFormat { get; } = string.Empty;
public string Format { get; }
public StreamPipeSource(System.IO.Stream source) public StreamPipeSource(System.IO.Stream source)
{ {
Source = source; Source = source;
} }
public Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken) => Source.CopyToAsync(outputStream, BlockSize, cancellationToken); public Task WriteAsync(System.IO.Stream outputStream, CancellationToken cancellationToken) => Source.CopyToAsync(outputStream, BlockSize, cancellationToken);
public string GetFormat() => StreamFormat;
} }
} }

View file

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=ffmpeg/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View file

@ -106,7 +106,7 @@ private static Instance PrepareInstance(string filePath, int outputCapacity)
{ {
FFProbeHelper.RootExceptionCheck(); FFProbeHelper.RootExceptionCheck();
FFProbeHelper.VerifyFFProbeExists(); FFProbeHelper.VerifyFFProbeExists();
var arguments = $"-print_format json -show_format -sexagesimal -show_streams \"{filePath}\""; var arguments = $"-loglevel error -print_format json -show_format -sexagesimal -show_streams \"{filePath}\"";
var instance = new Instance(FFMpegOptions.Options.FFProbeBinary(), arguments) {DataBufferCapacity = outputCapacity}; var instance = new Instance(FFMpegOptions.Options.FFProbeBinary(), arguments) {DataBufferCapacity = outputCapacity};
return instance; return instance;
} }