Example #1
0
        public static async Task <MediaHeaderBox> CreateAsync(IBinaryStream stream, Box box)
        {
            var mdhdBox = new MediaHeaderBox();

            stream.Position = box.Offset + 8;
            mdhdBox.Version = await stream.ReadByteAsync().ConfigureAwait(false);

            mdhdBox.Flags = await stream.ReadInt32Async(3, ByteOrder.BigEndian).ConfigureAwait(false);

            mdhdBox.CreationTime     = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(await stream.ReadInt32Async(ByteOrder.BigEndian).ConfigureAwait(false)).ToLocalTime();
            mdhdBox.ModificationTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(await stream.ReadInt32Async(ByteOrder.BigEndian).ConfigureAwait(false)).ToLocalTime();
            mdhdBox.TimeScale        = await stream.ReadInt32Async(ByteOrder.BigEndian).ConfigureAwait(false);

            mdhdBox.Duration = await stream.ReadInt32Async(ByteOrder.BigEndian).ConfigureAwait(false);

            mdhdBox.Language = await stream.ReadInt16Async(ByteOrder.BigEndian).ConfigureAwait(false);

            mdhdBox.Quality = await stream.ReadInt16Async(ByteOrder.BigEndian).ConfigureAwait(false);

            return(mdhdBox);
        }
Example #2
0
        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));
            }
        }