最近在整理自己电脑的音乐文件,希望音乐tag标签信息能使用java程序批量处理,网上找了相关资料,发现有一个老外写的工具类org.jaudiotagger可以满足需要,但在使用过程中发现读取中文变成乱码问题,然后我把这个问题解决了,并整理成工具类,好东西要拿来分享换积分,因为我的积分快用完了,哈哈…
测试文件: Ring1.mp3
先用千千静听打开查看当前tag信息
我的demo主要代码
public class Demo {
private static String testFile = "e:/tmp/audio/Ring1.mp3";
public static void main(String[] args) {
testRead(); //先读取查看
testWrite(); //修改
testRead(); //修改后再查看
}
public static void testRead(){
AudioModel am = AudioTagUtil.getAudioTag(testFile);
System.out.println(JSON.toJSONString(am));
}
public static void testWrite() {
AudioModel am = new AudioModel(testFile);
//修改歌名
am.setTitle("听了超舒服的短信铃声Abc1234576890");
//修改歌手
am.setAuthor("Ring888999");
AudioTagUtil.setAudioTag(am);
}
}
运行demo结果
{"album":"MyPhone","author":"Ring","comment":"note","fullPathName":"e:/tmp/audio/Ring1.mp3","title":"听了超舒服的短信铃声","trackLength":5,"year":"2021"}
{"album":"MyPhone","author":"Ring888999","comment":"note","fullPathName":"e:/tmp/audio/Ring1.mp3","title":"听了超舒服的短信铃声Abc1234576890","trackLength":5,"year":"2021"}
再用千千静听打开查看当前tag信息
可以看到结果与上面代码显示结果一致
我的工具类AudioTagUtil.java代码(仅供参考)
public class AudioTagUtil {
private final static Logger log = LoggerFactory.getLogger(AudioTagUtil.class);
private static Level defaultLevel=Level.OFF;
/**
* @author Ethan
* @date 2021-09-28
* @param fullPathName 完整路径文件名称,如 /tmp/music1.mp3
* @return AudioModel
* @description 读取音乐文件Tag标签
* 支持:mp3,flac...暂不支持ape
**/
public static AudioModel getAudioTag(String fullPathName) {
configLogLevel(defaultLevel);
AudioModel audioModel=null;
try {
File file = new File(fullPathName);
AudioFile af = AudioFileIO.read(file);
Tag tag = af.getTag();
String artist = tag.getFirst(FieldKey.ARTIST);
String title = tag.getFirst(FieldKey.TITLE);
String album = tag.getFirst(FieldKey.ALBUM);
String year = tag.getFirst(FieldKey.YEAR);
String comment = tag.getFirst(FieldKey.COMMENT);
int len = af.getAudioHeader().getTrackLength();
audioModel = new AudioModel(fullPathName);
String temp = artist;
if (StringUtils.isNotBlank(temp)) {
if (CharEncodeUtil.isISO88591(temp)) {
temp = CharEncodeUtil.convert2MyEncode(temp, MyEncodes.ISO88591, MyEncodes.GB2312);
}
audioModel.setAuthor(temp);
}
temp = title;
if (StringUtils.isNotBlank(temp)) {
if (CharEncodeUtil.isISO88591(temp)) {
temp = CharEncodeUtil.convert2MyEncode(temp, MyEncodes.ISO88591, MyEncodes.GB2312);
}
audioModel.setTitle(temp);
}
temp = album;
if (StringUtils.isNotBlank(temp)) {
if (CharEncodeUtil.isISO88591(temp)) {
temp = CharEncodeUtil.convert2MyEncode(temp, MyEncodes.ISO88591, MyEncodes.GB2312);
}
audioModel.setAlbum(temp);
}
temp = comment;
if (StringUtils.isNotBlank(temp)) {
if (CharEncodeUtil.isISO88591(temp)) {
temp = CharEncodeUtil.convert2MyEncode(temp, MyEncodes.ISO88591, MyEncodes.GB2312);
}
audioModel.setComment(temp);
}
if (StringUtils.isNotBlank(year)) {
audioModel.setYear(year);
}
audioModel.setTrackLength(len);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return audioModel;
}
/**
* @author Ethan
* @date 2021-09-29
* @param am
* @description 写入音乐文件tag
**/
public static void setAudioTag(AudioModel am) {
configLogLevel(defaultLevel);
try {
File file = new File(am.getFullPathName());
AudioFile af = AudioFileIO.read(file);
Tag tag = af.getTag();
String artist = am.getAuthor();
String title = am.getTitle();
String album = am.getAlbum();
String year = am.getYear();
String comment = am.getComment();
int isUpdate = 0;
if (StringUtils.isNotBlank(artist)) {
tag.setField(FieldKey.ARTIST, artist);
isUpdate++;
}
if (StringUtils.isNotBlank(title)) {
tag.setField(FieldKey.TITLE, title);
isUpdate++;
}
if (StringUtils.isNotBlank(album)) {
tag.setField(FieldKey.ALBUM, album);
isUpdate++;
}
if (StringUtils.isNotBlank(comment)) {
tag.setField(FieldKey.COMMENT, comment);
isUpdate++;
}
if (StringUtils.isNotBlank(year)) {
tag.setField(FieldKey.YEAR, year);
isUpdate++;
}
if (isUpdate > 0) {
af.setTag(tag);
af.commit();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
public static void configLogLevel(Level level) {
AudioFileIO.logger.setLevel(level);
org.jaudiotagger.tag.id3.ID3v23Tag.logger.setLevel(level);
org.jaudiotagger.tag.id3.framebody.AbstractID3v2FrameBody.logger.setLevel(level);
org.jaudiotagger.tag.id3.AbstractID3v2Tag.logger.setLevel(level);
org.jaudiotagger.tag.datatype.TextEncodedStringSizeTerminated.logger.setLevel(level);
org.jaudiotagger.tag.datatype.NumberFixedLength.logger.setLevel(level);
org.jaudiotagger.audio.mp3.MP3File.logger.setLevel(level);
org.jaudiotagger.audio.AudioFile.logger.setLevel(level);
org.jaudiotagger.tag.vorbiscomment.VorbisCommentReader.logger.setLevel(level);
}
}
Demo下载地址 https://download.csdn.net/download/tianbbs2008/25972899