0

I'm trying to read use the pipe feature of ffmpeg to not have to write files in the disk.

my inputs implementations are:

public FFMPEG input(string path)
{
    parameters.Add($"-i {path}");
    return this;
}
public FFMPEG input(byte[] file)
{
    parameters.Add("-i pipe:.mp4");
    files.Add(file);
    return this;
}

my output implementation is:

public FFMPEG output(string format)
{
    _output = $"-f {format} -";
    return this;
}

my run implementation is:

public byte[] run()
{
    if (_output == "")
    {
        throw new Exception("missing output format");
    }

    var process = new Process()
    {
        StartInfo =
        {
            FileName = @"ffmpeg.exe",
            Arguments = String.Join(" ", parameters) + " " + _output,
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            UseShellExecute = false,
        }
    };

    process.Start();

    foreach (var item in files)
    {
        process.StandardInput.BaseStream.Write(item);
    }

    byte[] result;
    using(var memstream = new MemoryStream())
    {
        process.StandardOutput.BaseStream.CopyTo(memstream);
        result = memstream.ToArray();
    }
    process.WaitForExit();

    return result;
}

which works perfectly if I use a file as input:

var result = new FFMPEG()
.input("./assets/video.mp4")
.output("avi")
.run();

but when I try to use bytes input:

var result = new FFMPEG()
    .input(File.ReadAllBytes("./assets/video.mp4"))
    .output("avi")
    .run();

I get the following error:

[mov,mp4,m4a,3gp,3g2,mj2 @ 00000137385a10c0] stream 1, offset 0x30: partial file
Error demuxing input file 0: Invalid data found when processing input
pipe:.mp4: Invalid data found when processing input
Cannot determine format of input stream 0:0 after EOF
Error marking filters as finished
thiago kaique
  • 95
  • 1
  • 6
  • Images of text like the error message above should either be replaced by the text they contain, or augmented with the text they contain. This is because the image is not searchable, nor is it accessible. – Jason Aller Nov 28 '22 at 22:33
  • for anyone with the same problem. as far as i know there is no way to use ffmpeg in memory only. you shuld create temporary files. – thiago kaique Nov 29 '22 at 21:47
  • I think that when FFMPEG reads from the Standard Input, you have to specify explicitly the format of that data. It looks like you did not. See https://stackoverflow.com/questions/45899585/pipe-input-in-to-ffmpeg-stdin for something similar. – EvensF Dec 05 '22 at 21:12
  • @EvensF even specifying the format, it still has problems: 1. It does not read mp4 files as I have metadata at the start or end of the file 2. Even with supported formats like mp3 not every function from ffmpeg works – thiago kaique Dec 14 '22 at 14:28
  • You can't pipe with compression format like .mp4 – Trương Quốc Khánh Dec 16 '22 at 03:45

0 Answers0