From cd216374247deb97a3e7ff00f8c66e441c33b974 Mon Sep 17 00:00:00 2001 From: paultaylor Date: Thu, 22 Sep 2011 09:47:52 +0000 Subject: [PATCH] #JAUDIOTAGGER-387:Not writing new tag correctly when file has no tag but does has free top header git-svn-id: https://svn.java.net/svn/jaudiotagger~svn/trunk@998 922cec90-98bc-ecba-b0a9-867c771b4a74 --- .../jaudiotagger/audio/mp4/Mp4InfoReader.java | 4 +- .../jaudiotagger/audio/mp4/Mp4TagWriter.java | 7 +-- .../jaudiotagger/tag/mp4/Mp4TagCreator.java | 4 +- .../org/jaudiotagger/issues/Issue387Test.java | 45 +++++++++++++++++++ testdata/testfiles.txt | 4 +- 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 srctest/org/jaudiotagger/issues/Issue387Test.java diff --git a/src/org/jaudiotagger/audio/mp4/Mp4InfoReader.java b/src/org/jaudiotagger/audio/mp4/Mp4InfoReader.java index 3da8b0b4..118c6058 100644 --- a/src/org/jaudiotagger/audio/mp4/Mp4InfoReader.java +++ b/src/org/jaudiotagger/audio/mp4/Mp4InfoReader.java @@ -282,7 +282,7 @@ public GenericAudioHeader read(RandomAccessFile raf) throws CannotReadException, } } } - //Set default channels if couldnt calculate it + //Set default channels if couldn't calculate it if (info.getChannelNumber() == -1) { info.setChannelNumber(2); @@ -294,7 +294,7 @@ public GenericAudioHeader read(RandomAccessFile raf) throws CannotReadException, info.setBitrate(128); } - //This is the most likley option if cant find a match + //This is the most likely option if cant find a match if (info.getEncodingType().equals("")) { info.setEncodingType(EncoderType.AAC.getDescription()); diff --git a/src/org/jaudiotagger/audio/mp4/Mp4TagWriter.java b/src/org/jaudiotagger/audio/mp4/Mp4TagWriter.java index 42923f44..9fe537f6 100644 --- a/src/org/jaudiotagger/audio/mp4/Mp4TagWriter.java +++ b/src/org/jaudiotagger/audio/mp4/Mp4TagWriter.java @@ -480,7 +480,7 @@ else if (oldIlstSize > newIlstSize) //There is not enough padding in the metadata free atom anyway //Size meta needs to be increased by (if not writing a free atom) //Special Case this could actually be negative (upto -8)if is actually enough space but would - //not be able to write free atom properly, it doesnt matter the parent atoms would still + //not be able to write free atom properly, it doesn't matter the parent atoms would still //need their sizes adjusted. else { @@ -510,10 +510,11 @@ else if (oldIlstSize > newIlstSize) || ((topLevelFreeSize - Mp4BoxHeader.HEADER_LENGTH < additionalMetaSizeThatWontFitWithinMetaAtom) && (topLevelFreeSize != additionalMetaSizeThatWontFitWithinMetaAtom))) { - //We dont bother using the top level free atom coz not big enough anyway, we need to adjust offsets + //We don't bother using the top level free atom coz not big enough anyway, we need to adjust offsets //by the amount mdat is going to be shifted if (mdatHeader.getFilePos() > moovHeader.getFilePos()) { + logger.config("Adjusting Offsets"); stco.adjustOffsets(additionalMetaSizeThatWontFitWithinMetaAtom); } } @@ -596,7 +597,7 @@ else if (oldIlstSize > newIlstSize) //If we have top level free atom that comes before mdat we might be able to use it but only if //the free atom actually come after the the metadata - if (topLevelFreeAtomComesBeforeMdatAtomAndAfterMetadata&&(topLevelFreePosition>startIlstWithinFile)) + if (topLevelFreeAtomComesBeforeMdatAtomAndAfterMetadata&&(topLevelFreePosition>=startIlstWithinFile)) { //If the shift is less than the space available in this second free atom data size we should //minimize the free atom accordingly (then we don't have to update stco atom) diff --git a/src/org/jaudiotagger/tag/mp4/Mp4TagCreator.java b/src/org/jaudiotagger/tag/mp4/Mp4TagCreator.java index 841861d4..a4ba508c 100644 --- a/src/org/jaudiotagger/tag/mp4/Mp4TagCreator.java +++ b/src/org/jaudiotagger/tag/mp4/Mp4TagCreator.java @@ -98,9 +98,9 @@ public ByteBuffer convert(Tag tag, int padding) throws UnsupportedEncodingExcept { processedArtwork = true; - //Because each artwork image is held within the tag as a seperate field, but when + //Because each artwork image is held within the tag as a separate field, but when //they are written they are all held under a single covr box we need to do some checks - //and special processing here if we have any artwork image (this code only neccessary + //and special processing here if we have any artwork image (this code only necessary //if we have more than 1 but do it anyway even if only have 1 image) ByteArrayOutputStream covrDataBaos = new ByteArrayOutputStream(); diff --git a/srctest/org/jaudiotagger/issues/Issue387Test.java b/srctest/org/jaudiotagger/issues/Issue387Test.java new file mode 100644 index 00000000..c82548f1 --- /dev/null +++ b/srctest/org/jaudiotagger/issues/Issue387Test.java @@ -0,0 +1,45 @@ +package org.jaudiotagger.issues; + +import org.jaudiotagger.AbstractTestCase; +import org.jaudiotagger.audio.AudioFile; +import org.jaudiotagger.audio.AudioFileIO; +import org.jaudiotagger.audio.mp4.Mp4AtomTree; + +import java.io.File; +import java.io.RandomAccessFile; + +/** + * Test writing mp4 + */ +public class Issue387Test extends AbstractTestCase +{ + public void testIssue() throws Exception + { + Exception caught = null; + try + { + File orig = new File("testdata", "test100.mp4"); + if (!orig.isFile()) + { + System.err.println("Unable to test file - not available"); + return; + } + + File testFile = AbstractTestCase.copyAudioToTmp("test100.mp4"); + AudioFile af = AudioFileIO.read(testFile); + System.out.println(af.getAudioHeader()); + af.getTagOrCreateAndSetDefault(); + af.commit(); + + Mp4AtomTree atomTree = new Mp4AtomTree(new RandomAccessFile(testFile, "r")); + atomTree.printAtomTree(); + + } + catch(Exception e) + { + caught=e; + e.printStackTrace(); + } + assertNull(caught); + } +} \ No newline at end of file diff --git a/testdata/testfiles.txt b/testdata/testfiles.txt index 7a88c23c..81e8d352 100644 --- a/testdata/testfiles.txt +++ b/testdata/testfiles.txt @@ -47,4 +47,6 @@ test94.mp3,no,369,mp3 without id3 test95.m4a,no,368,MPE reading discno,disctotal test96.m4a,no,370,valid mp4 with corrupted free header preventing finding mdat atom test97.mp3,no,028,Audio Version 2 Layer III -test98.m4a,no,380,Mp4 with problem with tracktotal \ No newline at end of file +test98.m4a,no,380,Mp4 with problem with tracktotal +test99.mp3,no,386,Mp3 that lib sees as 128 kbps when in fact it is 192 kbps +test100.mp4,no,387,Error creating tag in mp4 file \ No newline at end of file