public static async Task <ParseResult <Mp3File> > ParseSamplesAsync(IBinaryStream stream) { var streamLength = await stream.GetLengthAsync().ConfigureAwait(false); stream.Position = 0; if (await Id3v2.IsPointingToHeaderAsync(stream).ConfigureAwait(false)) { await Id3v2.SkipAsync(stream).ConfigureAwait(false); } var collectSamplesResult = await CollectSamplesAsync(stream, streamLength, null).ConfigureAwait(false); if (collectSamplesResult.Item1.Count == 0) { return(ParseResult <Mp3File> .CreateUnknownFormat()); } var file = new Mp3File(); file.Type = collectSamplesResult.Item2; file.Samples = collectSamplesResult.Item1; return(ParseResult <Mp3File> .Create(file)); }
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)); } }