【问题标题】:adding custom tag using tagLib sharp library使用 tagLib 锐利库添加自定义标签
【发布时间】:2016-04-03 03:45:16
【问题描述】:

是否可以使用TagLib# libary 将自定义标签(比如“SongKey: Em”)添加到 mp3 文件?

【问题讨论】:

    标签: c# mp3 taglib-sharp


    【解决方案1】:

    您可以通过在自定义(私有)帧中写入数据来将自定义标签添加到 MP3。

    但首先:

    如果您使用的是 ID3v1,则必须切换到 ID3v2。任何版本的 ID3v2 都可以,但与大多数事物兼容的版本是 ID3v2.3

    需要的 using 指令:

    using System.Text;
    using TagLib;
    using TagLib.Id3v2;
    

    创建私有框架:

    File f = File.Create("<YourMP3.mp3>"); // Remember to change this...
    TagLib.Id3v2.Tag t = (TagLib.Id3v2.Tag)f.GetTag(TagTypes.Id3v2); // You can add a true parameter to the GetTag function if the file doesn't already have a tag.
    PrivateFrame p = PrivateFrame.Get(t, "CustomKey", true);
    p.PrivateData = System.Text.Encoding.Unicode.GetBytes("Sample Value");
    f.Save(); // This is optional.
    

    在上面的代码中:

    • "&lt;YourMP3.mp3&gt;" 更改为 MP3 文件的路径。
    • "CustomKey" 更改为您想要的密钥名称。
    • "Sample Value" 更改为您要存储的任何数据。
    • 如果您有自定义的保存方法,可以省略最后一行。

    读取私有框架:

    File f = File.Create("<YourMP3.mp3>");
    TagLib.Id3v2.Tag t = (TagLib.Id3v2.Tag)f.GetTag(TagTypes.Id3v2);
    PrivateFrame p = PrivateFrame.Get(t, "CustomKey", false); // This is important. Note that the third parameter is false.
    string data = Encoding.Unicode.GetString(p.PrivateData.Data);
    

    在上面的代码中:

    • "&lt;YourMP3.mp3&gt;" 更改为 MP3 文件的路径。
    • "CustomKey" 更改为您希望密钥的名称。

    读写的区别在于PrivateFrame.Get()函数的第三个布尔参数。阅读时传递false,写作时传递true

    其他信息:

    由于byte[] 可以写入帧,不仅文本,而且几乎任何对象类型都可以保存在标签中,只要您正确转换(并在读取时转换回)数据。

    要将任何对象转换为byte[],请参阅this answer,它使用Binary Formatter 来执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多