0

The problem is the following. I need to convert a video to a set of pictures using the ffmpeg process. I have successfully done this before with the following code:

public void VideoToImages1()
    {
        var inputFile = @"D:\testVideo.avi";
        var outputFilesPattern = @"D:\image%03d.jpg";

        using var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                UseShellExecute = false,
                CreateNoWindow = true,
                Arguments = $"-y -i {inputFile} {outputFilesPattern}",
                FileName = "ffmpeg.exe"
            },
            EnableRaisingEvents = true
        };

        process.Start();

        process.WaitForExit();
    }

Now I need to stream video through the input Stream and receive data from the output Stream. For this I have the following code. It's fully working since I used it to convert video and successfully streamed input via Stream and received output via Stream and produced a valid file.

public void VideoToImages2()
    {
        var inputFile = @"D:\testVideo.avi";
        var outputFile = @"D:\resultImages.png";

        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                Arguments = "-y -i - -f image2 -",
                FileName = _ffmpeg
            },
            EnableRaisingEvents = true
        };

        process.Start();

        //Write input data to input stream
        var inputTask = Task.Run(() =>
        {
            using (var input = new FileStream(inputFile, FileMode.Open))
            {
                input.CopyTo(process.StandardInput.BaseStream);
                process.StandardInput.Close();
            }
        });


        //Read multiple files from output stream
        var outputTask = Task.Run(() =>
        {
            //Problem here
            using (var output = new FileStream(outputFile, FileMode.Create))
                process.StandardOutput.BaseStream.CopyTo(output);
        });


        Task.WaitAll(inputTask, outputTask);

        process.WaitForExit();
    }

The problem here is that instead of creating files in a directory according to the specified pattern, it returns these files in a stream. As a result, I do not know how to write all the files from the Stream and how to process this output, since it contains many files. At the moment I have only 1 image created. Help me please.

I tried googling but didn't find anything useful

  • you try use this , execute cmd in C# Visit https://stackoverflow.com/questions/2724542/how-to-convert-avi-file-to-an-jpgs-images-array-using-net – Hậu tran Jan 19 '23 at 06:21
  • you try use this , execute cmd in C# https://stackoverflow.com/questions/2724542/how-to-convert-avi-file-to-an-jpgs-images-array-using-net – Hậu tran Jan 19 '23 at 06:25

1 Answers1

0

You will have to decode the stream on the fly using the signatures of the output files. Any file has a unique signature and by recognizing these signatures you can keep track of the number of bytes related to this file. For example jpeg file starts with signature 0xFF 0xD8 0xFF and ends with signature 0xF9 xF8 if I'm not mistaken. Matching bytes with these signatures will have to find the files contained in the stream.