Can I use ffmpeg to output JPEGs to a memory stream instead of a file?
Asked Answered
C

2

7

I have a C# app. I have 100 JPEGs (for an example).

I can easily encode this into a video file.

When it has finished encoding on the client app I FTP upload to my server.

It would be 'neater' if I could not write the video file to a disc but instead write it to a memory stream (or array of bytes) and upload via web service perhaps?

I have checked out the ffmpeg documentation but as a C# developer I do not find it natural to understand the C examples.

I guess my first question is it possible, and if so can anyone post an example code in C#? At the moment I am using the process class in C# to execute ffmpeg.

Cribble answered 28/10, 2013 at 10:26 Comment(3)
Note that FFmpeg command line questions go to Super User, while FFmpeg API programming questions belong on Stack Overflow.Rescission
possible duplicate of Can you "stream" images to ffmpeg to construct a video, instead of saving them to disk?Holley
ffmpeg runs as a separate process, you cannot instantiate a stream directly hooking into or coming out of that. You can let ffmpeg pipe its output to stdin though, see the duplicate link and codeproject.com/Articles/16011/… for example.Holley
K
8

Your process method is already good, just needs adjustments:

  1. Set StartupInfo.RedirectStandardOutput = true and StartupInfo.UseShellExecute = false.
  2. Instead of an output file name, call ffmpeg with pipe:, which will make it write to the standard output. Also, since the format cannot be determined from the file name anymore, make sure you use the -f <format> switch as well.
  3. Start the process.
  4. Read from Process.StandardOutput.BaseStream (.BaseStream, so the StreamReader that is .StandardOutput doesn't mess anything up) while the process is running into your memory stream.
  5. Read anything still remaining buffered in Process.StandardOutput.BaseStream.
  6. Profit.

I coded a thumbnailer a while back (BSD 2-clause), that has actual code that demonstrates this. Doesn't matter if it is an image or a video coming out of ffmpeg in the end.

Kalliekallista answered 28/10, 2013 at 16:35 Comment(2)
AN interesting situation cropped up. Have you ever had an occasion were the Process 'hangs' whilst doing this? I just have. If I output it to a file on the hard drive no problem but outputting and capturing the base stream my code hangs...Cribble
Not really. But is it easy enough to deadlock something by not draining stdout (or stderr, if redirected) or not filling stdin (if redirected). See the remarks section of RedirectOutputStream. Without the actual code you're using, it's hard to tell anything. Anyway, you should file a new question.Kalliekallista
B
0

When using the nuget FFMpegCore for c# you can interact using streams like this:

// probe
using var streamInProbe = new MemoryStream(file.Content);
var probe = await FFProbe.AnalyseAsync(streamInProbe, ffOptions);

// prepare streams
using var inPipeStream = new MemoryStream(file.Content);
var inPipeThumb = new StreamPipeSource(inPipeStream);

using var streamOutThumb = new MemoryStream();
var outPipeThumbnail = new StreamPipeSink(streamOutThumb);


var args = FFMpegArguments
    .FromPipeInput(inPipeThumb, o => {/* your options here*/})
    .OutputToPipe(outPipeThumbnail, o => {/* your options here*/});
await args.ProcessAsynchronously(true, ffOptions);

You can find a longer example which is actually outputting images here: How to get snapshot from video memorystream or byte[] using FFmpegCore instead of file path?

Bingham answered 10/7, 2024 at 16:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.