5

I have 100 jpegs.

I use ffmpeg to encode to a video file which is written to a hard drive.

Is there a way to pipe it directly to a byte/stream?

I am using C# and I am using the process class to initate ffmpeg.

Thanks

4 Answers 4

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");
        }
    }
}
2
  • 2
    dear sir, i have tried your solution, but this throws an error "pipe ended", any help please?
    – Zakir_SZH
    Feb 16, 2019 at 6:53
  • Quick question, what if we want the process to keep working and give us frame by frame as an image bytes, like 5 files is enough Jan 20, 2020 at 22:55
3

in case anyone wanted to know. Adding '-' at the end of the arguments will redirect the stream to standard output which can be caught if you subscribe to the OutputDataReceived event of the process class.

1
  • Does it redirect the output of ffmpeg or its log? May 19, 2016 at 6:00
2

Instead of running ffmpeg process you should directly access ffmpeg library from your code. For example, check out AForge.Net. Among other things it has a ffmpeg managed wrapper. You are intersted in AForge.Video.FFMPEG.VideoFileWriter class, which does exactly that - writes images to video file stream using specified encoder. See online documentation for details.

2
  • Hi, I thought it only produces an avi file? I know Aforge quite well but I could not see a way to pass arguments like I would to ffmpeg? Oct 29, 2013 at 12:55
  • @AndrewSimpson, you are probably right. If file format is a limiting factor for you, you can look up other wrappers on the net (i cant give you a recommendation tho). Or create your own. I think it would be a better solution for such task then using a command line. However, its up to you, ofc.
    – Nikita B
    Oct 29, 2013 at 13:33
2

I have found this post few weeks ago when I was looking for answer for my problem. I tried to start ffmpeg process and pass arguments to it but it take sooo long to do everything. At this point I use Xabe.FFmpeg as it doing this out of the box and don't have to worry about ffmpeg executables because have feature to download latest version.

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();
2
  • 1
    This is a commercial (have to pay for product). So no good to me. But thanks Aug 15, 2018 at 9:13
  • It's free for non-commercial use :) Aug 20, 2018 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.