你可以查看扩展名或文件签名(幻数)
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视频文件
或者,如果您使用的是Unix/Linux,则可以从程序中解析文件(1)的输出.
编辑:
您不需要扫描整个文件来识别它,签名就可以了,但是,如果必须,请注意MP4是其他格式的容器,这意味着您可能需要了解MP4和其他格式此处还包含一些信息:http: //en.wikipedia.org/wiki/MPEG-4_Part_14
我会用像libffmpeg这样的东西代替.
将文件读取为 byte[] 然后解析 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";
}
}
Run Code Online (Sandbox Code Playgroud)