使用tagLib Sharp库添加自定义标签 [英] adding custom tag using tagLib sharp library

查看:252
本文介绍了使用tagLib Sharp库添加自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 TagLib#将自定义标签(例如"SongKey:Em")添加到mp3文件中库?

推荐答案

您可以通过在自定义(专用)帧中写入数据来向MP3添加自定义标签.

You can add custom tags to an MP3 by writing the data in custom (private) frames.

但首先:

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

You must switch to ID3v2 if you are using ID3v1. Any version of ID3v2 will do, but the version compatible with most things is ID3v2.3.

必需的使用指令:

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.

在上面的代码中:

  • "<YourMP3.mp3>"更改为MP3文件的路径.
  • "CustomKey"更改为您希望密钥为的名称.
  • "Sample Value"更改为要存储的任何数据.
  • 如果您有自定义的保存方法,则可以省略最后一行.
  • Change "<YourMP3.mp3>" to the path to your MP3 file.
  • Change "CustomKey" to the name you want the key to be.
  • Change "Sample Value" to whatever data you want to store.
  • You can omit the last line if you have a custom method for saving.

读取私人框架:

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

在上面的代码中:

  • "<YourMP3.mp3>"更改为MP3文件的路径.
  • "CustomKey"更改为您希望密钥为的名称.
  • Change "<YourMP3.mp3>" to the path to your MP3 file.
  • Change "CustomKey" to the name you want the key to be.

读写之间的区别是PrivateFrame.Get()函数的第三个布尔参数.在阅读时,您通过false,在书写时,您通过true.

The difference between reading and writing is the third boolean parameter of the PrivateFrame.Get() function. While reading, you pass false and while writing you pass true.

其他信息:

由于byte[]可以写在框架上,因此只要您正确转换(读取时可以转换回去)数据,标签中不仅可以保存文本,还可以保存几乎任何对象类型.

Since byte[] can be written on to the frames, not only text, but nearly any object type can be saved in the tags, provided you properly convert (and convert back when reading) the data.

有关将任何对象转换为byte[]的信息,请参见此答案使用Binary Formatter来做到这一点.

For converting any object to a byte[], see this answer which uses a Binary Formatter to do so.

这篇关于使用tagLib Sharp库添加自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆