Skip to content

Instantly share code, notes, and snippets.

@jhubble
Created March 18, 2023 17:54
Show Gist options
  • Save jhubble/69e89f1959b6088c5452f4eee48ac1d7 to your computer and use it in GitHub Desktop.
Save jhubble/69e89f1959b6088c5452f4eee48ac1d7 to your computer and use it in GitHub Desktop.
Change tags of all mp3s in directory from Title: Title (Artist) to Title: Title Artist: Artist
//
// For all files in directory (mp3s), change Title and Artist tags
// From: Title: This is the name (this is the artist)
// To: Title: This is the name Artist: this is the artist
// Must npm install node-id3 before running
const NodeID3 = require('node-id3');
var fs = require('fs');
const dir = process.argv[2];
if (!dir || dir.indexOf('-') === 0) {
console.error("usage: node updateTags.js directory\nWill extract artist in parens from title tag and make artist tag\n");
process.exit();
}
const updateTag = (file) => {
console.log("processing:",file);
const tags = NodeID3.read(file)
let title = tags.title;
console.log(tags.artist, tags.title);
tags.title = tags.title.replace(/\(([^\)]+)\)/, function(repl,artist) {
if (artist) {
tags.artist = artist;
}
return '';
});
if (title !== tags.title) {
console.log("updating");
const success = NodeID3.write(tags, file) // Returns true/Error
}
}
var files = fs.readdirSync(dir);
files.forEach(file => {
updateTag(dir+'/'+file);
console.log(dir+'/'+file);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment