2

We need to read some RTSP streams in C# using ffmpeg and pipe the stdout to a MemoryStream. We need to get a snapshot out of each stream. Here's my code so far:

var arguments =
    $@" -y -i {imageUri.AbsoluteUri} -vframes 1 -pix_fmt yuvj420p -vf select='eq(pict_type\,I)' -q:v 1 pipe:1";

var processStartInfo = new ProcessStartInfo
{
    Arguments = arguments,
    FileName = AppDomain.CurrentDomain.BaseDirectory + @"\lib\ffmpeg.exe",
    CreateNoWindow = true,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
var process = new Process
{
    StartInfo = processStartInfo,
    EnableRaisingEvents = true
};

process.Start();
var output = process.StandardError.Read();
process.StandardOutput.BaseStream.CopyTo(memoryStream);

But with above memoryStream.Length is always 0. What's going on? I've looked at this: How to get output from ffmpeg process in c# but I'm reading binary from stdout. And this answer gives little implementation details: https://stackoverflow.com/a/4535927/177416

Wasn't sure how to use this answer: .NET Process - Redirect stdin and stdout without causing deadlock

Alex
  • 34,699
  • 13
  • 75
  • 158
  • 1
    You need to add the `-f` option and declare a format to be used by the pipe to address the error you should be seeing in the ffmpeg console output/log: `Unable to find a suitable output format for 'pipe:1' pipe:1: Invalid argument`. – llogan Apr 12 '19 at 21:16
  • Thanks, @llogan. I’m trying to save a snapshot JPG image into the output. Would I declare the output format as jpeg? The output isn’t a video in this case. – Alex Apr 13 '19 at 04:38
  • 1
    `-f mjpeg`, `-f image2`, or `-f image2pipe` should all work for a piped single image output. You can see the complete list with `ffmpeg -muxers`. – llogan Apr 13 '19 at 05:56
  • Thanks, @llogan. I’ll test this when I’m in the office. The standard error was returning a “102” from my code above but didn’t know what the code indicated. Is there a list of what these codes mean? Thanks – Alex Apr 13 '19 at 19:35
  • 1
    Sorry, but I'm not familiar with c#, so I don't know what it means (it's not an error code from `ffmpeg` itself though). – llogan Apr 13 '19 at 19:36
  • @Ilogan, when I added the `-f image2` to my code above, the ffmpeg process never finishes; it just hangs and has to be killed via Task Manager. However, it works with just an MP4 video file -- but not with RTSP stream. – Alex Apr 17 '19 at 20:22
  • 1
    Eliminate the additional complexity introduced by the code by manually running the same ffmpeg command that the code executes: unscripted in a command-line interface. Does it still hang? Can you provide the RTSP URL so I can try it? – llogan Apr 17 '19 at 21:32
  • Thanks, @Ilogan. Got it working from the command prompt. Had to set `RedirectStandardError = false` otherwise it caused the hanging process (deadlock). – Alex Apr 18 '19 at 12:01
  • @llogan, sorry to bug you. Do you have any ideas on this ffmpeg issue? https://stackoverflow.com/questions/56808123/ffmpeg-returns-method-setup-failed-404-not-found – Alex Jun 28 '19 at 15:01

0 Answers0