I created the setArtwork method to set the artwork of an aac file in an .m4a container and it does exactly what I want it to when I run the main method
public static void setArtwork(File art, File m4a) throws Exception{
Mp4TagReader reader = new Mp4TagReader();
Mp4TagWriter writer = new Mp4TagWriter();
RandomAccessFile song = new RandomAccessFile(m4a.toString(),"rw");
Mp4Tag mp4tag = reader.read(song);
Artwork artwork = ArtworkFactory.createArtworkFromFile(art);
mp4tag.addField(artwork);
mp4tag.setField(artwork);
RandomAccessFile temp = new RandomAccessFile(m4a,"rw");
writer.write(mp4tag,song,temp);
}
public static void main(String[] args) throws Exception{
File art = new File("C:\\Users\\Zubair\\Documents\\music\\coverArt.jpeg");
File m4a = new File("C:\\Users\\Zubair\\Documents\\music\\song.m4a");
setArtwork(art,m4a);
}
BUT, when I try to use the setArtwork method in a different method in a different class it doesn't actually save the mp4 tag to the File. I did some debugging to see if the picture was even being added to the artwork tag and it all looks good, but it seems that the tag doesn't get written to the file.
public static void mp3Tom4a(File mp3File, File m4aFolder, File coverArt) throws Exception {
String input = mp3File.toString();
String name = mp3File.getName();
String output = name.substring(0,name.indexOf(MP3)) + M4A;
output = m4aFolder.toString() + "\\" + output;
//cd ffmpeg\bin && ffmpeg -y -i input.mp3 -an -vcodec copy cover.jpg && ffmpeg -y -i input.mp3 -c:a aac -b:a 192k -vn output.m4a
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe","/c","cd ffmpeg\\bin && ffmpeg -y -i "
+ input +" -an -vcodec copy "+coverArt+
" && ffmpeg -y -i " + input + " -c:a aac -b:a 128k -vn " + output);
builder.redirectErrorStream(true);
builder.start();
JAudioData.setArtwork(coverArt,new File(output));
}
public static void main(String[] args) throws Exception {
mp3Tom4a(new File("C:\\Users\\Zubair\\Documents\\music\\song.mp3"),
new File("C:\\Users\\Zubair\\Documents\\music"),
new File("C:\\Users\\Zubair\\Documents\\music\\coverArt.jpeg"));
}
it doesn't make sense that setArtwork only works when it's in the main method of its own class, especially because the objects I am using for the parameters are identical to eachother, so there should be no difference in the result. I think it might have something to do with the RandomAccessFile object being updated but the physical storage not getting updated
Edit- If I comment out builder.start() then it can successfully write the mp4tag to the m4a file. But I don't see why having builder.start() would prevent that from happenning. My best guess is that because builder.start() is what creates song.m4a it is still being "edited" by that and can't be edited by any other processes.