开发者

Having trouble writing ArtWork with Taglib-sharp 2.0.4.0 in .Net

I'm having trouble with writing artwork 开发者_运维百科in an MP3 File. I'm able to read and display all the artwork inside the MP3 file, using Taglib-sharp, but when it comes to insert more than 1 Picture (ex:FrontCover and BackCover) in the MP3 tag, i'm having problems with that.If it's just one Artwork... i can do it Can someone please throw me bone, and show me how to do it?? (vb.net would be great but C# also do the trick).

One more request... and deleting the image inside the mp3 tag?? Can someone please give me an example on how to do it also.

Thanks for your help


I came across the same problem as Bobby Bhamra. I found that iTunes hates UTF-16 and that's what's the problem there.

targetMp3File = TagLib.File.Create(...);

// define picture
TagLib.Id3v2.AttachedPictureFrame pic = new TagLib.Id3v2.AttachedPictureFrame();
pic.TextEncoding = TagLib.StringType.Latin1;
pic.MimeType     = System.Net.Mime.MediaTypeNames.Image.Jpeg;
pic.Type         = TagLib.PictureType.FrontCover;
pic.Data         = TagLib.ByteVector.FromPath(...);

// save picture to file
targetMp3File.Tag.Pictures = new TagLib.IPicture[1] { pic };    
targetMp3File.Save();

So essentially the whole thing is in the pic.TextEncoding line. Additionally i assigned the Mime Type through the .NET constant.

As a result there is no need for TagLib.PictureType.Other or the workaround using the Description. The only drawback on my solution is that it will only work correctly for MP3 files.


How are you inserting and removing images? Can you post some code?

All tags work using the IPicture interface and the Tag.Pictures getter and setter. Modifying the contents of the Tag.Pictures array will have no effect on the file so modifying the existing list involves getting the current value, maniplating it, and then setting it back. Simply setting or clearing the picture is easier.

You can set the file to have a single image with:

IPicture pictures = new IPicture[1];
pictures[0] = new Picture("path/to/picture.jpg");
file.Tag.Pictures = pictures;

You can remove all images from a tag with the following:

file.Tag.Pictures = new IPicture[0];
file.Save();

Manipulation or more complex but follows the same lines of thinking. It would have been better if Tag.Pictures was an IEnumerable instead of an array, but what's done is done.

Here's an example program that sets images from the command line arguments: https://github.com/mono/taglib-sharp/blob/master/examples/SetPictures.cs


I'm using the following taglib-sharp code in my MP3 tagging app. Without setting the 'mime' and 'type' I was unable to get the Artwork displaying in ITunes and subsequently on my Ipod even though Windows/WMP was correctly displaying it.

TagLib.File targetFileMp3Tag = TagLib.File.Create(...);

Picture picture = new Picture();
picture.Type = PictureType.Other;
picture.MimeType = "image/jpeg";
picture.Description = "Cover";        
picture.Data = ByteVector.FromStream(...);

targetFileMp3Tag.Tag.Pictures = new IPicture[1] { picture };
targetFileMp3Tag.save()

hth


I'm using Taglib version 2.1.0.0 and as of yet, there is no documentation for this version. So I had to come here to search through all answer to find something that actually worked with this version and this is what I came up with...

Private Sub SetTags()
    Me.Cursor = Cursors.WaitCursor

    'Set the version and force it.
    TagLib.Id3v2.Tag.DefaultVersion = 3
    TagLib.Id3v2.Tag.ForceDefaultVersion = True

    'Set all standard tags.
    strInput = Trim(txtMP3Input.Text)
    strTitle = Trim(txtTitle.Text)
    strArtist = Trim(txtArtist.Text)
    strAlbumArtist = Trim(txtAlbumArtist.Text)
    strAlbum = Trim(txtAlbum.Text)
    intYear = Convert.ToInt32(Val(cmbDate.SelectedItem))
    strGenre = cmbGenre.SelectedItem
    strComments = Trim(txtComment.Text)
    strArt = Trim(txtAlbumArt.Text)

    'Create a file (mp3)
    Dim fName As TagLib.File = TagLib.File.Create(strInput)

    'Set the Album art.
    Dim pics As Picture = New Picture(strArt)

    'Insert the standard tags.
    fName.Tag.Title = strTitle
    fName.Tag.Performers = New String() {strArtist}
    fName.Tag.AlbumArtists = New String() {strAlbumArtist}
    fName.Tag.Album = strAlbum
    fName.Tag.Year = intYear
    fName.Tag.Genres = New String() {strGenre}
    fName.Tag.Comment = strComments

    'Insert Album art and
    ' save to file and dispose.
    Dim picsFrame As New TagLib.Id3v2.AttachedPictureFrame(pics)
    picsFrame.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg

    'set the type of picture (front cover)
    picsFrame.Type = TagLib.PictureType.FrontCover

    'Id3v2 allows more than one type of image, just one is needed here.
    Dim pictFrames() As TagLib.IPicture = {picsFrame}

    'set the pictures in the tag
    fName.Tag.Pictures = pictFrames

    fName.Save()
    fName.Dispose()

    Me.Cursor = Cursors.Default
    lblSuccess.Text = "Tags Set Successfully."
End Sub

I installed this version using Visual Studio 2012 threw use of the Menu Items Tools->Library package Manager->Package manager Console. At the "PM>" prompt Type 'Install-Package taglib' This will add the Taglib-Sharp package to your application. It is actually a command line utility to the Power Shell. Wait a few seconds and you are ready to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜