Detect mp4 files

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.


  • 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.


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";
        }
    }

Tags:

C

Mp4