user1765862
user1765862

Reputation: 14155

Using pipes with FFMpeg as an input

I'm using FFMPEG library to manipulate video on user upload.

public async Task ManageVide(IFormFile file)
{   
   ... process file
   string command = $"-i inputFile.mp4 -vf -s 800x600 outFile.mp4";
   ...
}

is it possible to use memory stram as an input and output of the ffmpeg command? I read somewhere that for this can be used ffmpeg pipe. But I don't know how to structure the command

public async Task ManageVide(MemoryStream stream)
{   
    string command = $"-i pipe:{stream} -vf -s 800x600 test.mp4";
    ...
}

Upvotes: 2

Views: 1907

Answers (2)

tqk2811
tqk2811

Reputation: 568

You can use FFmpegArgs
Example

using FileStream input = new FileStream(".\\Images\\img0.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
using FileStream output = new FileStream("img0_out.jpg", FileMode.Create, FileAccess.Write, FileShare.Read);

FFmpegArg ffmpegArg = new FFmpegArg();
var videoMap = ffmpegArg.AddVideoInput(new VideoPipeInput(input, DemuxingFileFormat.mjpeg), 1, 0);
var imageMap = videoMap.ImageMaps.First().ScaleFilter().W("iw/2").H("ih/2").MapOut;
ffmpegArg.AddOutput(new ImagePipeOutput(output, MuxingFileFormat.mjpeg, imageMap));

var result = ffmpegArg.Render(c => c.WithFFmpegBinaryPath("path to ffmpeg")).Execute();

From arguments

var result = FFmpegRender
  .FromArguments(args,c => c.WithFFmpegBinaryPath("path to ffmpeg"))
  .WithStdInStream(pipein)
  .Execute();

Upvotes: 0

Christoph K
Christoph K

Reputation: 354

I've been using the library CliWrap to work with FFMPEG. A simple example, of reading and writing from a memorystream:

await Cli.Wrap("Path/to/exe")
         .WithArguments(arguments)
         .WithStandardInputPipe(PipeSource.FromStream(source))
         .WithStandardOutputPipe(PipeTarget.ToStream(destinationStream))
         .ExecuteAsync(cancellationToken);

destinationStream.Position = 0;

Upvotes: 5

Related Questions