Sunday, April 10, 2016

MP3 metadata is not getting updated

Leave a Comment

I want to add album name to mp3 files. I am using Jaudiotagger library for reading and writing mp3 metadata. In my mp3 file there is not media metadata except length. I have attached a screenshot of windows file properties of 1 such mp3 file. enter image description here

Given a directory the code goes through all mp3 files inside it and assigns album name to each mp3 file. The code executes without any errors but when I try to open the properties of that mp3 file. Still it does not have any album information. Please help me find out what am I missing.

Below is my code

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

Output

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 

Tracing audioFile.commit()

on debugging audioFile.commit(); on line number 103 it does the following code

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     } } 

and it comes out properly without any errors.

Note: using jaudiotagger-2.2.4

1 Answers

Answers 1

The following code reads an existing ALBUM tag from an MP3 file, and then overwrites it with a new ALBUM tag.

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

It has been tested to work with java 1.7 and jaudiotagger 2.2.3 on Linux.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment