// --------------------------------------------------------------------------- private int GetID3v2Size(BinaryReader Source) { ID3v2Header Header = new ID3v2Header(); // Get ID3v2 tag size (if exists) int result = 0; Source.BaseStream.Seek(0, SeekOrigin.Begin); Header.ID = Source.ReadChars(3); Header.Version = Source.ReadByte(); Header.Revision = Source.ReadByte(); Header.Flags = Source.ReadByte(); Header.Size = Source.ReadBytes(4); if (Utils.StringEqualsArr("ID3", Header.ID)) { result = Header.Size[0] * 0x200000 + Header.Size[1] * 0x4000 + Header.Size[2] * 0x80 + Header.Size[3] + 10; if (0x10 == (Header.Flags & 0x10)) { result += 10; } if (result > Source.BaseStream.Length) { result = 0; } } return(result); }
/// <summary>Creates the specified header.</summary> /// <param name="header">The header.</param> /// <param name="flags">The flags.</param> /// <param name="description">The description.</param> /// <param name="type">The type.</param> /// <param name="mimeType">Type of the MIME.</param> /// <param name="imageData">The image data.</param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> public static ID3v2APICFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string description, ID3v2PictureType type, string mimeType, byte[] imageData) { ID3v2EncodingType encoding = ID3v2Encoding.Select(header, description + mimeType); // header, encoding[1], mimeType+0, pitureType[1], description+0, data byte[] descriptionBytes = ID3v2Encoding.GetBytes(encoding, description, true); byte[] mimeTypeBytes = ID3v2Encoding.GetBytes(encoding, mimeType, true); int contentSize = descriptionBytes.Length + mimeTypeBytes.Length + 1 + 1 + imageData.Length; var frameHeader = ID3v2FrameHeader.Create(header, "APIC", flags, contentSize); using (var ms = new MemoryStream()) { var writer = new DataWriter(ms); writer.Write(frameHeader.Data); writer.Write((byte)encoding); writer.Write(mimeTypeBytes); writer.Write((byte)type); writer.Write(descriptionBytes); writer.Write(imageData); if (frameHeader.HeaderSize + contentSize != ms.Position) { throw new Exception(); } return(new ID3v2APICFrame(new ID3v2Frame(header, ms.ToArray()))); } }
/// <summary> /// Gets the ID3v2 tag size from a specified stream. Returns 0 if no tag exists. /// </summary> /// <param name="stream">The stream.</param> public static int GetTagSize(Stream stream) { try { if (stream.Length >= 16) { stream.Position = 0; byte[] identifier = stream.Read(3); // Identifier 'ID3' if (!(identifier[0] == 0x49 && identifier[1] == 0x44 && identifier[2] == 0x33)) { return(0); } ID3v2Header header = new ID3v2Header(stream); int tagSize = header.TagSize; if (tagSize != 0) { return(tagSize + 10 + (header.IsFooterPresent ? 10 : 0)); } else { return(0); } } return(0); } finally { stream.Position = 0; } }
/// <summary> /// Creates a new header. /// </summary> /// <param name="header">The tag header.</param> /// <param name="flags">The flags.</param> /// <param name="description">The description.</param> /// <param name="type">The type.</param> /// <param name="image">The image.</param> /// <param name="imageFormat">The image format.</param> /// <param name="quality">The quality.</param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static ID3v2APICFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string description, ID3v2PictureType type, SkiaSharp.SKImage image, SkiaSharp.SKEncodedImageFormat imageFormat = SkiaSharp.SKEncodedImageFormat.Jpeg, int quality = 99) { var data = image.Encode(imageFormat, quality); string mimeType; switch (imageFormat) { case SkiaSharp.SKEncodedImageFormat.Jpeg: mimeType = MimeTypes.FromExtension(".jpg"); break; case SkiaSharp.SKEncodedImageFormat.Png: mimeType = MimeTypes.FromExtension(".png"); break; default: throw new ArgumentOutOfRangeException(string.Format("ImageFormat {0} not suppoerted!", imageFormat)); } return(Create(header, flags, description, type, mimeType, data.ToArray())); }
/// <summary>Creates a new ID3v2TextFrame.</summary> /// <param name="header">The header.</param> /// <param name="flags">The flags.</param> /// <param name="id">The identifier.</param> /// <param name="text">The text.</param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> public static ID3v2TextFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string id, string text) { var encoding = ID3v2Encoding.Select(header, text); // header, encoding[1], name+0 var textBytes = ID3v2Encoding.GetBytes(encoding, text, true); var contentSize = 1 + textBytes.Length; var frameHeader = ID3v2FrameHeader.Create(header, id, flags, contentSize); using var ms = new MemoryStream(); var writer = new DataWriter(ms); writer.Write(frameHeader.Data); writer.Write((byte)encoding); writer.Write(textBytes); return(new ID3v2TextFrame(new ID3v2Frame(header, ms.ToArray()))); }
/// <summary>Creates a new header.</summary> /// <param name="header">The tag header.</param> /// <param name="flags">The flags.</param> /// <param name="data">The data.</param> /// <returns></returns> /// <exception cref="NotSupportedException">Unsupported Header Version.</exception> public static ID3v2XSLTFrame Create(ID3v2Header header, ID3v2FrameFlags flags, byte[] data) { switch (header.Version) { case 3: case 4: break; default: throw new NotSupportedException("Unsupported Header Version"); } var frameHeader = ID3v2FrameHeader.Create(header, "XSLT", flags, data.Length); using var ms = new MemoryStream(); var writer = new DataWriter(ms); writer.Write(frameHeader.Data); writer.Write(data); return(new ID3v2XSLTFrame(new ID3v2Frame(header, ms.ToArray()))); }
/// <summary>Creates a new header.</summary> /// <param name="header">The tag header.</param> /// <param name="flags">The flags.</param> /// <param name="name">The name.</param> /// <param name="value">The value.</param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> public static ID3v2TXXXFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string name, string value) { var encoding = ID3v2Encoding.Select(header, name + value); // header, encoding[1], name+0, value+0 var nameBytes = ID3v2Encoding.GetBytes(encoding, name, true); var valueBytes = ID3v2Encoding.GetBytes(encoding, value, true); var contentSize = nameBytes.Length + valueBytes.Length + 1; var frameHeader = ID3v2FrameHeader.Create(header, "TXXX", flags, contentSize); using var ms = new MemoryStream(); var writer = new DataWriter(ms); writer.Write(frameHeader.Data); writer.Write((byte)encoding); writer.Write(nameBytes); writer.Write(valueBytes); return(new ID3v2TXXXFrame(new ID3v2Frame(header, ms.ToArray()))); }
// --------------------------------------------------------------------------- private int GetID3v2Size(BinaryReader Source) { ID3v2Header Header = new ID3v2Header(); // Get ID3v2 tag size (if exists) int result = 0; Source.BaseStream.Seek(0, SeekOrigin.Begin); Header.ID = Source.ReadChars(3); Header.Version = Source.ReadByte(); Header.Revision = Source.ReadByte(); Header.Flags = Source.ReadByte(); Header.Size = Source.ReadBytes(4); if ( Utils.StringEqualsArr("ID3",Header.ID) ) { result = Header.Size[0] * 0x200000 + Header.Size[1] * 0x4000 + Header.Size[2] * 0x80 + Header.Size[3] + 10; if (0x10 == (Header.Flags & 0x10)) result += 10; if (result > Source.BaseStream.Length) result = 0; } return result; }