sandreas / tone

tone is a cross platform audio tagger and metadata editor to dump and modify metadata for a wide variety of formats, including mp3, m4b, flac and more. It has no dependencies and can be downloaded as single binary for Windows, macOS, Linux and other common platforms.
https://pilabor.com
Apache License 2.0
410 stars 17 forks source link

Remove specific strings from a tag #37

Closed StefanDorschu closed 4 months ago

StefanDorschu commented 1 year ago

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"

sandreas commented 1 year ago

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.

StefanDorschu commented 1 year ago

Wow thank you!

sandreas commented 1 year ago

Wow thank you!

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