Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect mp4 files

Tags:

c

mp4

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?

like image 573
Parth Shah Avatar asked Nov 02 '12 05:11

Parth Shah


2 Answers

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.

like image 145
iabdalkader Avatar answered Nov 02 '22 13:11

iabdalkader


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";
        }
    }
like image 45
Ali Bagheri Avatar answered Nov 02 '22 14:11

Ali Bagheri