0
I'm working with ffmpeg to process an incoming MPEGTS stream from remote cameras, and deliver it to multiple clients using my app.
Technically, I'm using ffmpeg to convert the incoming stream to an MJPEG output, and piping the data chunks (from the ffmpeg process stdout) to a writeable stream on the client http response.
However, I'm facing a problem- not all data chunks represent a full 'whole' frame. thus, displaying them in a row in the browser, results in a flickering video, with half-complete frames, on a random basis. I know this because when printing each chunk length, results most of the time in a big value (X), but every now and then I get 2 consecutive chunks with length (2/5X) followed by (3/5X) for example.
So the question - is there a way to force the ffmpeg process to output only whole frames? if not, is there a way for me to check each data chunk 'manually' and look for headers/metadata/flags to indicate frame start/end?
my ffmpeg command for outputting MJPEG is:
ffmpeg -i - -c:v mjpeg -f mjpeg -
explained:
"-i -" : (input) is the stdin of the process (and not a static file)
"-c:v mjpeg" : using the mjpeg codec
"-f mjpeg" : output will be in the mjpeg format
"-" : output not specified (file or url) - will be the process stdout
Motion JPEG consists exclusively of full frames. Sending the data in chunks should also not cause a problem. That leaves only the receiving logic as the culprit. It simply must wait for the frame to be complete before showing it. – Daniel B – 2019-12-24T14:50:20.173
and how can I achieve this on the receiving end? for each chunk of data received, how do I know to wait for the next one and add them together to show a full frame? does mjpeg contain any headers or metadata I can read to get a hint to that? – Maoration – 2019-12-24T14:55:43.287
Every frame is a JPEG image. Refer to this answer for a method to check for completeness.
– Daniel B – 2019-12-24T15:23:04.153following your suggestions, here is what I implemented: https://stackoverflow.com/questions/59468403/ffmpeg-how-to-force-mjpeg-output-of-whole-frames/59479278#59479278
– Maoration – 2019-12-25T14:29:04.690