(
inputPath: string,
dir: string,
opts: { model?: string; language?: string; json?: boolean; optional?: boolean },
)
| 214 | // --------------------------------------------------------------------------- |
| 215 | |
| 216 | async function transcribeAudio( |
| 217 | inputPath: string, |
| 218 | dir: string, |
| 219 | opts: { model?: string; language?: string; json?: boolean; optional?: boolean }, |
| 220 | ): Promise<void> { |
| 221 | const { transcribe } = await import("../whisper/transcribe.js"); |
| 222 | const { loadTranscript, patchCaptionHtml, stripBeforeOnset } = |
| 223 | await import("../whisper/normalize.js"); |
| 224 | |
| 225 | const model = opts.model ?? DEFAULT_MODEL; |
| 226 | const spin = opts.json ? null : clack.spinner(); |
| 227 | spin?.start(`Transcribing with ${c.accent(model)}...`); |
| 228 | |
| 229 | try { |
| 230 | const result = await transcribe(inputPath, dir, { |
| 231 | model, |
| 232 | language: opts.language, |
| 233 | onProgress: spin ? (msg) => spin.message(msg) : undefined, |
| 234 | }); |
| 235 | |
| 236 | let { words } = loadTranscript(result.transcriptPath); |
| 237 | |
| 238 | if (result.speechOnsetSeconds != null) { |
| 239 | const before = words.length; |
| 240 | words = stripBeforeOnset(words, result.speechOnsetSeconds); |
| 241 | const stripped = before - words.length; |
| 242 | if (stripped > 0 && !opts.json) { |
| 243 | spin?.message( |
| 244 | `Stripped ${stripped} words before speech onset at ${result.speechOnsetSeconds.toFixed(1)}s`, |
| 245 | ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | writeFileSync(result.transcriptPath, JSON.stringify(words, null, 2)); |
| 250 | patchCaptionHtml(dir, words); |
| 251 | |
| 252 | if (opts.json) { |
| 253 | console.log( |
| 254 | JSON.stringify({ |
| 255 | ok: true, |
| 256 | model, |
| 257 | wordCount: words.length, |
| 258 | durationSeconds: result.durationSeconds, |
| 259 | speechOnsetSeconds: result.speechOnsetSeconds, |
| 260 | transcriptPath: result.transcriptPath, |
| 261 | }), |
| 262 | ); |
| 263 | } else { |
| 264 | const onsetNote = |
| 265 | result.speechOnsetSeconds != null |
| 266 | ? ` — speech detected at ${result.speechOnsetSeconds.toFixed(1)}s` |
| 267 | : ""; |
| 268 | spin?.stop( |
| 269 | c.success( |
| 270 | `Transcribed ${c.accent(String(words.length))} words (${result.durationSeconds.toFixed(1)}s${onsetNote})`, |
| 271 | ), |
| 272 | ); |
| 273 | } |
no test coverage detected