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

And*_*son 4 c# ffmpeg

我有100个jpeg。

我使用ffmpeg编码为视频文件,然后将其写入硬盘。

有没有一种方法可以将其直接传递到字节/流?

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

谢谢

the*_*max 5

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 亲爱的先生,我已经尝试过您的解决方案,但这会引发错误“管道结束”,请问有什么帮助吗? (2认同)