Question

I've written a simple Groovy script (below) to set the values of four of the ID3v1 and ID3v2 tag fields in mp3 files using the JAudioTagger library. The script successfully makes the changes but it also deletes the first 5 to 10 seconds of some of the files, other files are unaffected. It's not a big problem, but if anyone knows a simple fix, I would be grateful. All the files are from the same source, all have v1 and v2 tags, I can find no obvious difference in the source files to explain it.

import org.jaudiotagger.*

java.util.logging.Logger.getLogger("org.jaudiotagger").setLevel(java.util.logging.Level.OFF)

Integer trackNum = 0
Integer totalFiles = 0
Integer invalidFiles = 0
validMP3File = true

def dir = new File(/D:\Users\Jeremy\Music\Speech Radio\Unlistened\Z Temp Files to MP3 Tagged/)

dir.eachFile({curFile ->
    totalFiles ++
    try {
    mp3File = org.jaudiotagger.audio.AudioFileIO.read(curFile)
    } catch (org.jaudiotagger.audio.exceptions.CannotReadException e) {
        validMP3File = false
        invalidFiles ++
    }

    // Get the file name excluding the extension
    baseFilename = org.jaudiotagger.audio.AudioFile.getBaseFilename(curFile)

    // Check that it is an MP3 file
    if (validMP3File) {
        if (mp3File.getAudioHeader().getEncodingType() != 'mp3') {
            validMP3File = false
            invalidFiles ++
        }
    }

    if (validMP3File) {

        trackNum ++

        if (mp3File.hasID3v1Tag()) {
            curTagv1 = mp3File.getID3v1Tag()
        } else {
            curTagv1 = new org.jaudiotagger.tag.id3.ID3v1Tag()
        }
        if (mp3File.hasID3v2Tag()) {
            curTagv2 = mp3File.getID3v2TagAsv24()
        } else {
            curTagv2 = new org.jaudiotagger.tag.id3.ID3v23Tag()
        }

        curTagv1.setField(org.jaudiotagger.tag.FieldKey.TITLE, baseFilename)
        curTagv2.setField(org.jaudiotagger.tag.FieldKey.TITLE, baseFilename)
        curTagv1.setField(org.jaudiotagger.tag.FieldKey.ARTIST, "BBC Radio")
        curTagv2.setField(org.jaudiotagger.tag.FieldKey.ARTIST, "BBC Radio")
        curTagv1.setField(org.jaudiotagger.tag.FieldKey.ALBUM, "BBC Radio - 20130205")
        curTagv2.setField(org.jaudiotagger.tag.FieldKey.ALBUM, "BBC Radio - 20130205")
        curTagv1.setField(org.jaudiotagger.tag.FieldKey.TRACK, trackNum.toString())
        curTagv2.setField(org.jaudiotagger.tag.FieldKey.TRACK, trackNum.toString())

        mp3File.setID3v1Tag(curTagv1)
        mp3File.setID3v2Tag(curTagv2)

        mp3File.save()
    }
})

println """$trackNum tracks created from $totalFiles files with $invalidFiles invalid files"""
Was it helpful?

Solution

I'm still investigating and it appears that there is no problem with JAudioTagger. Before setting the tags, I use Total Recorder to reduce the quality of the download from 128kbps, 44,100Hz to 56kbps, 22,050Hz. This reduces the file size to less than half and the quality is fine for speech radio.

If I run my script on the original files, none of the audio track is deleted. The deletion of the first part of the audio track only occurs with the files that have been processed by Total Recorder.

Looking at the JAudioTagger logging for these files, there does appear to be a problem with the header:

Checking further because the ID3 Tag ends at 0x23f9 but the mp3 audio doesnt start until 0x7a77
Confirmed audio starts at 0x7a77 whether searching from start or from end of ID3 tag

This check is not performed for files that have not been processed by Total Recorder.

The log of the header read operation also shows (for a 27 minute track):

trackLength:06:52

It looks as though I shall have to find a new MP3 file editor!

OTHER TIPS

Instead of

    mp3File.save()

could you try:

    mp3File.commit()

No idea if it will help, but that seems to be the documented method?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top