Detect mp4 files
Asked Answered
A

4

10

I have to design a module that detects mp4 files. If will get any random file as input and it has to tell whether it is a mp4 file or not. Where can I find the specification of headers for the mp4 file? Tried googling but could not find anything except the signature. Any other tips regarding C programming for the module?

Addam answered 2/11, 2012 at 5:48 Comment(0)
S
11

you can check the extension or the file signature (magic number)

http://www.garykessler.net/library/file_sigs.html

http://en.wikipedia.org/wiki/List_of_file_signatures

00 00 00 18 66 74 79 70 33 67 70 35 ....ftyp 3gp5 MPEG-4 video files

Or if you're on Unix/Linux you can parse the output of file(1) from your program.

Edit:

You don't need to scan the whole file to identify it, the signature will do, however, if you have to, note that MP4 is a container for other formats, which means you will probably need to know about MP4 and other formats it contains as well, there's some information here: http://en.wikipedia.org/wiki/MPEG-4_Part_14

I'd use something like libffmpeg to do that instead.

Sauter answered 2/11, 2012 at 5:52 Comment(5)
I also want information about the internal structures. Is it documented anywhere?Addam
@ParthShah if you just want to detect the file you don't need the full specification.Sauter
Yes I know. My team-lead insists that I scan the whole file! I will have to do it.Addam
that sequence is not always correct. Here is better one * * * * 66 74 79 70. * means it can be anythingMurdock
Aftershock where did you get this info from?Paraselene
E
8

read file as byte[] then parse mime.

byte[] header = new byte[20];
    System.arraycopy(fileBytes, 0, header, 0, Math.min(fileBytes.length, header.length));

    int c1 = header[0] & 0xff;
    int c2 = header[1] & 0xff;
    int c3 = header[2] & 0xff;
    int c4 = header[3] & 0xff;
    int c5 = header[4] & 0xff;
    int c6 = header[5] & 0xff;
    int c7 = header[6] & 0xff;
    int c8 = header[7] & 0xff;
    int c9 = header[8] & 0xff;
    int c10 = header[9] & 0xff;
    int c11 = header[10] & 0xff;
    int c12 = header[11] & 0xff;
    int c13 = header[12] & 0xff;
    int c14 = header[13] & 0xff;
    int c15 = header[14] & 0xff;
    int c16 = header[15] & 0xff;
    int c17 = header[16] & 0xff;
    int c18 = header[17] & 0xff;
    int c19 = header[18] & 0xff;
    int c20 = header[19] & 0xff;

if(c1 == 0x00 && c2 == 0x00 && c3 == 0x00)//c4 == 0x20 0x18 0x14
    {
        if(c5 == 0x66 && c6 == 0x74 && c7 == 0x79 && c8 == 0x70)//ftyp
        {
            if(c9 == 0x69 && c10 == 0x73 && c11 == 0x6F && c12 == 0x6D)//isom
                return "video/mp4";

            if(c9 == 0x4D && c10 == 0x53 && c11 == 0x4E && c12 == 0x56)//MSNV
                return "video/mp4";

            if(c9 == 0x6D && c10 == 0x70 && c11 == 0x34 && c12 == 0x32)//mp42
                return "video/m4v";

            if(c9 == 0x4D && c10 == 0x34 && c11 == 0x56 && c12 == 0x20)//M4V
                return "video/m4v"; //flv-m4v

            if(c9 == 0x71 && c10 == 0x74 && c11 == 0x20 && c12 == 0x20)//qt
                return "video/mov";

            if(c9 == 0x33 && c10 == 0x67 && c11 == 0x70 && c17 != 0x69 && c18 != 0x73)
                return "video/3gp";//3GG, 3GP, 3G2
        }

        if(c5 == 0x6D && c6 == 0x6F && c7 == 0x6F && c8 == 0x76)//MOOV
        {
            return "video/mov";
        }
    }
Entresol answered 8/1, 2020 at 12:29 Comment(1)
System.arraycopy ??Sievers
O
2
  • ISO base media file format is freely available and MP4 file format is extension of "Base media format", for most of the cases its enough to understand "Base media format". try google will return a standard.

  • To detect if the file is MP4 you need to read first 8 bytes, which contain (first 4 bytes the size of ATOM and next 4 bytes "FTYP"). after that it contain the majorbrand and compatiblebrans - if either of them is ISOM it is definitely a MP4 file.so you need to parse only initial few bytes to confiem MP4 file.

You can also look at "mp4v2-1.9.1" code to understand MP4 format.

Obeisance answered 13/11, 2012 at 5:31 Comment(1)
Ty for being concise!Cub
S
0

It's a container format. The specification is here.

Even with the specification in hand, you would have a lot of work to do to support all a container could represent in your program; Set realistic requirements.

Soot answered 2/11, 2012 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.