我想将相册名称添加到mp3文件中。我正在使用Jaudiotagger库来读取和编写mp3元数据。在我的mp3文件中除了长度之外没有媒体元数据。我附上了1个这样的mp3文件的windows文件属性的截图。
给定一个目录,代码遍历其中的所有mp3文件,并为每个mp3文件分配专辑名称。代码执行时没有任何错误,但是当我尝试打开该mp3文件的属性时。它仍然没有任何专辑信息。请帮我看看我错过了什么。
以下是我的代码
public class Test {
public static String albumName = null;
public static String toCamelCase(final String strInput) {
if (strInput==null)
return null;
final String init = strInput.replaceAll("-", " ").replaceAll("_", " ");
final StringBuilder ret = new StringBuilder(init.length());
for (final String word : init.split(" ")) {
if (!word.isEmpty()) {
ret.append(word.substring(0, 1).toUpperCase());
ret.append(word.substring(1).toLowerCase());
}
if (!(ret.length()==init.length()))
ret.append(" ");
}
return ret.toString();
}
private String getFileExtension(String fileName) {
String extension = "";
int i = fileName.lastIndexOf('.');
int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
if (i > p) {
extension = fileName.substring(i+1);
}
return extension;
}
private void traverse(File dir) throws CannotReadException, TagException, ReadOnlyFileException, InvalidAudioFrameException, CannotWriteException {
try {
File[] files = dir.listFiles();
for (File file : files) {
boolean fileAlbumChecked = false;
if (file.isDirectory()) {
if (Test.albumName == null)
Test.albumName = toCamelCase(file.getName());
System.out.println("directory:" + toCamelCase(file.getName()));
file.renameTo(new File(file.getCanonicalPath()+"/"+toCamelCase(file.getName())));
traverse(file);
} else {
if (getFileExtension(file.getName()).equals("mp3")) {
MP3File mp3File = (MP3File)AudioFileIO.read(file);
//MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File.getAudioHeader();
boolean hasV1Tag = mp3File.hasID3v1Tag();
boolean hasV2Tag = mp3File.hasID3v2Tag();
System.out.println(hasV1Tag+", "+hasV2Tag);
Tag tag = mp3File.getTag();
//ID3v1Tag v1Tag = (ID3v1Tag)tag;
AbstractID3v2Tag v2Tag = mp3File.getID3v2Tag();
ID3v24Tag v24Tag = mp3File.getID3v2TagAsv24();
String tempAlbumName = null;
AudioFile audioFile = AudioFileIO.read(file);
Tag tagwa = audioFile.getTag();
System.out.println("("+tagwa+")");
if (hasV1Tag) {
//Tag tag = mp3File.getTag();
//ID3v1Tag v1Tag = (ID3v1Tag)tag;
tempAlbumName = (tag.toString());
if (Test.albumName == null || Test.albumName.equalsIgnoreCase("unknown") || tempAlbumName != null) {
fileAlbumChecked = true;
Test.albumName = tempAlbumName;
tagwa.setField(FieldKey.ALBUM, Test.albumName);
audioFile.commit();
}
} else if (hasV2Tag) {
tempAlbumName = (v2Tag.getFirst(ID3v24Frames.FRAME_ID_ALBUM));
if (Test.albumName == null || Test.albumName.equalsIgnoreCase("unknown") || tempAlbumName != null) {
fileAlbumChecked = true;
Test.albumName = tempAlbumName;
tagwa.setField(FieldKey.ALBUM, Test.albumName);
audioFile.commit();
}
} else {
if (tagwa == null) {
tagwa = audioFile.createDefaultTag();
System.out.println("("+tagwa+")"+", Class: "+tagwa.getClass());
}
tagwa.addField(FieldKey.ALBUM, Test.albumName);
//tagwa.setField(FieldKey.ALBUM, Test.albumName);
audioFile.commit();
}
//AudioFileIO.write(audioFile);
}
System.out.println("file:" + file.getCanonicalPath()+", Album: "+Test.albumName);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String filePath = "C:\\music";
File dir = new File(filePath);
Test test = new Test();
try {
test.traverse(dir);
} catch (CannotReadException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotWriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出
directory:Ddlj
false, false
(null)
(Tag content:
), Class: class org.jaudiotagger.tag.id3.ID3v23Tag
file:C:\music\DDLJ\DDLJ-HO_GAYA_HAI_TUJKO_PYAR_SAJNA.mp3, Album: Ddlj
false, false
(null)
(Tag content:
), Class: class org.jaudiotagger.tag.id3.ID3v23Tag
file:C:\music\DDLJ\DDLJ-TUJHE_DEKHA_TO.mp3, Album: Ddlj
false, false
(null)
(Tag content:
), Class: class org.jaudiotagger.tag.id3.ID3v23Tag
file:C:\music\DDLJ\DDLJ-ZARA_SA_JHOOM_LOON_MAIN.mp3, Album: Ddlj
跟踪audioFile.commit()
在第103行的调试audioFile.commit();
上执行以下代码
if (id3v2tag == null)
{
rfile = new RandomAccessFile(file, "rw");
(new ID3v24Tag()).delete(rfile);
(new ID3v23Tag()).delete(rfile);
(new ID3v22Tag()).delete(rfile);
logger.config("Deleting ID3v2 tag:"+file.getName());
rfile.close(); //<-- comes till here
}
...
rfile = new RandomAccessFile(file, "rw");
...
if (TagOptionSingleton.getInstance().isId3v1Save())
{
logger.config("Processing ID3v1");
if (id3v1tag == null)
{
logger.config("Deleting ID3v1");
(new ID3v1Tag()).delete(rfile); //<-- comes till here too
}
...
}
... //some catch statements
finally
{
if (rfile != null)
{
rfile.close(); //<-- comes till here too
}
}
它出来没有任何错误。
注意:使用jaudiotagger-2.2.4
答案 0 :(得分:0)
以下代码从MP3文件中读取现有的ALBUM标记,然后使用新的ALBUM标记覆盖它。
MP3File audioFile = (MP3File) AudioFileIO.read(new File("test.mp3"));
Tag tag = audioFile.getTag();
if (tag == null) {
tag = new ID3v22Tag();
}
List<TagField> albumFields = tag.getFields(FieldKey.ALBUM);
System.out.println("Existing ALBUM fields: " + albumFields);
tag.deleteField(FieldKey.ALBUM);
tag.addField(FieldKey.ALBUM, "test album 2");
audioFile.setTag(tag);
audioFile.save();
它已经过测试,可以在Linux上使用java 1.7和jaudiotagger 2.2.3。