头部是一个大容器,包含两个box - ftyp和free
- public void writeHeader() throws IOException{
- writeFtypBox();
- writeFreeBox();
- }
- private void writeFtypBox() throws IOException{
- write(intTobyte(20));//box_length
- write("ftyp");//box_type (4字节)
- write("isom");//major_brand (4字节)
- write(intTobyte(512));//minor_version (4 字节)
- write("mp41");//compatible_brands (4字节)
- }
- private void writeFreeBox() throws IOException{
- write(intTobyte(8));
- write("free");
- }
- private byte[] intTobyte(int value){
- byte[] data = new byte[4];
- data[3] = (byte)(value);
- data[2] = (byte)(value>>8);
- data[1] = (byte)(value>>16);
- data[0] = (byte)(value>>24);
- return data;
- }
- private void write(byte[] data) throws IOException{
- mOutput.write(data);
- }
- private void write(String str) throws IOException{
- mOutput.write(str.getBytes());
- }
MP4文件有很多box组成,每个box呢,都有一部分共同的属性,还有一部分自己独特的扩展属性,就跟基类和实现类的关系是一样的。
共性部分非常简单
- aligned(8) class Box (unsigned int(32) boxtype,
- optional unsigned int(8)[16] extended_type) {
- unsigned int(32) size;
- unsigned int(32) type = boxtype;
- if (size==1) {
- unsigned int(64) largesize;
- } else if (size==0) {
- // box extends to end of file
- }
- if (boxtype==‘uuid’) {
- unsigned int(8)[16] usertype = extended_type;
- }
- }
核心的就2个:4个字节的长度(长度为0和1时候有特殊意义),4个字节的type
ftyp box继承于box,在ISO规范中是这样定义的
- aligned(8) class FileTypeBox
- extends Box(‘ftyp’) {
- unsigned int(32) major_brand; //ex:isom
- unsigned int(32) minor_version;//ex:512
- unsigned int(32) compatible_brands[]; // to end of the box ex:mp41
- }
多个3个信息:主版本信息,次版本信息 ,兼容信息