Last active
June 25, 2023 22:42
-
-
Save kentcdodds/a8111ff0636e669222c1bb8aee8269f7 to your computer and use it in GitHub Desktop.
Update mp3 ID3 tags for general conference talks
This file contains 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
import path from 'node:path' | |
import assert from 'node:assert' | |
import * as fs from 'node:fs/promises' | |
// from scraping: https://www.churchofjesuschrist.org/study/general-conference/speakers/dallin-h-oaks?lang=eng | |
// with: | |
/* | |
function toDate(string) { | |
const [month, year] = string.split(' ') | |
return `${year}-${month.includes('Apr') ? '04' : '10'}-30` | |
} | |
copy($$('.sc-omeqik-2').reverse().map(n => ({date: toDate(n.children[0].textContent), title: n.children[1].textContent}))) | |
*/ | |
import data from './raw.json' assert { type: 'json' } | |
import * as mm from 'music-metadata' | |
import NodeID3 from 'node-id3' | |
// from downloading all the files for each talk on | |
// https://www.churchofjesuschrist.org/study/general-conference/speakers/dallin-h-oaks?lang=eng | |
const mp3Files = await fs.readdir('./mp3s') | |
assert.strictEqual(data.length, mp3Files.length) | |
const start = 0 | |
const iterations = mp3Files.length | |
// const iterations = start + 1 | |
for (let index = start; index < iterations; index++) { | |
const file = mp3Files[index] | |
const d = data[index] | |
const filepath = path.join('./mp3s', file) | |
const meta = await mm.parseFile(filepath) | |
const isTitleTheSame = | |
normalizeTitle(meta.common.title) === normalizeTitle(d.title) | |
const tags = {} | |
// year === date 🙃 | |
const currentDate = meta.native['ID3v2.3']?.find(i => i.id === 'TXXX:year') | |
if (!currentDate) { | |
tags.date = d.date | |
tags.year = d.date | |
tags.userDefinedText = [ | |
{ | |
description: 'year', | |
value: d.date, | |
}, | |
] | |
} | |
if (!isTitleTheSame) { | |
tags.title = d.title | |
} | |
if (!meta.common.picture) { | |
tags.image = './art.jpg' | |
} | |
if (Object.entries(tags).length === 0) { | |
console.log('no need to update', filepath) | |
continue | |
} | |
console.log('updating', filepath, tags) | |
NodeID3.update(tags, filepath) | |
} | |
function normalizeTitle(title) { | |
if (!title) return '' | |
return title.replace(/“/g, '"').replace(/”/g, '"').replace(/’/g, "'") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment