Last active
July 1, 2022 18:03
-
-
Save gaurangrshah/770b664a7f4bba87eff80f56d7beb515 to your computer and use it in GitHub Desktop.
Id3 Tag Clean up and OSX Tag from Comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// yarn add node-id3 osx-tag | |
const path = require('path'); | |
const fs = require('fs'); | |
const NodeID3 = require('node-id3') | |
const tag = require('osx-tag'); | |
const urlREGEX = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+"); | |
const allowedTags = ['3AM', '4AM', '5AM', 'acid', 'afro', 'ah', 'ambient', 'analog', 'bouncy', 'breakdown', 'buildup', 'chillout', 'classic', 'closer', 'closing', 'commercial', 'dark', 'deep', 'dirty', 'dub', 'eastern', 'elemental', 'festival', 'funk', 'grimy', 'grinder', 'hiphouse', 'hiptech', 'house', 'I', 'I-II', 'I-IV', 'I-V', 'II', 'II-III', 'II-IV', 'III', 'III-IV', 'indie', 'IV', 'IV-V', 'jack', 'khole', 'latin', 'lounge', 'massive', 'melodic', 'minimal', 'nobass', 'nudisco', 'nujazz', 'opener', 'opening', 'PODCAST', 'prime', 'proggy', 'soul', 'spoken', 'sundaze', 'sunrise', 'tech', 'trance', 'tribal', 'V', 'vox', 'XXX'] | |
const usbDir = '/Volumes/USB DISK/' | |
const folder = 'processing-prime/' | |
const sampleDir = '/Users/gshah/Desktop/test/' | |
// const directoryPath = path.join(__dirname, 'Downloads'); | |
const directoryPath = path.join(sampleDir); | |
const getFileExtension = (filename) => filename.split('.').pop(); | |
const cleanComment = (string) => string.replace(/[^\w\s]/gi, ''); | |
const stringToComments = (string) => string.split(' '); | |
const getComments = (string) => stringToComments(cleanComment(string)); | |
function setTags(filepath, tags) { | |
tag.setTags(filepath, tags, (err, tags) => { | |
if(err) console.log(err) | |
console.log(tags); | |
}); | |
} | |
function parseMetaAndSetOSTag(meta, allowedTags) { | |
const tags = []; | |
if (meta) { | |
// console.log('has meta') | |
const album = meta.album; | |
if(album) { | |
tags.push(album.split('Set ')[1]); | |
} | |
if(meta.comment) { | |
// console.log('has comments') | |
if(meta.comment.text) { | |
// console.log('comments have text') | |
const comments = getComments(meta.comment.text); | |
if(comments.length > 0) { | |
// console.log(comments) | |
comments.forEach(tag => { | |
if(allowedTags.includes(tag)) { | |
tags.push(tag) | |
} | |
}); | |
} | |
if(tags.length > 0) { | |
setTags(filepath, tags); | |
console.log(file); | |
} | |
} | |
} | |
} | |
// @TODO: throw error if no meta exists | |
} | |
function cleanFilename(file) { | |
let currFile = file; | |
const ext = getFileExtension(file) | |
function removeUnicodeArrowFromFilename(file) { | |
const unicodeArrow = /\u{25BA}/u; // ► | |
if(file.match(unicodeArrow)) { | |
currFile = file.split(unicodeArrow)[1].trim() | |
} | |
} | |
function removeLabelAndKeyFromFilename(file) { | |
if(file.includes('[')) { | |
currFile = file.split('[')[0].trim() + "." + ext; | |
} | |
} | |
removeUnicodeArrowFromFilename(currFile) | |
removeLabelAndKeyFromFilename(currFile) | |
console.log('✅', currFile) | |
fs.rename(path.join(directoryPath, file), path.join(directoryPath, currFile), (err) => { | |
if(err) throw err; | |
}); | |
} | |
function iterateOverAudioFilessAndCleanFilename() { | |
if(err) return console.log('Unable to scan directory' + err); | |
files.forEach((file) => { | |
const ext = getFileExtension(file) | |
if (ext === 'mp3' || ext === 'wav' || ext === 'flac') { | |
const filepath = path.join(directoryPath, file); | |
cleanFilename(file) // removes ascii play symbol + label and key from filename | |
} | |
}); | |
} | |
function iterateOverAudioFilessAndAddOSTagsFromComments() { | |
if(err) return console.log('Unable to scan directory' + err); | |
files.forEach((file) => { | |
const ext = getFileExtension(file) | |
if (ext === 'mp3' || ext === 'wav' || ext === 'flac') { | |
const filepath = path.join(directoryPath, file); | |
const meta = NodeID3.read(filepath) | |
parseMetaAndSetOSTag(meta, allowedTags) // set OSX tags for each file | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment