Embedding images into Mp3's
I'm a huge fan of electronic music and being the type of frood who knows where his towel is at I've got rather a nice selection of Mp3s and a lovely IRiver H340 to play them on. The H340 comes with it own built in firmware which although it's very colourful it's still crap. So some sexy developers have got together and produced Rockbox, a free alternative firmware for certain Mp3 players.
One of the custom themes of RockBox, iCatcher, appears to extract from the Id3v2 tag of the mp3 the embedded image stored as the album art (front cover, back cover, etc). That got me looking into software tools that let you embed images into mp3s and what I found wasn't good enough, so I created my own; it's great being a developer.
First off I had a look for some .NET libraries that let you get or create mp3 Id3v2 tags and low and behold....
TagLib#... http://www.taglib-sharp.com/Main_Page
An excellent API for altering mp3 (and many other formats) tags. So here is some code using this library that allows you to embed an image. This code only works if the image is correctly sized, 75 pixels by 75 pixels is suitable.
'get the mp3 file
Dim mp3 As TagLib.File = TagLib.File.Create("D:\Towers Of Dub.mp3")
'create the picture for the album cover
Dim picture As TagLib.Picture = TagLib.Picture.CreateFromPath("D:\UfOrb.jpg")
'create Id3v2 Picture Frame
Dim albumCoverPictFrame As New TagLib.Id3v2.AttachedPictureFrame(picture)
albumCoverPictFrame.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg
'set the type of picture (front cover)
albumCoverPictFrame.Type = TagLib.PictureType.FrontCover
'Id3v2 allows more than one type of image, just one needed
Dim pictFrames() As TagLib.IPicture = {albumCoverPictFrame}
mp3.Tag.Pictures = pictFrames 'set the pictures in the tag
mp3.Save()Unfortunately!!
Microsofts Media Player only supports Id3 v2.3, which has been superceded, so you have to use the following lines of code to tell the TibLib library to use v2.3, otherwise it uses a default of v2.4.
TagLib.Id3v2.Tag.DefaultVersion = 3
TagLib.Id3v2.Tag.ForceDefaultVersion = True
If you have any need to work with mp3 tags TagLib# is the API to use!