2011-08-29 46 views
1

我在將文件寫入MP3文件時遇到問題。 我能夠使用Taglib-sharp讀取和顯示MP3文件內的所有圖片,但是當涉及在MP3標籤中插入多於一張圖片(例如:FrontCover和BackCover)時,我遇到了問題如果它只是一個藝術品...我可以做到 有人可以扔我骨頭,並告訴我如何做到這一點? (vb.net會很棒,但C#也可以做到這一點)。在.Net中使用Taglib-sharp 2.0.4.0編寫ArtWork時遇到問題

還有一個請求...並刪除了mp3標籤內的圖像?有人可以給我一個例子,說明如何做到這一點。

感謝您的幫助

回答

4

如何插入和刪除圖像?你可以發佈一些代碼嗎?

所有標籤都使用IPicture接口和Tag.Pictures獲取器和設置器。修改Tag.Pictures數組的內容對文件沒有任何影響,因此修改現有列表需要獲取當前值,對其進行處理,然後將其設置回來。簡單地設置或清除圖片更容易。

您可以設置文件有一個單一的形象:

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

可以從標籤中刪除所有圖像有以下:

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

操作或更復雜,但是遵循相同的思路。如果Tag.Pictures是IEnumerable而不是數組,那會更好,但是完成了什麼。

下面是一個例子程序,設置命令行參數圖片:https://github.com/mono/taglib-sharp/blob/master/examples/SetPictures.cs

1

我用我的MP3標籤的應用程序將下面的taglib銳利的代碼。如果不設置'mime'和'type',即使Windows/WMP正確顯示它,我也無法獲取iTunes中顯示的圖形,然後顯示Ipod中的圖形。

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() 

心連心

4

我遇到了同樣的問題,因爲鮑比Bhamra。我發現iTunes討厭UTF-16,那就是那裏的問題。

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(); 

因此,基本上整個事情是在pic.TextEncoding行。此外,我通過.NET常量分配了Mime類型。

因此,不需要TagLib.PictureType.Other或使用說明的解決方法。我的解決方案唯一的缺點是它只能用於MP3文件。

1

我使用的是Taglib 2.1.0.0版本,至今還沒有此版本的文檔。 所以我不得不來這裏通過所有答案搜索找到的東西,實際上這個版本的工作 ,這就是我想出了...

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 

我使用Visual Studio 2012扔使用安裝此版本菜單項目 工具 - >庫包管理器 - >包管理器控制檯。在「PM>」提示符處輸入'Install-Package taglib'這會將Taglib-Sharp軟件包添加到您的應用程序中。它實際上是Power Shell的一個命令行實用程序。等幾秒鐘,你就可以走了。

+0

謝謝,這太棒了!我不得不添加'picsFrame.Description ='「',否則藝術品的原始位置存儲在mp3文件中。 –