How to decode a MJPEG file

Posted on 2012-09-04
Last Modified: 2013-11-08
I need help to understand how one specific MJPEG file is put together. The file(s) are produced by a Synology Disk Station. The disk station is connected to Axis M1054 cameras which produce a MJPEG stream. I believe that the disk station is actually just saving images from the camera directly to the disk, but at the same time, it probably adds encapsulation to the file. I want to build a program that can browse quickly in the files produced by the disk, so I need information on how to read the file. I tried to read the file by VirtualDub and it was capable to browse through it super fast, so that's why I believe it is a MJPEG file. I will put it out on a .torrent soon, cause it is very big.

Are there any programs that will show me which bytes are for what, like Wireshark is doing for a network packet?
Question by:lefodnes

    Author Comment

    I analyzed the file in MediaInfo, and I'm attaching the result from it. I'm not allowed to upload a torrent file here, so it is accessible from
    LVL 69

    Expert Comment

    I find wiki is good for a starter explains the inner workings
    Motion JPEG
    MJPEG Tools
    The mjpeg programs are a set of tools that can do recording of videos and playback, simple cut-and-paste editing and the MPEG compression of audio and video. Command line tools

    Author Comment

    From what I can tell, wiki gives no information about the inner workings of Motion JPEG, except the workings of a http stream. Just pro/cons, tools that can edit video, which is not what I am after. I am not looking for simple editing software (MJPEG-Tools).

    I'm looking for information on the protocol, so that I will be able to decode AVI/MJPEG from the byte level of a file.
    LVL 30

    Expert Comment

    Hi lefodnes,

    did you already take a look at ffmpeg? With this tool you should be able to extract single frames from the MJPEG. You can find it here:

    If you want to write your own program to extract frames I think you could use libav which is a free library which supports audio/video en-/decoding:


    Author Comment

    Hi Zoppo,
    Thank you for the link to I will have a look at it.
    My goal is to make something that reads the file in an optimized way, since the file is stored on a Synology on a LAN. I'd like to download only certain parts of the file on the fly, and build this file locally as a cache. This's why I need to understand the bits and bytes of the file. In my search I will also be looking closer at the RIFF/AVI format, since this MJPEG is contained in an AVI.
    As I mentioned earlier, I do not need a program or library that can show or edit the file as video or images. I need a program from which I can learn how to read and browse quickly inside an AVI / MJPEG file.
    LVL 81

    Assisted Solution

    LVL 25

    Accepted Solution

    The Synology unit stores the mjpeg in avi format but the 'frames' or jpg files are easily extracted.

    All you need to do is scan the file for the start marker for the jpg pictures,  it will be a hex sequence 0xFF 0xD8.  This is the start of a jpg picture.  Then just scan ahead for the next start marker and everything before it is the jpg file - including the start marker.


    0xFF 0xD8 ...... picture data ...  0xFF 0xD8 .... next picture data ... etc

    You can check this out with a hex editor so you can look at the raw data in the files.  A hex editor is the equivalent to wireshark but for files.  

    Heres a free editor that works good,

    Search out the marker and you can check it out by cutting out a frame and pasting it into a new file.  I actually verified this on a file from a synology unit just to be sure they did not re-encode anything.

    Jpeg uses the 0xFF <2nd hex value> as the start for headers and encoding information.  The 0xFF 0xD8 is always the start of the whole picture though.

    When you look at jpg files with the hex editor you will likely see other 0xFF sections at the start with other information.  For example I'm looking at a jpg file and it shows me a date time and that the file was created with Adobe Photoshop.  If the Axis camera puts in a date and time you would be able to extract that as well.

    Author Closing Comment

    Hi kode99 and leakim971,

    Thank you both for providing good information.

    kode99, I appreciate that you actually verified the file format and described this in a good way.

    leakim971, Thank you for the links. I have not read them all yet, but you both provided very good information for me to continue working with this project.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Adobe Customization Wizard XI issues 26 139
    scoreUp challenge 14 40
    powerN  challenge 3 36
    How to make text scroll for a video? 3 15
    Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
    This is about my first experience with programming Arduino.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now