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

本文关键字:ffmpeg 管道 | 更新日期: 2023-09-27 18:37:13

我有100个jpeg。

我使用 ffmpeg 编码为写入硬盘驱动器的视频文件。

有没有办法直接将其传送到字节/流?

我正在使用 C#,并且正在使用进程类来初始化 ffmpeg。

谢谢

如何在 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");
        }
    }
}

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

与其运行 ffmpeg 进程,不如直接从代码访问 ffmpeg 库。例如,签出 AForge.Net。除此之外,它还有一个 ffmpeg 托管包装器。您穿插在AForge.Video.FFMPEG.VideoFileWriter类中,该类正是这样做的 - 使用指定的编码器将图像写入视频文件流。有关详细信息,请参阅联机文档。

几周

前,当我在寻找问题的答案时,我发现了这篇文章。我试图启动 ffmpeg 过程并将参数传递给它,但做所有事情都需要很长时间。在这一点上,我使用Xabe.FFmpeg,因为它开箱即用,不必担心ffmpeg可执行文件,因为具有下载最新版本的功能。

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();