Added StreamPipeSource

This commit is contained in:
Максим Багрянцев 2020-04-27 21:22:05 +03:00
parent 9a4d200483
commit 9903e333e6
3 changed files with 109 additions and 0 deletions

View file

@ -17,6 +17,7 @@ public enum ImageType
public static class VideoLibrary
{
public static readonly FileInfo LocalVideo = new FileInfo($".{Path.DirectorySeparatorChar}Resources{Path.DirectorySeparatorChar}input.mp4");
public static readonly FileInfo LocalVideoWebm = new FileInfo($".{Path.DirectorySeparatorChar}Resources{Path.DirectorySeparatorChar}input.webm");
public static readonly FileInfo LocalVideoAudioOnly = new FileInfo($".{Path.DirectorySeparatorChar}Resources{Path.DirectorySeparatorChar}audio_only.mp4");
public static readonly FileInfo LocalVideoNoAudio = new FileInfo($".{Path.DirectorySeparatorChar}Resources{Path.DirectorySeparatorChar}mute.mp4");
public static readonly FileInfo LocalAudio = new FileInfo($".{Path.DirectorySeparatorChar}Resources{Path.DirectorySeparatorChar}audio.mp3");

View file

@ -63,6 +63,61 @@ public bool Convert(VideoType type, bool multithreaded = false, VideoSize size =
}
}
private void ConvertFromStreamPipe(VideoType type, ArgumentContainer container)
{
var output = Input.OutputLocation(type);
try
{
var input = VideoInfo.FromFileInfo(VideoLibrary.LocalVideoWebm);
using (var inputStream = System.IO.File.OpenRead(input.FullName))
{
var pipeSource = new StreamPipeSource(inputStream);
var arguments = new ArgumentContainer { new InputPipeArgument(pipeSource) };
foreach (var arg in container)
{
arguments.Add(arg.Value);
}
arguments.Add(new OutputArgument(output));
var scaling = container.Find<ScaleArgument>();
Encoder.Convert(arguments);
var outputVideo = new VideoInfo(output.FullName);
Assert.IsTrue(File.Exists(output.FullName));
Assert.IsTrue(Math.Abs((outputVideo.Duration - input.Duration).TotalMilliseconds) < 1000.0 / input.FrameRate);
if (scaling == null)
{
Assert.AreEqual(outputVideo.Width, input.Width);
Assert.AreEqual(outputVideo.Height, input.Height);
}
else
{
if (scaling.Value.Width != -1)
{
Assert.AreEqual(outputVideo.Width, scaling.Value.Width);
}
if (scaling.Value.Height != -1)
{
Assert.AreEqual(outputVideo.Height, scaling.Value.Height);
}
Assert.AreNotEqual(outputVideo.Width, input.Width);
Assert.AreNotEqual(outputVideo.Height, input.Height);
}
}
}
finally
{
if (File.Exists(output.FullName))
File.Delete(output.FullName);
}
}
public void Convert(VideoType type, ArgumentContainer container)
{
var output = Input.OutputLocation(type);
@ -193,6 +248,13 @@ public void Video_ToMP4_Args_Pipe()
ConvertFromPipe(VideoType.Mp4, container);
}
[TestMethod]
public void Video_ToMP4_Args_StreamPipe()
{
var container = new ArgumentContainer { new VideoCodecArgument(VideoCodec.LibX264) };
ConvertFromStreamPipe(VideoType.Mp4, container);
}
[TestMethod]
public void Video_ToTS()
{

View file

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace FFMpegCore.FFMPEG.Pipes
{
/// <summary>
/// Implementation of <see cref="IPipeSource"/> used for stream redirection
/// </summary>
public class StreamPipeSource : IPipeSource
{
public System.IO.Stream Source { get; private set; }
public int BlockSize { get; set; } = 4096;
public StreamPipeSource(System.IO.Stream stream)
{
Source = stream;
}
public void FlushData(System.IO.Stream pipe)
{
var buffer = new byte[BlockSize];
int read;
while ((read = Source.Read(buffer, 0, buffer.Length)) != 0)
{
pipe.Write(buffer, 0, read);
}
}
public async Task FlushDataAsync(System.IO.Stream pipe)
{
var buffer = new byte[BlockSize];
int read;
while ((read = await Source.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
await pipe.WriteAsync(buffer, 0, read);
}
}
public string GetFormat()
{
return "";
}
}
}