ndc-content-tools/import-transcript.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-08-30 11:36:09 +00:00
const fs = require('fs')
function importTranscript(srtContent, names) {
2023-08-30 11:39:23 +00:00
const entries = srtContent.trim().split(/\n\n+/)
const nameSet = new Set(names.map(name => name.trim()))
2023-08-30 11:36:09 +00:00
2023-08-30 11:39:23 +00:00
let foundSpeakers = new Set()
2023-08-30 11:36:09 +00:00
2023-08-30 11:39:23 +00:00
const mdEntries = entries.map(entry => {
const lines = entry.split(/\n/)
if (lines.length < 3) return null
2023-08-30 11:36:09 +00:00
2023-08-30 11:39:23 +00:00
const timeParts = lines[1].split(' --> ')
if (timeParts.length !== 2) return null
2023-08-30 11:36:09 +00:00
2023-08-30 11:39:23 +00:00
let content = lines.slice(2).join(' ').trim()
for (const name of nameSet) {
const regex = new RegExp(`^${name}:`, 'i')
if (content.match(regex)) {
content = content.replace(name, `**${name}**`)
foundSpeakers.add(name)
break
}
}
2023-08-30 11:36:09 +00:00
2023-08-30 11:39:23 +00:00
return `==${timeParts[0]}==${content}==${timeParts[1]}==`
}).filter(Boolean)
2023-08-30 11:36:09 +00:00
2023-08-30 11:39:23 +00:00
nameSet.forEach(name => {
if (!foundSpeakers.has(name)) {
console.warn(`Warning: Speaker ${name} wasn't found. Did you misspell their name?`)
}
})
2023-08-30 11:36:09 +00:00
2023-08-30 11:39:23 +00:00
return mdEntries.join('\n\n')
2023-08-30 11:36:09 +00:00
}
let srtFileName, mdFileName, names = ''
for (let i = 2; i < process.argv.length; i++) {
2023-08-30 11:39:23 +00:00
switch (process.argv[i]) {
case '--input':
case '-i':
srtFileName = process.argv[++i]
break
case '--output':
case '-o':
mdFileName = process.argv[++i]
break
case '--speakers':
names = process.argv[++i]
break
}
2023-08-30 11:36:09 +00:00
}
if (!srtFileName) {
2023-08-30 11:39:23 +00:00
console.log("This utility converts valid .srt files to NDC compatible transcripts. Usage: node import-transcripts.js --input <input.srt> --output <output.md> --speakers 'Name1,Name2,...'")
process.exit(1)
2023-08-30 11:36:09 +00:00
}
const srtContent = fs.readFileSync(srtFileName, 'utf8')
const nameList = names.split(',')
const mdContent = importTranscript(srtContent, nameList)
if (mdFileName) {
2023-08-30 11:39:23 +00:00
fs.writeFileSync(mdFileName, mdContent, 'utf8')
console.log(`Converted content written to ${mdFileName}`)
2023-08-30 11:36:09 +00:00
} else {
2023-08-30 11:39:23 +00:00
console.log(mdContent)
2023-08-30 11:36:09 +00:00
}