public void save(String newFilename) throws IOException, NotSupportedException { if (path.toAbsolutePath().compareTo(Paths.get(newFilename).toAbsolutePath()) == 0) { throw new IllegalArgumentException("Save filename same as source filename"); } try (SeekableByteChannel saveFile = Files.newByteChannel(Paths.get(newFilename), EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE))) { if (hasId3v2Tag()) { ByteBuffer byteBuffer = ByteBuffer.wrap(id3v2Tag.toBytes()); byteBuffer.rewind(); saveFile.write(byteBuffer); } saveMpegFrames(saveFile); if (hasCustomTag()) { ByteBuffer byteBuffer = ByteBuffer.wrap(customTag); byteBuffer.rewind(); saveFile.write(byteBuffer); } if (hasId3v1Tag()) { ByteBuffer byteBuffer = ByteBuffer.wrap(id3v1Tag.toBytes()); byteBuffer.rewind(); saveFile.write(byteBuffer); } saveFile.close(); } }
Mp3File song = new Mp3File(filename); if (song.hasId3v2Tag()){ ID3v2 id3v2tag = song.getId3v2Tag(); byte[] imageData = id3v2tag.getAlbumImage(); //converting the bytes to an image BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageData)); }
private int maxEndOffset() { int maxEndOffset = (int) getLength(); if (hasId3v1Tag()) maxEndOffset -= ID3v1Tag.TAG_LENGTH; return maxEndOffset; }
public static void main(String[] args) throws UnsupportedTagException, InvalidDataException, IOException, NotSupportedException { Mp3File mp3file = new Mp3File("src/test/resources/v1andv23tagswithalbumimage.mp3"); System.out.println("Length of this mp3 is: " + mp3file.getLengthInSeconds() + " seconds"); System.out.println("Bitrate: " + mp3file.getBitrate() + " kbps " + (mp3file.isVbr() ? "(VBR)" : "(CBR)")); System.out.println("Sample rate: " + mp3file.getSampleRate() + " Hz"); System.out.println("Has ID3v1 tag?: " + (mp3file.hasId3v1Tag() ? "YES" : "NO")); System.out.println("Has ID3v2 tag?: " + (mp3file.hasId3v2Tag() ? "YES" : "NO")); System.out.println("Has custom tag?: " + (mp3file.hasCustomTag() ? "YES" : "NO")); if (mp3file.hasId3v1Tag()) { ID3v1 id3v1Tag = mp3file.getId3v1Tag(); System.out.println("Track: " + id3v1Tag.getTrack()); System.out.println("Artist: " + id3v1Tag.getArtist()); if (mp3file.hasId3v1Tag()) { id3v1Tag = mp3file.getId3v1Tag(); } else { id3v1Tag = new ID3v1Tag(); mp3file.setId3v1Tag(id3v1Tag); id3v1Tag.setGenre(12); id3v1Tag.setComment("Some comment"); mp3file.save("MyMp3File.mp3"); if (mp3file.hasId3v2Tag()) { ID3v2 id3v2Tag = mp3file.getId3v2Tag(); System.out.println("Track: " + id3v2Tag.getTrack()); System.out.println("Artist: " + id3v2Tag.getArtist());
protected Mp3Retag(String filename) { try { mp3file = new Mp3File(filename); boolean hasId3v1Tag = mp3file.hasId3v1Tag(); boolean hasId3v2Tag = mp3file.hasId3v2Tag(); boolean hasImage = false; if (hasId3v2Tag && mp3file.getId3v2Tag().getAlbumImage() != null) hasImage = true; if (! hasId3v1Tag && ! hasId3v2Tag) { printError("ERROR processing \"" + FileUtils.extractFilename(filename) + "\" - no ID3 tags found"); } else { boolean hasCustomTag = mp3file.hasCustomTag(); if (hasCustomTag && mp3file.getCustomTag().length > CUSTOM_TAG_WARNING_THRESHOLD) { printError("WARNING processing \"" + FileUtils.extractFilename(filename) + "\" - custom tag is " + mp3file.getCustomTag().length + " bytes, potential corrupt file"); message.append(FileUtils.extractFilename(mp3file.getFilename())); message.append("\""); if (! hasId3v1Tag) message.append(", added ID3v1 tag"); if (! hasId3v2Tag) message.append(", added ID3v2 tag"); if (! hasImage && mp3file.getId3v2Tag().getAlbumImage() != null) { message.append(", added album image"); if (mp3file.hasCustomTag()) message.append(", added custom tag"); } else { if (! mp3file.hasCustomTag()) message.append(", removed custom tag"); else if (keepCustomTag && customTag != null && customTag.length() > 0) message.append(", appended to custom tag"); else message.append(", replaced custom tag"); if (mp3file != null) deleteFile(mp3file.getFilename() + RETAG_EXTENSION); } catch (Exception e) { printError("ERROR processing \"" + FileUtils.extractFilename(filename) + "\" - " + formatExceptionMessage(e));
private void catalogMp3Fields(StringBuffer cat) { catalogField(cat, FileUtils.extractFilename(mp3file.getFilename())); catalogField(cat, Long.toString(mp3file.getLength())); catalogField(cat, Long.toString(mp3file.getLengthInSeconds())); catalogField(cat, mp3file.getVersion()); catalogField(cat, mp3file.getLayer()); catalogField(cat, Integer.toString(mp3file.getSampleRate())); catalogField(cat, Integer.toString(mp3file.getBitrate())); catalogField(cat, vbrString(mp3file.isVbr())); catalogField(cat, mp3file.getChannelMode()); }
Mp3File mp3file = new Mp3File("example.mp3"); ID3v2 id3v2Tag; if (mp3file.hasId3v2Tag()) { id3v2Tag = mp3file.getId3v2Tag(); } else { id3v2Tag = new ID3v24Tag(); mp3file.setId3v2Tag(id3v2Tag); } id3v2Tag.clearAlbumImage(); mp3file.save("example.mp3");
Mp3File mp3file = new Mp3File("SomeMp3File.mp3"); System.out.println("Length of this mp3 in seconds is: " + mp3file.getLengthInSeconds()); if(mp3file.hasId3v2Tag()) { ID3v2 id3v2tag = mp3file.getId3v2Tag(); System.out.println("The artist in the ID3v2 tag is: " + id3v2tag.getArtist()); id3v2tag.setArtist("A new artist"); mp3file.save("ASavedMp3File.mp3"); System.out("Saved mp3 file with new name and artist set to 'A new artist'"); }
Mp3File mp3file = new Mp3File("SomeMp3File.mp3"); 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.setTrack("5"); id3v1Tag.setArtist("An Artist"); id3v1Tag.setTitle("The Title"); id3v1Tag.setAlbum("The Album"); id3v1Tag.setYear("2001"); id3v1Tag.setGenre(12); id3v1Tag.setComment("Some comment"); mp3file.save("MyMp3File.mp3");
protected ID3Wrapper createId3Wrapper(String filename) throws IOException, UnsupportedTagException, InvalidDataException { Mp3File mp3file = new Mp3File(filename, false); ID3Wrapper id3Wrapper = new ID3Wrapper(mp3file.getId3v1Tag(), mp3file.getId3v2Tag()); return id3Wrapper; }
private void updateId3Tags() { ID3Wrapper oldId3Wrapper = new ID3Wrapper(mp3file.getId3v1Tag(), mp3file.getId3v2Tag()); ID3Wrapper newId3Wrapper = new ID3Wrapper(new ID3v1Tag(), new ID3v23Tag()); preProcess(oldId3Wrapper); mp3file.setId3v1Tag(newId3Wrapper.getId3v1Tag()); mp3file.setId3v2Tag(newId3Wrapper.getId3v2Tag());
if (fileResult == JFileChooser.APPROVE_OPTION) { String path = jfc.getSelectedFile().getPath(); mp3file = new Mp3File(path); if (mp3file!=null && mp3file.hasId3v1Tag()) { ID3v1 id3v1Tag = mp3file.getId3v1Tag(); System.out.println("Track: " + id3v1Tag.getTrack()); System.out.println("Artist: " + id3v1Tag.getArtist());
private String chooseFilename(String mimeType) { String extension; int idx; if ((idx = mimeType.indexOf('/')) > 0) extension = "." + mimeType.substring(idx + 1).toLowerCase(); else mimeType = extension = "." + mimeType.toLowerCase(); ID3Wrapper id3Wrapper = new ID3Wrapper(mp3file.getId3v1Tag(), mp3file.getId3v2Tag()); String path = FileUtils.extractPath(mp3file.getFilename()); String baseFilename = path + toCompressedString(id3Wrapper.getArtist()) + "-" + toCompressedString(id3Wrapper.getAlbum()); String filename; if (!fileExists(filename = baseFilename + extension)) return filename; int i = 1; while (true) { if (!fileExists(filename = baseFilename + Integer.toString(i) + extension)) return filename; i++; } }
private void catalogId3Fields(StringBuffer cat) { ID3v1 id3v1tag = mp3file.getId3v1Tag(); ID3v2 id3v2tag = mp3file.getId3v2Tag(); ID3Wrapper id3wrapper = new ID3Wrapper(id3v1tag, id3v2tag); if (id3v1tag != null) catalogField(cat, "1." + id3v1tag.getVersion()); else catalogField(cat, null); if (id3v2tag != null) catalogField(cat, "2." + id3v2tag.getVersion()); else catalogField(cat, null); catalogField(cat, id3wrapper.getTrack()); catalogField(cat, id3wrapper.getArtist()); catalogField(cat, id3wrapper.getAlbum()); catalogField(cat, id3wrapper.getTitle()); catalogField(cat, id3wrapper.getYear()); catalogField(cat, id3wrapper.getGenreDescription()); catalogField(cat, id3wrapper.getComment()); catalogField(cat, id3wrapper.getComposer()); catalogField(cat, id3wrapper.getOriginalArtist()); catalogField(cat, id3wrapper.getCopyright()); catalogField(cat, id3wrapper.getUrl()); catalogField(cat, id3wrapper.getEncoder()); catalogField(cat, id3wrapper.getAlbumImageMimeType()); }
private void initId3v1Tag(SeekableByteChannel seekableByteChannel) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate(ID3v1Tag.TAG_LENGTH); seekableByteChannel.position(getLength() - ID3v1Tag.TAG_LENGTH); byteBuffer.clear(); int bytesRead = seekableByteChannel.read(byteBuffer); if (bytesRead < ID3v1Tag.TAG_LENGTH) throw new IOException("Not enough bytes read"); try { id3v1Tag = new ID3v1Tag(byteBuffer.array()); } catch (NoSuchTagException e) { id3v1Tag = null; } }
private void retag() throws IOException, NotSupportedException { updateId3Tags(); updateCustomTag(); mp3file.save(mp3file.getFilename() + RETAG_EXTENSION); renameFiles(); }
protected Mp3Details(String filename) { try { mp3file = new Mp3File(filename); show(); } catch (BaseException e) { printError("ERROR processing " + FileUtils.extractFilename(filename) + " - " + e.getDetailedMessage()); } catch (Exception e) { printError("ERROR processing " + FileUtils.extractFilename(filename) + " - " + formatExceptionMessage(e)); } }
protected void extractPics() throws IOException { ID3v2 id3v2tag = mp3file.getId3v2Tag(); if (id3v2tag != null) { String mimeType = id3v2tag.getAlbumImageMimeType(); byte[] data = id3v2tag.getAlbumImage(); String filename = chooseFilename(mimeType); RandomAccessFile file = null; try { file = new RandomAccessFile(filename, "rw"); file.write(data); } finally { try { if (file != null) { printOut(" Extracted " + filename); file.close(); } } catch (IOException e) { // do nothing } } } }
private void showId3v1Fields(StringBuffer buffer) { buffer.append("ID3v1 Data\n"); ID3v1 id3v1tag = mp3file.getId3v1Tag(); if (id3v1tag == null) { buffer.append(" NONE!\n"); } else { showField(buffer, "Track", id3v1tag.getTrack()); showField(buffer, "Artist", id3v1tag.getArtist()); showField(buffer, "Title", id3v1tag.getTitle()); showField(buffer, "Album", id3v1tag.getAlbum()); showField(buffer, "Year", id3v1tag.getYear()); showField(buffer, "Genre", id3v1tag.getGenreDescription()); showField(buffer, "Comment", id3v1tag.getComment()); } }
private void showMp3Fields(StringBuffer buffer) { buffer.append("MP3 Data\n"); showField(buffer, "Filename", FileUtils.extractFilename(mp3file.getFilename())); showField(buffer, "Size", Long.toString(mp3file.getLength())); showField(buffer, "Length", formatTime(mp3file.getLengthInSeconds())); showField(buffer, "Version", mp3file.getVersion()); showField(buffer, "Layer", mp3file.getLayer()); showField(buffer, "Sample rate", Integer.toString(mp3file.getSampleRate()), "Hz"); showField(buffer, "Bitrate", Integer.toString(mp3file.getBitrate()), "kbps (" + vbrString(mp3file.isVbr()) + ")"); showField(buffer, "Channel mode", mp3file.getChannelMode()); }