public static Mp4Box CreateMp4Box(byte[] buffer, int offset) { if ((buffer != null) && (offset + 8 < buffer.Length)) { Mp4Box box = CreateMp4BoxFromType(ReadMp4BoxType(buffer, offset)); if (box != null) { box.Length = ReadMp4BoxLength(buffer, offset); if ((offset + box.Length <= buffer.Length) && (box.Length > 8)) { box.Type = ReadMp4BoxType(buffer, offset); box.SetPath("/" + box.GetBoxType()); box.Data = ReadMp4BoxData(buffer, offset, box.Length); List <Mp4Box> list = box.GetChildren(); if ((list != null) && (list.Count > 0)) { foreach (var b in list) { box.AddMp4Box(b); } } return(box); } } } return(null); }
public static Mp4Box ReadMp4Box(FileStream fs) { Mp4Box box = null; if (fs != null) { byte[] buffer = new byte[4]; if (buffer != null) { if (fs.Read(buffer, 0, 4) == 4) { int mp4BoxLen = 0; mp4BoxLen |= (int)(buffer[0] << 24); mp4BoxLen |= (int)(buffer[1] << 16); mp4BoxLen |= (int)(buffer[2] << 8); mp4BoxLen |= (int)(buffer[3] << 0); if (mp4BoxLen >= 8) { buffer = new byte[mp4BoxLen]; if (buffer != null) { WriteMp4BoxInt32(buffer, 0, mp4BoxLen); if (fs.Read(buffer, 4, mp4BoxLen - 4) == (mp4BoxLen - 4)) { return(CreateMp4Box(buffer, 0)); } } } } } } return(box); }
public void UpdateParentLength(Mp4Box box, int Len) { Mp4Box pbox = box.GetParent(); while (pbox != null) { pbox.SetBoxLength(pbox.GetBoxLength() + Len); pbox = pbox.GetParent(); } }
public static Mp4Box CreateEmptyMp4Box(string Type) { Mp4Box box = new Mp4Box(); if (box != null) { box.Length = 8; if (!string.IsNullOrEmpty(Type) && (Type.Length <= 4)) { box.Type = Type; return(box); } } return(null); }
public bool AddMp4Box(Mp4Box box, bool bAddInData = false) { if (Children == null) { Children = new List <Mp4Box>(); } if (Children != null) { box.SetParent(this); box.SetPath(this.GetPath() + "/" + box.GetBoxType()); Children.Add(box); if (bAddInData == true) { Append(box.Data); } return(true); } return(false); }
public List <Mp4Box> GetChildren() { List <Mp4Box> list = new List <Mp4Box>(); if ((list != null) && (Data != null)) { ChildrenOffset = 0; if (this.GetBoxType() == "stsd") { ChildrenOffset = 8; } else if (this.GetBoxType() == "dref") { ChildrenOffset = 8; } else if (this.GetBoxType() == "encv") { ChildrenOffset = 78; } else if (this.GetBoxType() == "enca") { ChildrenOffset = 28; } int Offset = ChildrenOffset; while (Offset < Data.Length) { Mp4Box box = CreateMp4Box(Data, Offset); if (box != null) { list.Add(box); Offset += box.Length; } else { break; } } } return(list); }
public static bool WriteMp4Box(Mp4Box box, FileStream fs) { bool result = false; if ((box != null) && (fs != null)) { try { byte[] header = new byte[8]; Mp4Box.WriteMp4BoxInt32(header, 0, box.Length); Mp4Box.WriteMp4BoxString(header, 4, box.GetBoxType()); fs.Write(header, 0, 8); fs.Write(box.Data, 0, box.Data.Length); result = true; } catch (Exception ex) { System.Diagnostics.Debug.Write("Exception while writing box in file: " + ex.Message); } } return(result); }
public Mp4Box GetChild(string type) { if (Children != null) { foreach (var box in Children) { if (box.GetBoxType() == type) { return(box); } else { Mp4Box childbox = box.GetChild(type); if (childbox != null) { return(childbox); } } } } return(null); }
public Mp4Box FindChildBox(string BoxType) { if (Children != null) { foreach (var box in Children) { if (box.GetBoxType() == BoxType) { return(box); } else { Mp4Box ChildBox = box.FindChildBox(BoxType); if (ChildBox != null) { return(ChildBox); } } } } return(null); }
public static string GetBoxChildrenString(int level, Mp4Box box) { string result = string.Empty; int locallevel = level + 1; if (box != null) { List <Mp4Box> list = box.GetChildren(); if ((list != null) && (list.Count > 0)) { foreach (var m in list) { string prefix = string.Empty; for (int i = 0; i < locallevel; i++) { prefix += "\t\t"; } result += prefix + m.ToString() + "\r\n"; result += GetBoxChildrenString(locallevel, m); } } } return(result); }
public void SetParent(Mp4Box box) { this.Path = box.GetPath() + "/" + this.GetBoxType(); Parent = box; }