如何使用idsharp将id3v标签写入mp3流

本文关键字:mp3 标签 id3v 何使用 idsharp | 更新日期: 2023-09-27 18:30:02

 FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read);
 MemoryStream ms = new MemoryStream();
 fs.CopyTo(ms);
 ms.Seek(0, SeekOrigin.Begin);     
 IdSharp.Tagging.ID3v2.IID3v2 tags = IdSharp.Tagging.ID3v2.ID3v2Helper.CreateID3v2(ms);                       
 // here i am changing year to new value
 tags.Year = "2012";       
 tags.Save("path"); // here it is asking for file path.

我的任务是读取和写入 id3v2 标签到 mp3 流。但是在这种情况下,保存方法是将字符串路径作为参数。有什么办法可以做到这一点吗? 目前我正在使用idsharp dll。

如何使用idsharp将id3v标签写入mp3流

你可以看看 save 方法的实现。

int originalTagSize = IdSharp.Tagging.ID3v2.ID3v2Tag.GetTagSize(filename);
byte[] tagBytes = tags.GetBytes(originalTagSize);
if (tagBytes.Length < originalTagSize)
{
    // Eventually this won't be a problem, but for now we won't worry about shrinking tags
    throw new Exception("GetBytes() returned a size less than the minimum size");
}
else if (tagBytes.Length > originalTagSize)
{
    //In the original implementation is done with:
    //ByteUtils.ReplaceBytes(path, originalTagSize, tagBytes);
    //Maybe you should write a 'ReplaceBytes' method that accepts a stream
}
else
{
    // Write tag of equal length
    ms.Write(tagBytes, 0, tagBytes.Length);
}

我只报告了当前实现的代码,我没有测试或编译。