0

I want to send a couple of images to ffmpeg and output a video

I have tried to use something similar to this which always seems to finish but when I check the output, it always returns an empty 48b file

I have tried changing the order of the flags but nothing seems to work

Here is my code

using System.Diagnostics;
using SkiaSharp;

Process process = new Process();
process.StartInfo.FileName = @"ffmpeg";
process.StartInfo.Arguments = "-f image2pipe -r 30 -i - /home/user/Videos/test33.mp4";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
        
process.Start();

for (int i = 0; i < 300; i++)
{
    using var ms = new MemoryStream();
    SKImageInfo imageInfo = new SKImageInfo(1280, 720, SKColorType.Rgba8888);
    using SKSurface surface = SKSurface.Create(imageInfo);
    SKCanvas canvas = surface.Canvas;
    canvas.Clear(SKColor.Parse("#ffffff"));
    using SKImage image = surface.Snapshot();
    using SKData data = image.Encode(SKEncodedImageFormat.Png, 80);
    data.SaveTo(ms);
    ms.WriteTo(process.StandardInput.BaseStream);
}
m_j_alkarkhi
  • 337
  • 4
  • 14
  • I do not think the link code ever worked. When you write to a memory stream the pointer is at end of the stream. So when you do the WriteTo() you are at the end of the stream and you send nothing. So after you SaveTo(ms) you need to set the position to zero : ms.Position = 0; then do the WriteTo(). – jdweng Nov 06 '22 at 12:59
  • @jdweng Still the same result – m_j_alkarkhi Nov 06 '22 at 13:08
  • I don't think ffmpeg will accept inputs from both a file and standard input. Try setting up ffmpeg to accept data from standard input then send a video through c#. – jdweng Nov 06 '22 at 14:22
  • @jdweng As far as I know, I don't have any file input. Are you talking about the end of the command? That's the output, not input – m_j_alkarkhi Nov 06 '22 at 15:11
  • Than what is the mp4 file on the command line? – jdweng Nov 06 '22 at 16:10
  • @jdweng the output – m_j_alkarkhi Nov 06 '22 at 16:11
  • 1
    Try closing stdin pipe after the loop: `process.StandardInput.Close();` You may also try `Flush()` before `Close()`, but I don't think it's necessary. – Rotem Nov 06 '22 at 17:13
  • 1
    +1 to @Rotem - If you don't close the stdin, FFmpeg will hang and your program kills FFmpeg before it produces the output. Once you sort this out, something else to think about from performance standpoint: outputting raw RGB data with `-f rawvideo` may be more performant than using intermediate PNG. – kesh Nov 06 '22 at 17:44
  • Have example [here](https://github.com/tqk2811/FFmpegArgs/blob/bf9c2205b21e1e84a7e2b29fc37fb8762e569d38/FFmpegArgs.Executes/FFmpegRender.cs#L206) and [test](https://github.com/tqk2811/FFmpegArgs/blob/bf9c2205b21e1e84a7e2b29fc37fb8762e569d38/FFmpegArgs.Test/FeatureTest/PipeTest.cs#L15) – Trương Quốc Khánh Dec 16 '22 at 03:50

0 Answers0