Transcribe a folder into TXT and SRT
Node.js 18+, no dependencies
Point it at a folder. Every audio or video file that has no transcript next to it is uploaded, and when the transcript is ready the script writes a .txt file (with speakers, when they were recognised) and an .srt subtitle file beside the original. Files that already have a .txt are skipped, so it can run every night from cron or Task Scheduler and only pick up what is new.
It also shows the two things a real integration needs: sending a large file in ordered chunks, and reading a long transcript page by page.
TRANSCRIFY_KEY=trk_... node transcribe-folder.mjs ./recordings#!/usr/bin/env node
// Transcribe every recording in a folder.
//
// For each audio or video file that has no .txt next to it yet, this uploads
// the file, waits for the transcript and writes two files beside it:
// interview.mp3 -> interview.txt (text, with speakers when recognised)
// interview.srt (subtitles with timestamps)
// Run it again whenever new recordings appear (or from cron): finished files
// are skipped.
//
// export TRANSCRIFY_KEY=trk_...
// node transcribe-folder.mjs ./recordings
//
// Node 18 or newer, no dependencies. Scopes: files.write, files.read.
import { open, readdir, readFile, stat, writeFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { randomUUID } from 'node:crypto'
import path from 'node:path'
const API = process.env.TRANSCRIFY_API ?? 'https://api.transcrify.com'
const KEY = process.env.TRANSCRIFY_KEY
const FOLDER = process.argv[2]
const MEDIA = /\.(mp3|m4a|wav|ogg|opus|flac|aac|wma|amr|mp4|mov|mkv|webm|avi|m4v)$/i
const SINGLE_REQUEST_MAX = 45 * 1024 * 1024 // requests are limited to 50 MB
const CHUNK_SIZE = 20 * 1024 * 1024
const FAILED = ['transcription_failed', 'download_failed', 'youtube_download_failed', 'audio_extraction_failed', 'upload_failed']
if (!KEY || !FOLDER) {
console.error('Usage: TRANSCRIFY_KEY=trk_... node transcribe-folder.mjs <folder>')
process.exit(2)
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
/** One API call. Waits and repeats on 429; throws with the API's error code otherwise. */
async function api(method, route, body) {
for (;;) {
const response = await fetch(API + route, {
method,
headers: { 'X-Api-Key': KEY, ...(body && !(body instanceof FormData) ? { 'Content-Type': 'application/json' } : {}) },
body: body instanceof FormData ? body : body ? JSON.stringify(body) : undefined
})
if (response.status === 429) {
await sleep((Number(response.headers.get('Retry-After')) || 5) * 1000)
continue
}
const data = await response.json().catch(() => ({}))
if (!response.ok) {
const error = new Error(data?.error?.message ?? `HTTP ${response.status}`)
error.code = data?.error?.code
error.status = response.status
throw error
}
return data
}
}
/** Uploads a file in one request, or in ordered chunks when it is large. Returns the file id. */
async function upload(filePath) {
const name = path.basename(filePath)
const { size } = await stat(filePath)
if (size <= SINGLE_REQUEST_MAX) {
const form = new FormData()
form.append('file', new Blob([await readFile(filePath)]), name)
return (await api('POST', '/api/v1/files', form)).id
}
const totalChunks = Math.ceil(size / CHUNK_SIZE)
const uid = randomUUID()
const handle = await open(filePath, 'r')
let id
try {
for (let chunk = 1; chunk <= totalChunks; chunk++) {
const buffer = Buffer.alloc(Math.min(CHUNK_SIZE, size - (chunk - 1) * CHUNK_SIZE))
await handle.read(buffer, 0, buffer.length, (chunk - 1) * CHUNK_SIZE)
const form = new FormData()
form.append('file', new Blob([buffer]), name)
form.append('uid', uid)
if (chunk === 1) {
form.append('totalChunks', String(totalChunks))
id = (await api('POST', '/api/v1/files', form)).id
} else {
form.append('currentChunk', String(chunk))
await api('POST', '/api/v1/files/chunk', form)
}
process.stdout.write(`\r uploading ${Math.round((chunk / totalChunks) * 100)}%`)
}
process.stdout.write('\n')
} finally {
await handle.close()
}
return id
}
async function waitUntilTranscribed(id) {
for (;;) {
const file = await api('GET', `/api/v1/files/${id}`)
if (file.statusLabel === 'transcribed') return
if (FAILED.includes(file.statusLabel)) throw new Error(file.statusLabel)
await sleep(20_000)
}
}
/** The whole transcript as timed segments; long ones come in pages. */
async function readSegments(id) {
const segments = []
for (let offset = 0; offset !== null;) {
const page = await api('GET', `/api/v1/files/${id}/transcript?offset=${offset}&limit=1000`)
segments.push(...page.segments)
offset = page.nextOffset
}
return segments
}
const srtTime = (seconds) => {
const ms = Math.round(seconds * 1000)
const pad = (n, width = 2) => String(n).padStart(width, '0')
return `${pad(Math.floor(ms / 3_600_000))}:${pad(Math.floor(ms / 60_000) % 60)}:${pad(Math.floor(ms / 1000) % 60)},${pad(ms % 1000, 3)}`
}
function toText(segments) {
const lines = []
let speaker
for (const segment of segments) {
if (segment.speaker && segment.speaker !== speaker) {
speaker = segment.speaker
lines.push(`\n${speaker}:`)
}
lines.push(segment.text.trim())
}
return lines.join('\n').trim() + '\n'
}
const toSrt = segments => segments
.filter(segment => segment.start !== null && segment.end !== null)
.map((segment, i) => `${i + 1}\n${srtTime(segment.start)} --> ${srtTime(segment.end)}\n${segment.text.trim()}\n`)
.join('\n')
const withoutExtension = file => file.replace(/\.[^.]+$/, '')
// Recordings that have no transcript next to them yet.
const names = (await readdir(FOLDER))
.filter(name => MEDIA.test(name) && !existsSync(path.join(FOLDER, `${withoutExtension(name)}.txt`)))
.sort()
let done = 0
for (const name of names) {
const filePath = path.join(FOLDER, name)
const base = withoutExtension(filePath)
console.log(name)
try {
const id = await upload(filePath)
console.log(` accepted as file ${id}, transcribing...`)
await waitUntilTranscribed(id)
const segments = await readSegments(id)
await writeFile(`${base}.txt`, toText(segments))
const srt = toSrt(segments)
if (srt) await writeFile(`${base}.srt`, srt)
console.log(` saved ${path.basename(base)}.txt${srt ? ' and .srt' : ''}`)
done++
} catch (error) {
console.error(` failed: ${error.message}`)
// 401: the key is wrong. 402: the account has no allowance left.
// Nothing later would get through either.
if (error.status === 401 || error.status === 402) break
}
}
console.log(`${done} of ${names.length} new recordings transcribed.`)