5

我有 100 张 JPEG。

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

有没有办法将它直接通过管道传输到字节/流?

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

谢谢

4

4 回答 4

6
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");
        }
    }
}
于 2016-04-21T15:59:36.763 回答
2

您应该直接从代码中访问 ffmpeg 库,而不是运行 ffmpeg 进程。例如,查看AForge.Net。除其他外,它还有一个 ffmpeg 托管包装器。您对课程很感兴趣AForge.Video.FFMPEG.VideoFileWriter,这正是这样做的 - 使用指定的编码器将图像写入视频文件流。有关详细信息,请参阅在线文档。

于 2013-10-29T12:50:56.790 回答
2

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

于 2013-10-29T13:30:47.377 回答
2

几周前,当我为我的问题寻找答案时,我发现了这篇文章。我试图启动 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();
于 2018-08-14T17:22:24.827 回答