D.Rosado
D.Rosado

Reputation: 5773

Processing jpgs stream from the standard output

Using FFmpeg I converted a video (mpeg2) into jpgs, redirecting the output into the standard output.

StringBuilder str = new StringBuilder();

    //Input file path
    str.Append(string.Format(" -i {0}", myinputFile));

    //Set the frame rate
    str.Append(string.Format(" -r {0}", myframeRate);

    //Indicate the output format
    str.Append(" -f image2pipe");

    //Connect the output to the standard output
    str.Append(" pipe:1");
    return str.ToString();

I'm able to receive the callback from the process with the data:

_ffmpegProc.OutputDataReceived += new DataReceivedEventHandler(_ffmpegProc_OutputDataReceived);

But how can I save that back into jpg files?, I'd need to know the size of each jpg, but I cant figure out how.

Thanks.

Upvotes: 1

Views: 1647

Answers (1)

fvu
fvu

Reputation: 32953

You can't without reading and at least partially understanding the stream structure because JPEG files don't contain e.g. an explicit length indication somewhere at file start.

I see two possibilities that don't involve transcoding:

  1. store the intermediate files on disk, but I guess that's just what you want to avoid.
  2. try minimally parsing the files - a JPEG file starts with a SOI marker (hex FF D8) and ends with an EOI marker (hex FF D9). With a relatively simple state machine you could try splitting the stream of JPG's into separate frames this way. Now, for robustness sake it's probably better to read the segments individually (see the JPEG file format for more info). You can also study the ffmpeg mjpeg decoder's source implementation, it looks quite robust.

Upvotes: 3

Related Questions