C# TagLib 为 Mp3 设置专辑封面

C# TagLib Set Album Cover for Mp3

我有一个 mp3 文件,我想在其中添加专辑封面。艺术作品已保存到一个临时文件夹中,我已经检查过它并且它在那里并且是一个 jpeg。 这是我给的代码:

        public void AddMp3Tags()
        {
            TagLib.File file = TagLib.File.Create(OutputPath + OutputName + "." + Format);
            SetAlbumArt(Art, file);
            file.Tag.Title = SongTitle;
            file.Tag.Performers = Artists.Split(',');
            file.Tag.Album = Album;           
            file.Tag.Track = (uint)TrackNumber;
            file.Tag.Year = (uint)Convert.ToInt32(Regex.Match(Year, @"(\d)(\d)(\d)(\d)").Value);            
            file.Save();
        }

        public void SetAlbumArt(string url, TagLib.File file)
        {     
            string path = string.Format(@"{0}temp\{1}.jpg", OutputPath, Guid.NewGuid().ToString());
            using (WebClient client = new WebClient())
            {

                client.DownloadFile(new Uri(url), path);
            }


            TagLib.Picture pic = new TagLib.Picture
            {
                Type = TagLib.PictureType.FrontCover,
                Description = "Cover",
                MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg
            };
            MemoryStream ms = new MemoryStream();
            Image image = Image.FromFile(path);
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Position = 0;
            pic.Data = TagLib.ByteVector.FromStream(ms);
            file.Tag.Pictures = new TagLib.IPicture[] { pic };
            file.Save();
            ms.Close();

        }

除了仅显示黑框的艺术作品外,所有标签均已正确设置: Black box cover art in windows media player.

我试了很多东西,我做错了什么?

确保您的文件下载成功,然后试试这个:

public void SetAlbumArt(string url, TagLib.File file)
{     
    string path = string.Format(@"{0}temp\{1}.jpg", OutputPath, Guid.NewGuid().ToString());
    using (WebClient client = new WebClient())
    {

        client.DownloadFile(new Uri(url), path);
    }

    file.Tag.Pictures = new TagLib.IPicture[]
    {
        new TagLib.Picture(new TagLib.ByteVector((byte[])new System.Drawing.ImageConverter().ConvertTo(System.Drawing.Image.FromFile(path), typeof(byte[]))))
        {
            Type = TagLib.PictureType.FrontCover,
            Description = "Cover",
            MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg
        }
    };

    file.Save();
}

所以我做了更多的研究,结果发现默认情况下 WMP 会尝试使用网络服务来获取专辑封面,我在 VLC 中打开这首歌并显示了封面。专辑代码已正确编写,如下所示:Mp3Tag Viewer/Editor

我发现的另一件事是我的标签使用的是 Id3v2.4 和 Id3v1。 WMP 出于某种原因不喜欢这样,所以我强迫 TagLib 使用 Id3v2.3。我还将文本编码更改为 UFT16,因为 UFT8 无法正常工作。专辑封面现在显示在 WMP 和 windows 资源管理器中。

我也找到了不把图片写入磁盘的方法,就是从网页上下载数据,保存到内存中。

这是我的最终代码:

public void AddMp3Tags()
{
    TagLib.Id3v2.Tag.DefaultVersion = 3;
    TagLib.Id3v2.Tag.ForceDefaultVersion = true;
    TagLib.File file = TagLib.File.Create(OutputPath + OutputName + ".mp3");
    SetAlbumArt(Art, file);
    file.Tag.Title = SongTitle;
    file.Tag.Performers = Artists.Split(',');
    file.Tag.Album = Album;           
    file.Tag.Track = (uint)TrackNumber;
    file.Tag.Year = (uint)Convert.ToInt32(Regex.Match(Year, @"(\d)(\d)(\d)(\d)").Value);
    file.RemoveTags(file.TagTypes & ~file.TagTypesOnDisk);
    file.Save();
}

public void SetAlbumArt(string url, TagLib.File file)
{            
    string path = string.Format(@"{0}temp\{1}.jpg", OutputPath, Guid.NewGuid().ToString());
    byte[] imageBytes;
    using (WebClient client = new WebClient())
    {
        imageBytes = client.DownloadData(url);
    }

    TagLib.Id3v2.AttachedPictureFrame cover = new TagLib.Id3v2.AttachedPictureFrame
    {
        Type = TagLib.PictureType.FrontCover,
        Description = "Cover",
        MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg,
        Data = imageBytes,
        TextEncoding = TagLib.StringType.UTF16


    };
    file.Tag.Pictures = new TagLib.IPicture[] { cover };
}

我希望这可以帮助任何和我有同样问题并且不需要像我一样花那么多时间来解决这个问题的人。