コード例 #1
0
        public static async Task <MP3File> ReadMP3FileAsync(string fileName)
        {
            using (var fileStream = File.OpenRead(fileName))
            {
                MP3File file = await ReadMP3FileAsync(fileStream).ConfigureAwait(false);

                file.OriginalFile = fileName;
                return(file);
            }
        }
コード例 #2
0
ファイル: MP3File.cs プロジェクト: stebet/Stebet.ID3
        public static async Task<MP3File> ReadMP3FileAsync(Stream stream)
        {
            var file = new MP3File();
            file.HasId3V2 = (stream.ReadByte() == Constants.ID3Header[0] && stream.ReadByte() == Constants.ID3Header[1] && stream.ReadByte() == Constants.ID3Header[2]);
            if (file.HasId3V2)
            {
                // Let's read the tag header.
                file.OriginalTagHeader = await TagHeader.ReadTagHeader(stream).ConfigureAwait(false);

                // Let's read the tag frames into a byte array and wrap it into a memorystream.
                var tags = ArrayPool<byte>.Shared.Rent(file.OriginalTagHeader.TagLength);
                await stream.ReadAsync(tags, 0, file.OriginalTagHeader.TagLength).ConfigureAwait(false);
                using (var memoryStream = new MemoryStream(tags, 0, file.OriginalTagHeader.TagLength))
                {
                    // Let's read the tag.
                    switch (file.OriginalTagHeader.Version)
                    {
                        case TagVersion.V22:
                            break;
                        case TagVersion.V23:
                            file.Tag = await Tag23Factory.FromStream(memoryStream, file.OriginalTagHeader.TagLength).ConfigureAwait(false);
                            break;
                        case TagVersion.V24:
                            //Tag = Tag24Factory.FromBytes(tagBytes);
                            break;
                        case TagVersion.Unknown:
                            break;
                    }
                }
                ArrayPool<byte>.Shared.Return(tags);
            }
            else // No ID3v2 Tag so the Mpeg Frames presumably start at the beginning but let's make sure though
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            // Now let's find the MPEG frames, seek until we find the first 0xFF byte that
            // should be part of the first Mpeg frame.
            while (stream.Position < stream.Length && stream.ReadByte() != 0xFF)
            {
                if (stream.ReadByte() >= 0xE0)
                {
                    stream.Seek(-2, SeekOrigin.Current);
                    break;
                }
            }

            // We should be at the first MPEG frame
            file.MpegStart = (int)stream.Position;

            //Let's see if this file has an ID3v1 tag
            stream.Seek(-128, SeekOrigin.End);
            if (stream.ReadByte() == 0x54 && stream.ReadByte() == 0x41 && stream.ReadByte() == 0x47)
            {
                // yup, the MPEG frames end at File.Length - 128 bytes
                // we've read 3 bytes to see if the ID3v1 tag is present so the MPEG frames ended 3 bytes ago
                file.MpegEnd = (int)stream.Position - 3;
            }
            else
            {
                // nope.. no ID3v1 tag is present so the MPEG frames end at the end of the file
                file.MpegEnd = (int)stream.Length;
            }

            return file;
        }
コード例 #3
0
        public static async Task <MP3File> ReadMP3FileAsync(Stream stream)
        {
            var file = new MP3File();

            file.HasId3V2 = (stream.ReadByte() == Constants.ID3Header[0] && stream.ReadByte() == Constants.ID3Header[1] && stream.ReadByte() == Constants.ID3Header[2]);
            if (file.HasId3V2)
            {
                // Let's read the tag header.
                file.OriginalTagHeader = await TagHeader.ReadTagHeader(stream).ConfigureAwait(false);

                // Let's read the tag frames into a byte array and wrap it into a memorystream.
                var tags = ArrayPool <byte> .Shared.Rent(file.OriginalTagHeader.TagLength);

                await stream.ReadAsync(tags, 0, file.OriginalTagHeader.TagLength).ConfigureAwait(false);

                using (var memoryStream = new MemoryStream(tags, 0, file.OriginalTagHeader.TagLength))
                {
                    // Let's read the tag.
                    switch (file.OriginalTagHeader.Version)
                    {
                    case TagVersion.V22:
                        break;

                    case TagVersion.V23:
                        file.Tag = await Tag23Factory.FromStream(memoryStream, file.OriginalTagHeader.TagLength).ConfigureAwait(false);

                        break;

                    case TagVersion.V24:
                        //Tag = Tag24Factory.FromBytes(tagBytes);
                        break;

                    case TagVersion.Unknown:
                        break;
                    }
                }
                ArrayPool <byte> .Shared.Return(tags);
            }
            else // No ID3v2 Tag so the Mpeg Frames presumably start at the beginning but let's make sure though
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            // Now let's find the MPEG frames, seek until we find the first 0xFF byte that
            // should be part of the first Mpeg frame.
            while (stream.Position < stream.Length && stream.ReadByte() != 0xFF)
            {
                if (stream.ReadByte() >= 0xE0)
                {
                    stream.Seek(-2, SeekOrigin.Current);
                    break;
                }
            }

            // We should be at the first MPEG frame
            file.MpegStart = (int)stream.Position;

            //Let's see if this file has an ID3v1 tag
            stream.Seek(-128, SeekOrigin.End);
            if (stream.ReadByte() == 0x54 && stream.ReadByte() == 0x41 && stream.ReadByte() == 0x47)
            {
                // yup, the MPEG frames end at File.Length - 128 bytes
                // we've read 3 bytes to see if the ID3v1 tag is present so the MPEG frames ended 3 bytes ago
                file.MpegEnd = (int)stream.Position - 3;
            }
            else
            {
                // nope.. no ID3v1 tag is present so the MPEG frames end at the end of the file
                file.MpegEnd = (int)stream.Length;
            }

            return(file);
        }