如何在c#中的ffmpeg中使用管道

我有100个jpeg。

我使用ffmpeg编码为一个写入硬盘的video文件。

有没有办法将它直接传递给字节/流?

我正在使用C#,我正在使用进程类来启动ffmpeg。

谢谢

using System; using System.Diagnostics; using System.Drawing; using System.IO; namespace PipeFfmpeg { class Program { public static void Video(int bitrate, int fps, string outputfilename) { Process proc = new Process(); proc.StartInfo.FileName = @"ffmpeg.exe"; proc.StartInfo.Arguments = String.Format("-f image2pipe -i pipe:.bmp -maxrate {0}k -r {1} -an -y {2}", bitrate, fps, outputfilename); proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); for (int i = 0; i < 500; i++) { using (var ms = new MemoryStream()) { using (var img = Image.FromFile(@"lena.png")) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.WriteTo(proc.StandardInput.BaseStream); } } } } static void Main(string[] args) { Video(5000, 10, "lena.mp4"); } } } 

您应该直接从代码中访问ffmpeg库,而不是运行ffmpeg进程。 例如,查看AForge.Net 。 除此之外,它还有一个ffmpeg托管包装器。 您在AForge.Video.FFMPEG.VideoFileWriter类中AForge.Video.FFMPEG.VideoFileWriter ,它正是这样做的 – 使用指定的编码器将图像写入video文件流。 有关详情,请参阅在线文档

几个星期前,当我在寻找问题的答案时,我发现了这篇文章。 我试图启动ffmpeg进程并将参数传递给它,但是做一切都需要很长时间。 此时我使用Xabe.FFmpeg开箱即用,并且不必担心ffmpeg可执行文件,因为它具有下载最新版本的function。

 bool conversionResult = await new Conversion().SetInput(Resources.MkvWithAudio) .AddParameter(String.Format("-f image2pipe -i pipe:.bmp -maxrate {0}k -r {1} -an -y {2}",bitrate, fps, outputfilename)) .Start(); 

如果有人想知道。 在参数末尾添加“ – ”会将流重定向到标准输出,如果您订阅了流程类的OutputDataReceived事件,则可以捕获该输出。