Search code examples
pythonvideomjpeg

Construct an MJPEG from a bunch of JPEGs


I am programming this in Python. I have a bunch of JPEGs and their time-stamps in a database. They are not exactly periodic (the frame-rate is a bit unstable, +/- a few milliseconds per frame). I want to query the database and construct an MJPEG file that I can save to disk and further transcode with ffmpeg if desired so. How do I do that (don't need code, just some high-level comments if this is possible, which modules if any to use, etc.)?


Solution

  • You can dynamically create video frames in FFmpeg via sending RGB data from Python.

    Why not just read the JPEGs and send their decoded RGB data to FFmpeg to create an H264 video file? The H264 video frames can, later on, be put into MP4 container together with any sound, subtitles etc.

    You need to run FFmpeg as a process in Python and then write the RGB byte array to the stdin of the process. Each write makes a frame so if you need a picture to display for so many millisecs then you just calculate how many times to send same RGB array.

    For example if you setup FFmpeg to output a 30 FPS video and you want a JPEG to display for 2 seconds, you write the same JPEG's decoded RGB data 60 times (using a for-loop).

    An ideal function looks like this writeFrame (rgbData, numTimesToWrite); where numTimesToWrite would be 60. Then to use, you simply decode a JPEG to RGB and then feed into that function with how many frames to write. Since there is 1000 msecs per second, any 1 frame represents around 33 millisecs of output video.

    • See this Python tutorial for an idea on writing frames. You need to put pipe.proc.stdin.write in a function that is called whenever you want to write a frame.

    • This is how I did it (see function makeVideoFrame() for ideas) using the ActionScript-3 language (any language with Standard In/Out features can do it).