gpt4 book ai didi

java - jAudiotagger - 如何创建自定义 TXXX 标签

转载 作者:太空宇宙 更新时间:2023-11-04 08:27:45 24 4
gpt4 key购买 nike

我想创建/添加自定义 ID3 标签到 MP3(ID3v2.3 或 ID3v2.4)。有一个用于此目的的 TXXX 标记,但我不知道如何使用 jAudiotagger 库创建它。

最佳答案

我自己刚刚发现,以下代码未经过正确测试/不干净,但完成了任务:

/**
* This will write a custom ID3 tag (TXXX).
* This works only with MP3 files (Flac with ID3-Tag not tested).
* @param description The description of the custom tag i.e. "catalognr"
* There can only be one custom TXXX tag with that description in one MP3 file
* @param text The actual text to be written into the new tag field
* @return True if the tag has been properly written, false otherwise
*/

public boolean setCustomTag(AudioFile audioFile, String description, String text){
FrameBodyTXXX txxxBody = new FrameBodyTXXX();
txxxBody.setDescription(description);
txxxBody.setText(text);

// Get the tag from the audio file
// If there is no ID3Tag create an ID3v2.3 tag
Tag tag = audioFile.getTagOrCreateAndSetDefault();
// If there is only a ID3v1 tag, copy data into new ID3v2.3 tag
if(!(tag instanceof ID3v23Tag || tag instanceof ID3v24Tag)){
Tag newTagV23 = null;
if(tag instanceof ID3v1Tag){
newTagV23 = new ID3v23Tag((ID3v1Tag)audioFile.getTag()); // Copy old tag data
}
if(tag instanceof ID3v22Tag){
newTagV23 = new ID3v23Tag((ID3v11Tag)audioFile.getTag()); // Copy old tag data
}
audioFile.setTag(newTagV23);
}

AbstractID3v2Frame frame = null;
if(tag instanceof ID3v23Tag){
frame = new ID3v23Frame("TXXX");
}
else if(tag instanceof ID3v24Tag){
frame = new ID3v24Frame("TXXX");
}

frame.setBody(txxxBody);

try {
tag.addField(frame);
} catch (FieldDataInvalidException e) {
e.printStackTrace();
return false;
}

try {
audioFile.commit();
} catch (CannotWriteException e) {
e.printStackTrace();
return false;
}
return true;
}

关于java - jAudiotagger - 如何创建自定义 TXXX 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8170142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com