mpatric / mp3agic Goto Github PK
View Code? Open in Web Editor NEWA java library for reading mp3 files and reading / manipulating the ID3 tags (ID3v1 and ID3v2.2 through ID3v2.4).
License: MIT License
A java library for reading mp3 files and reading / manipulating the ID3 tags (ID3v1 and ID3v2.2 through ID3v2.4).
License: MIT License
As Java SDK suggests, you should override hashCode() too when you override equals(). This speeds up your code a bit, and most importantly, your code will not produce compiler warnings.
Hi, I started editing the mp3 tags using mp3agic and it worked well.
But I am a little dissapointed that when the metadata is to be saved it has to go to a new file, not the same file.
I know that to save the edited tags in the same file is possible as VLC, Winamp and other programs do it. And I want the program I am doing to do it as well.
Is that possible using mp3agic? if not, can you suggest me another library that is able to do it?
I hope and thank your answers.
this is the stack trace
0 java.lang.OutOfMemoryError
1 at com.mpatric.mp3agic.BufferTools.copyBuffer(Unknown Source)
2 at com.mpatric.mp3agic.ID3v2Frame.unpackFrame(Unknown Source)
3 at com.mpatric.mp3agic.ID3v2Frame.(Unknown Source)
4 at com.mpatric.mp3agic.ID3v24Frame.(Unknown Source)
5 at com.mpatric.mp3agic.ID3v24Tag.createFrame(Unknown Source)
6 at com.mpatric.mp3agic.AbstractID3v2Tag.unpackFrames(Unknown Source)
7 at com.mpatric.mp3agic.AbstractID3v2Tag.unpackTag(Unknown Source)
8 at com.mpatric.mp3agic.AbstractID3v2Tag.(Unknown Source)
9 at com.mpatric.mp3agic.AbstractID3v2Tag.(Unknown Source)
10 at com.mpatric.mp3agic.ID3v24Tag.(Unknown Source)
11 at com.mpatric.mp3agic.ID3v2TagFactory.createTag(Unknown Source)
12 at com.mpatric.mp3agic.Mp3File.initId3v2Tag(Unknown Source)
13 at com.mpatric.mp3agic.Mp3File.init(Unknown Source)
14 at com.mpatric.mp3agic.Mp3File.(Unknown Source)
15 at com.mpatric.mp3agic.Mp3File.(Unknown Source)
I'm not sure if a disc number tag really exists (couldn't find anything on http://www.id3.org/ ) but other libs support it.
Maybe you could clarify this? Thanks.
When the string sent to any of the "setXXXX" methods of ID3v2Tag contains a UTF-8 character outside of the ASCII range, I am getting a NullPointerException down in one of the frame data classes.
Here is a sample unit test that demonstrates the problem:
@Test
public void testUTF8AlbumData() throws Exception {
// Set a string with a UTF-8 apostrophe in it,
// http://www.fileformat.info/info/unicode/char/2019/index.htm
String title = "The Album’s Name";
Mp3File mp3File = new Mp3File("///path/to/some/file.mp3");
ID3Wrapper tag = new ID3Wrapper(mp3File.hasId3v1Tag() ? mp3File.getId3v1Tag() : new ID3v1Tag(),
mp3File.hasId3v2Tag() ? mp3File.getId3v2Tag() : new ID3v24Tag());
// This generates a NullPointerException down in frame data
tag.setAlbum(title);
}
The stack trace that this test generates looks like this:
java.lang.NullPointerException
at com.mpatric.mp3agic.ID3v2TextFrameData.getLength(ID3v2TextFrameData.java:37)
at com.mpatric.mp3agic.ID3v2TextFrameData.packFrameData(ID3v2TextFrameData.java:26)
at com.mpatric.mp3agic.AbstractID3v2FrameData.packAndUnsynchroniseFrameData(AbstractID3v2FrameData.java:21)
at com.mpatric.mp3agic.AbstractID3v2FrameData.toBytes(AbstractID3v2FrameData.java:29)
at com.mpatric.mp3agic.AbstractID3v2Tag.setAlbum(AbstractID3v2Tag.java:389)
at com.mpatric.mp3agic.ID3Wrapper.setAlbum(ID3Wrapper.java:90)
From my reading of the code, it looks like AbstractID3v2Tag is hard-coding the text encoding as ISO-8859-1 when it creates an EncodedText instance in each of its setters.
Useful in stream-only environments
Hi
I am a newbie in Android development, and i am trying to use this library in my project, but i actually dont know about the junit, maven or ant procedures. Is there a library version of mp3agic as a jar so i can add it to my project?
Thanks
Hi,
I'm trying to use mp3agic api in a netbeans project. I have no compilation errors though when I try to run the example code, I am getting an error when trying to do a new Mp3File. In debug mode, I get up to (but not into)
id3v2Tag = ID3v2TagFactory.createTag(bytes);
But, then a NoSuchTagException exception is raised that apparently is not found, though the class does exist in the package com.mpatric.mp3agic!
Could you please help me? I am new to Java but I have coding experience ...
Here is the error message I get:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class NoSuchTagException
location: class com.mpatric.mp3agic.ID3v23Tag
at com.mpatric.mp3agic.ID3v23Tag.(ID3v23Tag.java:12)
at com.mpatric.mp3agic.ID3v2TagFactory.createTag(ID3v2TagFactory.java:10)
at com.mpatric.mp3agic.Mp3File.initId3v2Tag(Mp3File.java:256)
at com.mpatric.mp3agic.Mp3File.init(Mp3File.java:68)
at com.mpatric.mp3agic.Mp3File.(Mp3File.java:57)
at com.mpatric.mp3agic.Mp3File.(Mp3File.java:41)
at musicchecker.Main.main(Main.java:60)
I am in the process of testing an Mp3 metadata extraction application for audiobook files based upon the mp3agic library. When I did my unit testing, I used an input mp3 file that had never been edited using the Mp3tag tag editor (http://www.mp3tag.de/en/). The application met all unit test criteria and I moved on to complete my testing.
Unfortunately, when I did a volume test, most of my audiobook Mp3 files had been edited using the Mp3tag tag editor. I had used the editor to change the genre and in many cases to add cover art to the Mp3 files. When processing these edited Mp3 files, my application is not finding the metadata from the ID3v2.3 tag. I am declaring my variable using the ID3v2 interface. I am calling the methods in that interface. I have confirmed that I am working with an instance of the class ID3v23Tag. Yet, most of the getters() are returning null. This includes artist, title, album, year, comment, publisher, original artist, copyright, URL, and encoder. The getters() for track, genre, and Mime type are returning values. My only thought is that the mp3agic library is sensitive to some change that is being made to the file by the Mp3tag editor.
When I re-edit the problem files using the Mp3tag editor, that application reports that there is an ID3v2.3 tag. It also finds all of the metadata from that tag. So, I know that the metadata is not lost from the file. It just seems to not be findable by the mp3agic library. Also, other user agents like Itunes and Windows Media Player seem to find this ID3v2.3 metadata just fine too. So, whatever format the Mp3tag editor is writing these files in cannot be that non-compliant.
Do you have any ideas about solving this issue?
Thanks for your help.
Kevin
How could i get disk no data from mp3, is it not in id3 specification?
A mp3 i am trying to write id3v2 tags to has no id3v2 tags , but i haven't been able to find a way to set just some blank id3v2 tags on the file then work of that. I tried setting them to null, but that did not work which is to be suspected. Sorry if i am some obvious work around.
I grabbed the code because I was looking for some java code that would completely remove the ID3 tag. The readme indicate that this is supported but I don't see a method for it. How does one go about removing the tag. I expected to be able to do something like:
Mp3File mp3in = new Mp3File("myMP3File");
if(mp3in.hasId3v1Tag())
mp3in.getId3v1Tag().remove();
How do I completely remove a tag?
Thanks
Travis builds seem to be intermittently failing on the Mp3RetagTest. It's not always the same test that fails either. Need to sort this out.
I write a code to save album image into an mp3 file. but seems unable to save image tag.
code:
byte[] content = readImageContentFromFile("cover.jpg");
Mp3File mp3file = new Mp3File("xxx.mp3");
ID3v2 id3v2Tag;
if (mp3file.hasId3v2Tag()) {
id3v2Tag = mp3file.getId3v2Tag();
} else {
// mp3 does not have an ID3v2 tag, create one
id3v2Tag = new ID3v24Tag();
mp3file.setId3v2Tag(id3v2Tag);
}
id3v2Tag.setAlbumImage(content, "image/jpg");
mp3file.save("xxx1.mp3");
Now that mp3agic is synced to Maven central, this should be mentioned in the project description, e.g.:
Releases are available via Maven central. To add a dependency to mp3agic
, use
<dependency>
<groupId>com.mpatric</groupId>
<artifactId>mp3agic</artifactId>
<version>0.8.x</version>
</dependency>
This library would be awesome if it implemented the lyric section of the ID3 tag. I have a question over at Stack Overflow where I have tried to implement this, but the lyrics would not stick in the tag after saving the MP3. I added the code below to AbstractID3v3Tag.java. If there is anything I can do to help, let me know.
http://stackoverflow.com/questions/11924922/set-lyrics-in-mp3-id3v2-tag
Added Code to: AbstractID3v2Tag.java
//define lyric tag for id3v2
public static final String ID_TEXT_LYRICS = "USLT";
//get the lyrics from the tag
public String getLyrics() {
ID3v2TextFrameData frameData;
if (obseleteFormat) return null;
else frameData = extractTextFrameData(ID_TEXT_LYRICS);
if (frameData != null && frameData.getText() != null)
return frameData.getText().toString();
return null;
}
//set the lyrics in the tag
public void setLyrics(String lyrics) {
if (lyrics != null && lyrics.length() > 0) {
invalidateDataLength();
ID3v2TextFrameData frameData = new ID3v2TextFrameData(useFrameUnsynchronisation(), new EncodedText(lyrics));
addFrame(createFrame(ID_TEXT_LYRICS, frameData.toBytes()), true);
}
}
As java 8 is out for a while now and java 6 didn't see any updates since Feb 2013 (http://www.oracle.com/technetwork/java/javase/eol-135779.html#Java6-end-public-updates) I wonder if we could switch to distributing java 7 jars of mp3agic via mvn central.
Any vetos?
There isn't currently an easy way to remove album art. I'm proposing adding a clearAlbumImage method to the ID3v2 interface and an implementation in AbstractID3v2Tag.
Hello, its possible to use mp3agic in an android project?
Thank you
I really appreciate how easy you've made the overall process of dealing with tags.
However, if I have done things correctly, reading a tag is extremely slow.
Reading tags on 400 tracks takes an average of 600ms per track (Android 4.4, Galaxy S4)...
for (Track track : tracklist){
mp3file = new Mp3File(track.getPath());
if (mp3file != null && mp3file.hasId3v2Tag()) {
mp3file.getId3v2Tag();
}
}
Any idea to speed up the process ?
thanks
Hi, is there any way to know which encoder has been used to encode the MP3 File?
Something like a method mp3File.getEncoder() which returns 'Lame 3.99', 'Lame 3.95', 'Xing' etc?
I've seen this information is inside the file (and is shown for example in EncSpot), but i really have no idea on how to get it.
Anyway, is a great library.
Giorgio
Hi guys,
I know this might seem like a dumb question, but I'm in the middle of writing a Java Swing GUI application that functions like MP3Tag (for educational purposes) and I'm currently writing my saveTag method to save the newly written tag data into the imported MP3 file.
This is my saveTag method, commented to get the logic across. (see above)
This (see above) is a picture of the GUI in action, the new tag data is taken from the JComboBox components and is set into the tag data, and all of this works. However, when the code gets to the Mp3File.save(); function, it doesn't seem to create a new file, and it continues to delete the original just fine. Any help with understanding how to use the save function would be very much appreciated. Thank you!!
I am making changes to the tags like the example in the docs "Setting ID3v2 field values"
but I when I am trying to save the file by the same name I am getting an exception (on android)
do I have to save the changes on a new file could I just apply them without creating a new file ?
I just found out, that German umlauts in the path when instantiating the Mp3File are not supported. (ö, ä, ü, Ö, Ä, Ü, ß)
As mp3agic is a maven project since quite a while it seems reasonable to remove the ant files.
This issue only exists as kind of a warning and to ask if anyone still uses ant for a good reason.
Ant will removed in >2 weeks if no good reasons occur.
Hello,
Does this library support only Unicode encoding in tags? If no, how could I specify the encoding for reading?
Regards,
Igor
Encoding issue, only happens on Windows. Problem seems to be with the two failing tests, not the code.
[junit] null expected:<T[]G> but was:<T[è]G>
[junit] junit.framework.ComparisonFailure: null expected:<T[]G> but was:<T[è]G>
[junit] junit.framework.ComparisonFailure: null expected:<T[]G> but was:<T[è]G>
[junit] at com.mpatric.mp3agic.BufferToolsTest.testShouldConvertBufferContainingHighAscii
Handling of Unicode text blocks could be better. Also, support for Byte Order Mark (BOM) on Unicode text blocks should be added.
I created a simple mp3 Arranger using mp3agic and its working Fine On Windows but when i try to Run this On Ubuntu its not Reading Tags at all...
Have look on Source of Mp3 Arranger
https://github.com/iamaamir/Mp3-Arranger
The library seems very nice, but how do you expect people to know how to use it if there is no documentation? I cannot tell if I can use the library to extract PCM or raw audio data from mp3s, even though the description implies that it can.
This issue exists to make it more transparent that all unit tests are planned to get rewritten as spock [0] tests.
The long term goal is to increase the unit test coverage and substitute the JUnit 3 tests with more readable spock tests.
You may also raise your voice here if you have good reasons to criticise this decision.
If you like it or simply want to play with spock tests, patches are welcome in the spock_unit_testing branch [1].
0: http://code.google.com/p/spock/
1: https://github.com/mpatric/mp3agic/tree/spock_unit_testing
I need this library :D but i cant build this with ant, any step tutorial to do it in Eclipse ?
Hi,
I am missing Support for User defined text information frames in mp3agic.
I am using Amarok and it stores FMPS_Playcount, FMPS_Rating and some strange FMPS_Rating_Amarok_Score in my Files.
I would like to access this information in my Java Program and have already managed to get this Information in mp3agic but there is no way how this could should fit into the Structure of mp3agic.
In the Frameset you already store the different entries with the Same ID 'TXXX' but the Data cannot be accessed.
First I thought, that when unpacking the Frame, that its ID should be set to 'TXXX:whatever'. But the Problem is, that 'whatever', the Description of the User Defined Information is in the Same encoding as the Value and therefor should not be mixed with the ID.
Have you ever thought of how this should be implemented. I would work it out according to what we agree.
My experience in interpreting those byte[] according to the Specs of http://id3.org/id3v2.3.0#User_defined_text_information_frame is very low and I am sure that I will need your support for that implementation, but I would be glad If I could contribute to the Project.
mp3agic should be available from mvn central.
File: AbstractID3v2Tag.java
Line: 526
This is the line:
if (description != null & description.length() > 0) {
It should have logical AND instead of bitwise AND:
if (description != null && description.length() > 0) {
As far as I know it is allowed to save a string into id3 tags.
The genre IDs are only there to save some space.
Jaudiotagger ( http://www.jthink.net/jaudiotagger/ ) e.g. does support saving free text into the genre field.
I am very new with Java programming so thanks in advance for humoring me.
Is there any reason not to have an Mp3File constructor that accepts a File object and/or a getFile() method that returns a File object?
Right now my code looks like this...
Collection<File> files = FileUtils.listFiles(directory, extensions, true);
for (File file:files) {
Mp3File mp3file = new Mp3File(file.getAbsolutePath());
// do stuff
System.out.println("Filename: " + file.getName());
}
I would like to be able to do this...
Collection<File> files = FileUtils.listFiles(directory, extensions, true);
Mp3File[] mp3s = new Mp3File[files.length]
for (int i=0; i<mp3s.length; i++) {
mp3s[i] = new Mp3File(files[i]);
}
for (mp3:mp3s) {
// do stuff
System.out.println("Filename: " + mp3.getFile().getName());
}
setGenreDescription() should be implemented also for IDV3_1, it's only implemented for the IDV3_2
We should add a method like ID3v2.getMinorVersion()
to return an integer indicating the ID3 v2 minor tag version number (e.g. 3 for ID3 v2.3).
This makes sense because some ID3v2
API methods may have different behavior, depending on the tag version. For example, the user may provide a genre as free-text to a v2.4 tag only.
I am trying to read tags of Russian songs in Java using mp3agic:
Mp3File song;
try {
song = new Mp3File(newURI);
if (song.hasId3v2Tag()) {
ID3v2 id3v2tag = song.getId3v2Tag();
title = id3v2tag.getTitle();
artist = id3v2tag.getArtist();
}
else if (song.hasId3v1Tag()){
ID3v1 id3v1tag = song.getId3v1Tag();
title = id3v1tag.getTitle();
artist = id3v1tag.getArtist();
}
}
However I get this "??-2????????? ?????" instead of this "Би-2Скользкие Улицы". What can I do to resolve this issue?
ID3v2.4 text information frames can contain multiple strings, whereas in ID3v2.3 and earlier they can only contain one. From the ID3v2.4 specification:
The text information frames are often the most important frames,
containing information like artist, album and more. There may only be
one text information frame of its kind in an tag. All text
information frames supports multiple strings, stored as a null
separated list, where null is reperesented by the termination code
for the charater encoding. All text frame identifiers begin with "T".
Only text frame identifiers begin with "T", with the exception of the
"TXXX" frame. All the text information frames have the following
format:
<Header for 'Text information frame', ID: "T000" - "TZZZ",
excluding "TXXX" described in 4.2.6.>
Text encoding $xx
Information <text string(s) according to encoding>
Mp3agic will currently only return the first value when reading multi-values text information frames and will only allow you to set one value.
Hey,
Thanks for the library, it is coming out to be really helpful for my project. I just had one question, is there a way to set multiple artists for the same mp3 file. In the documentation, it just shows one artist included in the id3v2 artist tag.
Cheers,
Yogesh
No support yet for tag synchronisation of whole id3v2 tags with unsynchronisation. It's extremely rare to find applications that do this anyway.
The POM lists a dependency to plugin maven-download-plugin. This makes mp3agic depending on "half of the internet". The plugin is available at central, so you may just remove it from the dependencies section.
How about releasing mp3agic 0.8.2?
Since version 0.8.1 half a year ago, there have been made some improvements and fixes that should be worth bumping a new release.
If you would add a jar including the lib and one including the source for each release as a download here at github people could use these downloads as dependencies in maven projects.
when using this method returns null for me all the songs that I try even knowing that they have cover
public Musica getMusica(String path) {
Musica musica = new Musica();
// caminho
musica.setCaminho(path);
Mp3File mp3file;
try {
mp3file = new Mp3File(path);
// duração
musica.setDuracao((int) mp3file.getLengthInSeconds());
if (mp3file.hasId3v1Tag()) {
//load the data in the object Musica
} else if (mp3file.hasId3v2Tag()) {
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
//load the data in the object Musica
byte[] imageData = id3v2Tag.getAlbumImage();
if (imageData != null) {
Image img = ImageIO
.read(new ByteArrayInputStream(imageData));
musica.setImage(img);
}
} else throw new UnsupportedTagException();
} catch (UnsupportedTagException e) {
musica.setArtista("Desconhecido");
musica.setAlbum("Desconhecido");
musica.setTitulo(path.substring(path.lastIndexOf("\\") + 1,
path.lastIndexOf(".") - 1));
musica.setGenero("Desconhecido");
} catch (InvalidDataException | IOException e) {
System.out.println("kk");
return null;
}
return musica;
}
If you edit tags and save the file without modifying the file name, mp3agic throws an IllegalArgumentException ("Save filename same as source filename")
This is a very common task and should be possible.
Hello, i'm trying to use mp3agic to change the title property of my mp3 files, but the generated mp3 files are the same as the original files. The title property has not changed. I'm basically using the example in "Setting ID3v2 field values" section.
Here's my code:
...
File folderPath = new File(JOptionPane.showInputDialog("Folder Path:"));
File changedFilesFolder = new File(folderPath.getAbsolutePath()+File.separator+"ChangedFiles");
changedFilesFolder.mkdirs();
String fileNames[] = folderPath.list();
for(String fileName : fileNames)
{
if(fileName.endsWith(".mp3"))
{
Mp3File mp3file = new Mp3File(folderPath.getAbsolutePath()+File.separator+fileName);
ID3v1 id3v1Tag;
if (mp3file.hasId3v1Tag()) {
id3v1Tag = mp3file.getId3v1Tag();
} else {
// mp3 does not have an ID3v1 tag, let's create one..
id3v1Tag = new ID3v1Tag();
mp3file.setId3v1Tag(id3v1Tag);
}
id3v1Tag.setTitle(fileName);
mp3file.save(changedFilesFolder.getAbsolutePath()+File.separator+fileName);
}
}
...
What am I doing wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.