public static async Task <MetadataBox> CreateAsync(IBinaryStream stream, Box box) { var metadataBox = new MetadataBox(); stream.Position = box.Offset + 8; metadataBox.Version = await stream.ReadByteAsync().ConfigureAwait(false); metadataBox.Flags = await stream.ReadInt32Async(3, ByteOrder.BigEndian).ConfigureAwait(false); var boxes = new List <Box>(); while (stream.Position < box.Offset + box.Length) { var subBox = await Mpeg4File.ReadBoxAsync(stream, box.Offset + box.Length).ConfigureAwait(false); if (subBox != null) { metadataBox.Boxes.Add(subBox); } } return(metadataBox); }
public static async Task <ParseResult <Mpeg4File> > ParseAsync(IBinaryStream stream, ParseOptions options) { try { stream.Position = 4; if (await stream.ReadStringAsync(4) != "ftyp") { return(ParseResult <Mpeg4File> .CreateUnknownFormat()); } var file = new Mpeg4File(); stream.Position = 0; var streamLength = await stream.GetLengthAsync().ConfigureAwait(false); while (stream.Position < streamLength) { file.Boxes.Add(await Mpeg4File.ReadBoxAsync(stream, await stream.GetLengthAsync().ConfigureAwait(false))); } if (options == ParseOptions.Metadata) { var mediaData = file.Contains("mdat") ? file["mdat"] : null; var metadataItems = file.Contains("moov.udta.meta.ilst") ? await MetadataItemsBox.CreateAsync(stream, file["moov.udta.meta.ilst"]).ConfigureAwait(false) : null; var mediaHeader = file.Contains("moov.trak.mdia.mdhd") ? await MediaHeaderBox.CreateAsync(stream, file["moov.trak.mdia.mdhd"]).ConfigureAwait(false) : null; file.Metadata = new Mpeg4Metadata(metadataItems?.Cover, mediaHeader?.Duration, mediaHeader?.TimeScale, mediaData?.Length); } return(ParseResult <Mpeg4File> .Create(file)); } catch (Exception exception) { return(ParseResult <Mpeg4File> .Create(exception)); } }