Additional information for Mp4 files

Reading Data

The Mp4FieldKey enumeration internally maps to the audio specific enumeration, you can use this directly


Mp4Tag mp4tag = (Mp4Tag)f.getTag();	
mp4tag.getFirst(Mp4FieldKey.ARTIST);
mp4tag.getFirst(Mp4FieldKey.COMPOSER);
mp4tag.getFirst(Mp4FieldKey.LYRICS);

and it gives access to additional fields that are specific to that format


mp4tag.getFirst(Mp4FieldKey.PART_OF_GAPLESS_ALBUM);
mp4tag.getFirst(Mp4FieldKey.ITUNES_SMPB);

Mp4 supports binary data, although Artwork is the only binary field I know of in use, artwork can be retrieved as follows


Mp4Tag mp4tag = (Mp4Tag)f.getTag();	
TagField covrArtField =  mp4tag.getFirstField(Mp4FieldKey.ARTWORK);	
BufferedImage bi = ImageIO.read(ImageIO.createImageInputStream(new ByteArrayInputStream(coverArtField.getData()))); 
			

Mp4 TRACK and DISC_NO fields support the '1/10' and '1' format, you can programatically access these components as follows


short discNo     = ((Mp4DiscNoField)mp4tag.getFirstField(Mp4FieldKey.DISCNUMBER)).getDiscNo());
short disctotal  = ((Mp4DiscNoField)mp4tag.getFirstField(Mp4FieldKey.DISCNUMBER)).getDiscTotal());
short trackNo    = ((Mp4TrackField)mp4tag.getFirstField(Mp4FieldKey.TRACK)).getTrackNo());
short trackTotal = ((Mp4TrackField)mp4tag.getFirstField(Mp4FieldKey.TRACK)).getTrackTotal());          
		

Some fields in Mp4 are held in a format called Reverse Dns, you can identify which fields are reverse dns by looking at the Mp4FieldKey enumeration. These fields contain three components: an issuer, a descriptor and the data itself. You can retrieve the full information as follows

	
Mp4TagReverseDnsField rvs = (Mp4TagReverseDnsField)mp4tag.getFirstField(Mp4FieldKey.MUSICBRAINZ_ALBUMID);
rvs.getIssuer();
rvs.getDescriptor();
rvs.getContent();

Writing Data

The TagFieldKey enumeration internally maps to the audio specific enumeration, you can use this directly


Mp4Tag mp4tag = (Mp4Tag)f.getTag();	
mp4tag.set(mp4tag.createTagField(Mp4FieldKey.COMPOSER,"Charlie keigher"));
mp4tag.set(mp4tag.createTagField(Mp4FieldKey.ARTIST_SORT,"king of Slums"));
mp4tag.set(mp4tag.createTagField(Mp4FieldKey.LYRICS,""you are a fanciable headcase ..."));

and it gives access to additional fields that are specific to that format


mp4tag.set(mp4tag.createTagField(Mp4FieldKey.PART_OF_GAPLESS_ALBUM,"1"));

Mp4 only supports a limited set of fields, if you need to create a field that is not available in jaudiotagger please add an issue to the jaudiotagger issue list.

Mp4 does directly support binary data, although Artwork is the only binary field I know of in use


RandomAccessFile imageFile = new RandomAccessFile(new File(coverart.png","r");
byte[] imagedata = new byte[(int)imageFile.length()];
imageFile.read(imagedata);
tag.add(((Mp4Tag)tag).createArtworkField(imagedata));
			

These formats deal with some other fields differently as well. Mp4 TRACK and DISC_NO fields support the '1/10' and '1' format, Jaudiotagger can parse the value provided or you can programatically set these components as follows


mp4tag.set(tag.createTagField(TagFieldKey.TRACK,"4/15"));
mp4tag.set(new Mp4TrackField(4,15));
mp4tag.set(new Mp4TrackField(4));

mp4tag.set(tag.createTagField(TagFieldKey.DISC_NO,"1/10"));
mp4tag.set(new Mp4DiscNoField(1,10));
mp4tag.set(new Mp4DiscNoField(1));