问题
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.
回答1:
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:
- store the intermediate files on disk, but I guess that's just what you want to avoid.
- try minimally parsing the files - a JPEG file starts with a
SOImarker (hex FF D8) and ends with anEOImarker (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.
来源:https://stackoverflow.com/questions/11742896/processing-jpgs-stream-from-the-standard-output