sandreas/tone

Remove specific strings from a tag

StefanDorschu opened this issue · 3 comments

Is it possible to remove a specific string in a tag of a m4b-file if that string exists?

e.g.
Album name: "The great audiobook - stringtoberemoved"

--> Album name: "The great audiobook"

Is it possible to remove a specific string in a tag of a m4b-file if that string exists?

It is possible, although you have to use a custom javascript tagger for this.

Here is an example:

Create a file replace.js with the following content:

function replace(metadata, parameters) {
    const separator = "::";
    parameters.filter(p => p.indexOf(separator) !== -1)
        .map((p) => {
            let splitAt = p.split(separator);
            if(splitAt.length === 2) {
                splitAt.push("");
            }
            return splitAt;
        }).forEach((i) => {
            let [field, pattern, replacement] = i;
            if(field in metadata && typeof metadata[field] === "string") {
                console.log("replacing <" + pattern + "> with <" + replacement+ "> in field " + field);
                metadata[field] = metadata[field].replaceAll(pattern, replacement);
            } else {
                console.log("field " + field + " is not a string and cannot be replaced");
            }
        });
}

tone.RegisterTagger("replace");

Then run:

 tone tag audiofile.m4b --script="replace.js" --taggers="replace" --script-tagger-parameter="Album:: - stringtoberemoved::" --dry-run

Remove the --dry-run to actually write the changes to the file. Custom JavaScript taggers are a mighty tool to extend tone. If you know only a little bit of JavaScript, you can change the code of the function to do whatever you like.

Hope it helps.

Wow thank you!

Wow thank you!

You're welcome! Let me know on success, then I can close this issue :-)