public void testDeleteFields() throws Exception { // Delete using generic key File testFile = AbstractTestCase.copyAudioToTmp("test1.wma", new File("testDeleteFields.wma")); AudioFile f = AudioFileIO.read(testFile); List<TagField> tagFields = f.getTag().getFields(FieldKey.ALBUM_ARTIST_SORT); assertEquals(0, tagFields.size()); f.getTag().addField(FieldKey.ALBUM_ARTIST_SORT, "artist1"); tagFields = f.getTag().getFields(FieldKey.ALBUM_ARTIST_SORT); assertEquals(1, tagFields.size()); f.getTag().deleteField(FieldKey.ALBUM_ARTIST_SORT); f.commit(); // Delete using flac id f = AudioFileIO.read(testFile); tagFields = f.getTag().getFields(FieldKey.ALBUM_ARTIST_SORT); assertEquals(0, tagFields.size()); f.getTag().addField(FieldKey.ALBUM_ARTIST_SORT, "artist1"); tagFields = f.getTag().getFields(FieldKey.ALBUM_ARTIST_SORT); assertEquals(1, tagFields.size()); f.getTag().deleteField("WM/AlbumArtistSortOrder"); tagFields = f.getTag().getFields(FieldKey.ALBUM_ARTIST_SORT); assertEquals(0, tagFields.size()); f.commit(); f = AudioFileIO.read(testFile); tagFields = f.getTag().getFields(FieldKey.ALBUM_ARTIST_SORT); assertEquals(0, tagFields.size()); }
private boolean setAlbumArt( byte[] imageBytes, String mp3Filename, String mp3outputFilename, String username, String title, String detailsUrl) { try { AudioFile f = AudioFileIO.read(new File(mp3Filename)); Tag tag = f.getTagOrCreateAndSetDefault(); tag.setField(FieldKey.ALBUM, username + ": " + title + " via SoundCloud.com"); tag.setField(FieldKey.ARTIST, username); tag.setField(FieldKey.TITLE, title); tag.setField(FieldKey.URL_OFFICIAL_RELEASE_SITE, detailsUrl); Artwork artwork = ArtworkFactory.getNew(); artwork.setBinaryData(imageBytes); artwork.setMimeType("image/jpg"); tag.addField(artwork); f.commit(); return true; } catch (Throwable e) { return false; } }
public void testWriteToRelativeMp3File() { File orig = new File("testdata", "testV1.mp3"); if (!orig.isFile()) { System.err.println("Unable to test file - not available"); return; } File testFile = null; Exception exceptionCaught = null; try { testFile = AbstractTestCase.copyAudioToTmp("testV1.mp3"); // Copy up a level coz we need it to be in same folder as working directory so can just // specify filename File outputFile = new File(testFile.getName()); boolean result = copy(testFile, outputFile); assertTrue(result); // make Relative assertTrue(outputFile.exists()); // Read File okay AudioFile af = AudioFileIO.read(outputFile); // Create tag and Change File af.getTagOrCreateAndSetDefault(); af.getTag().setField(ArtworkFactory.createArtworkFromFile(new File("testdata/coverart.jpg"))); af.commit(); } catch (Exception e) { e.printStackTrace(); exceptionCaught = e; } assertNull(exceptionCaught); }
/** Lets now check the value explicity are what we expect */ public void testTagFieldKeyWrite2() { Exception exceptionCaught = null; try { File testFile = AbstractTestCase.copyAudioToTmp("test1.wma", new File("testwrite1.wma")); AudioFile f = AudioFileIO.read(testFile); AudioFileIO.delete(f); // test fields are written with correct ids f = AudioFileIO.read(testFile); Tag tag = f.getTag(); for (FieldKey key : FieldKey.values()) { if (!(key == FieldKey.COVER_ART)) { tag.addField(tag.createField(key, key.name() + "_value")); } } f.commit(); // Reread File f = AudioFileIO.read(testFile); tag = f.getTag(); TagField tf = tag.getFirstField(AsfFieldKey.ALBUM.getFieldName()); assertEquals("WM/AlbumTitle", tf.getId()); assertEquals("ALBUM_value", ((TagTextField) tf).getContent()); assertEquals("UTF-16LE", ((TagTextField) tf).getEncoding()); tf = tag.getFirstField(AsfFieldKey.ALBUM_ARTIST.getFieldName()); assertEquals("WM/AlbumArtist", tf.getId()); assertEquals("ALBUM_ARTIST_value", ((TagTextField) tf).getContent()); assertEquals("UTF-16LE", ((TagTextField) tf).getEncoding()); tf = tag.getFirstField(AsfFieldKey.AMAZON_ID.getFieldName()); assertEquals("ASIN", tf.getId()); assertEquals("AMAZON_ID_value", ((TagTextField) tf).getContent()); assertEquals("UTF-16LE", ((TagTextField) tf).getEncoding()); tf = tag.getFirstField(AsfFieldKey.TITLE.getFieldName()); assertEquals("TITLE", tf.getId()); assertEquals("TITLE_value", ((TagTextField) tf).getContent()); assertEquals("UTF-16LE", ((TagTextField) tf).getEncoding()); } catch (Exception e) { e.printStackTrace(); exceptionCaught = e; } assertNull(exceptionCaught); }
/** Just create fields for all the tag field keys defined, se if we hit any problems */ public void testTagFieldKeyWrite() { Exception exceptionCaught = null; try { File testFile = AbstractTestCase.copyAudioToTmp("test1.wma", new File("testwrite1.wma")); AudioFile f = AudioFileIO.read(testFile); AudioFileIO.delete(f); // Tests multiple iterations on same file for (int i = 0; i < 2; i++) { f = AudioFileIO.read(testFile); Tag tag = f.getTag(); for (FieldKey key : FieldKey.values()) { if (!(key == FieldKey.COVER_ART)) { tag.setField(tag.createField(key, key.name() + "_value_" + i)); } } f.commit(); f = AudioFileIO.read(testFile); tag = f.getTag(); for (FieldKey key : FieldKey.values()) { /* * Test value retrieval, using multiple access methods. */ if (!(key == FieldKey.COVER_ART)) { String value = key.name() + "_value_" + i; System.out.println("Value is:" + value); assertEquals(value, tag.getFirst(key)); AsfTagTextField atf = (AsfTagTextField) tag.getFields(key).get(0); assertEquals(value, atf.getContent()); atf = (AsfTagTextField) tag.getFields(key).get(0); assertEquals(value, atf.getContent()); } } } } catch (Exception e) { e.printStackTrace(); exceptionCaught = e; } assertNull(exceptionCaught); }
/** * writes bpm and key to KEY_START and BPM fields in the tag * * @param filename * @param formattedBpm * @param key */ public boolean updateTags(String filename, String formattedBpm, String key) { File file = new File(filename); try { AudioFile f = AudioFileIO.read(file); if (!setCustomTag(f, "KEY_START", key)) { throw new IOException("Error writing Key Tag"); } if (!c.noBpm) { Tag tag = f.getTag(); if (tag instanceof Mp4Tag) { if (!setCustomTag(f, "BPM", formattedBpm)) { throw new IOException("Error writing BPM Tag"); } } tag.setField(FieldKey.BPM, formattedBpm); } f.commit(); return true; } catch (Exception e) { System.out.println("problem with tags in file " + filename); return false; } }
public void writeFile(AudioFile f) throws CannotWriteException { // Because audio file is an instanceof MP3File this directs it to save f.commit(); }
public void deleteTag(AudioFile f) throws CannotWriteException { // Because audio file is an instanceof MP3File this directs it to save // taking into account if the tag has been sent to null in which case it will be deleted f.commit(); }
/** * This will write a custom ID3 tag (TXXX). This works only with MP3 files (Flac with ID3-Tag not * tested). * * @param description The description of the custom tag i.e. "catalognr" There can only be one * custom TXXX tag with that description in one MP3 file * @param text The actual text to be written into the new tag field * @return True if the tag has been properly written, false otherwise */ public static boolean setCustomTag(AudioFile audioFile, String description, String text) throws IOException { FrameBodyTXXX txxxBody = new FrameBodyTXXX(); txxxBody.setDescription(description); txxxBody.setText(text); // Get the tag from the audio file // If there is no ID3Tag create an ID3v2.3 tag Tag tag = audioFile.getTagOrCreateAndSetDefault(); if (tag instanceof AbstractID3Tag) { // If there is only a ID3v1 tag, copy data into new ID3v2.3 tag if (!(tag instanceof ID3v23Tag || tag instanceof ID3v24Tag)) { Tag newTagV23 = null; if (tag instanceof ID3v1Tag) { newTagV23 = new ID3v23Tag((ID3v1Tag) audioFile.getTag()); // Copy old tag data } if (tag instanceof ID3v22Tag) { newTagV23 = new ID3v23Tag((ID3v22Tag) audioFile.getTag()); // Copy old tag data } audioFile.setTag(newTagV23); tag = newTagV23; } AbstractID3v2Frame frame = null; if (tag instanceof ID3v23Tag) { if (((ID3v23Tag) audioFile.getTag()).getInvalidFrames() > 0) { throw new IOException("read some invalid frames!"); } frame = new ID3v23Frame("TXXX"); } else if (tag instanceof ID3v24Tag) { if (((ID3v24Tag) audioFile.getTag()).getInvalidFrames() > 0) { throw new IOException("read some invalid frames!"); } frame = new ID3v24Frame("TXXX"); } frame.setBody(txxxBody); try { tag.setField(frame); } catch (FieldDataInvalidException e) { Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, e); return false; } } else if (tag instanceof FlacTag) { try { ((FlacTag) tag).setField(description, text); } catch (KeyNotFoundException ex) { Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (FieldDataInvalidException ex) { return false; } } else if (tag instanceof Mp4Tag) { // TagField field = new Mp4TagTextField("----:com.apple.iTunes:"+description, text); TagField field; field = new Mp4TagReverseDnsField( Mp4TagReverseDnsField.IDENTIFIER + ":" + "com.apple.iTunes" + ":" + description, "com.apple.iTunes", description, text); // TagField field = new Mp4TagTextField(description, text); try { tag.setField(field); } catch (FieldDataInvalidException ex) { Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex); return false; } } else if (tag instanceof VorbisCommentTag) { try { ((VorbisCommentTag) tag).setField(description, text); } catch (KeyNotFoundException ex) { Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (FieldDataInvalidException ex) { Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex); return false; } } else { // tag not implented Logger.getLogger(TrackAnalyzer.class.getName()) .log( Level.WARNING, "couldn't write key information for " + audioFile.getFile().getName() + " to tag, because this format is not supported."); return false; } // write changes in tag to file try { audioFile.commit(); } catch (CannotWriteException e) { e.printStackTrace(); return false; } return true; }
public void testWriteFile() { Exception exceptionCaught = null; try { File testFile = AbstractTestCase.copyAudioToTmp("test1.wma", new File("testwrite1.wma")); AudioFile f = AudioFileIO.read(testFile); assertEquals("32", f.getAudioHeader().getBitRate()); assertEquals( "ASF (audio): 0x0161 (Windows Media Audio (ver 7,8,9))", f.getAudioHeader().getEncodingType()); assertEquals("2", f.getAudioHeader().getChannels()); assertEquals("32000", f.getAudioHeader().getSampleRate()); assertFalse(f.getAudioHeader().isVariableBitRate()); assertTrue(f.getTag() instanceof AsfTag); AsfTag tag = (AsfTag) f.getTag(); // Write some new values and save tag.setField(FieldKey.ARTIST, "artist2"); tag.setField(FieldKey.ALBUM, "album2"); tag.setField(FieldKey.TITLE, "tracktitle2"); tag.setField(FieldKey.COMMENT, "comments2"); tag.addField(FieldKey.YEAR, "1972"); tag.setField(FieldKey.GENRE, "genre2"); tag.setField(FieldKey.TRACK, "4"); tag.setCopyright("copyright"); tag.setRating("rating"); tag.setField(tag.createField(FieldKey.URL_LYRICS_SITE, "http://www.lyrics.fly.com")); tag.setField(tag.createField(FieldKey.URL_DISCOGS_ARTIST_SITE, "http://www.discogs1.com")); tag.setField(tag.createField(FieldKey.URL_DISCOGS_RELEASE_SITE, "http://www.discogs2.com")); tag.setField(tag.createField(FieldKey.URL_OFFICIAL_ARTIST_SITE, "http://www.discogs3.com")); tag.setField(tag.createField(FieldKey.URL_OFFICIAL_RELEASE_SITE, "http://www.discogs4.com")); tag.addField(tag.createField(FieldKey.URL_WIKIPEDIA_ARTIST_SITE, "http://www.discogs5.com")); tag.addField(tag.createField(FieldKey.URL_WIKIPEDIA_RELEASE_SITE, "http://www.discogs6.com")); tag.setField(tag.createField(FieldKey.DISC_TOTAL, "3")); tag.setField(tag.createField(FieldKey.TRACK_TOTAL, "11")); // setField the IsVbr value (can be modified for now) tag.setField(tag.createField(AsfFieldKey.ISVBR, Boolean.TRUE.toString())); f.commit(); f = AudioFileIO.read(testFile); tag = (AsfTag) f.getTag(); assertTrue(f.getAudioHeader().isVariableBitRate()); assertEquals("artist2", tag.getFirst(FieldKey.ARTIST)); assertEquals("album2", tag.getFirst(FieldKey.ALBUM)); assertEquals("tracktitle2", tag.getFirst(FieldKey.TITLE)); assertEquals("comments2", tag.getFirst(FieldKey.COMMENT)); assertEquals("1972", tag.getFirst(FieldKey.YEAR)); assertEquals("4", tag.getFirst(FieldKey.TRACK)); assertEquals("genre2", tag.getFirst(FieldKey.GENRE)); assertEquals("copyright", tag.getFirstCopyright()); assertEquals("rating", tag.getFirstRating()); assertEquals("http://www.lyrics.fly.com", tag.getFirst(FieldKey.URL_LYRICS_SITE)); assertEquals("http://www.discogs1.com", tag.getFirst(FieldKey.URL_DISCOGS_ARTIST_SITE)); assertEquals("http://www.discogs2.com", tag.getFirst(FieldKey.URL_DISCOGS_RELEASE_SITE)); assertEquals("http://www.discogs3.com", tag.getFirst(FieldKey.URL_OFFICIAL_ARTIST_SITE)); assertEquals("http://www.discogs4.com", tag.getFirst(FieldKey.URL_OFFICIAL_RELEASE_SITE)); assertEquals("http://www.discogs5.com", tag.getFirst(FieldKey.URL_WIKIPEDIA_ARTIST_SITE)); assertEquals("http://www.discogs6.com", tag.getFirst(FieldKey.URL_WIKIPEDIA_RELEASE_SITE)); assertEquals("3", tag.getFirst(FieldKey.DISC_TOTAL)); assertEquals("11", tag.getFirst(FieldKey.TRACK_TOTAL)); AudioFileIO.delete(f); f = AudioFileIO.read(testFile); tag = (AsfTag) f.getTag(); assertFalse(f.getAudioHeader().isVariableBitRate()); assertTrue(tag.isEmpty()); } catch (Exception e) { e.printStackTrace(); exceptionCaught = e; } assertNull(exceptionCaught); }