Skip to content

Effort free video editing!

Feed the Japanese parakeet short, joined segments

WyattBlue56adb4f98988
diff --git a/src/cli.nim b/src/cli.nim
index ed4ad519..5bfe6e73 100644
--- a/src/cli.nim
+++ b/src/cli.nim
@@ -115,7 +115,7 @@ const whisperOptions*: seq[OptDef] = @[
OptDef(names: "-tr, --translate", kind: Flag, datum: coTranslate,
help: "Translate from source language to english"),
OptDef(names: "--queue", datum: coQueue, metavar: "SECS",
- help: "The maximum size in seconds of a single transcribed segment (default 30, or 120 with parakeet models)"),
+ help: "The maximum size in seconds of a single transcribed segment (default 30; 120 with parakeet models, 8 with the Japanese one)"),
OptDef(names: "--prompt", datum: coPrompt, metavar: "TEXT",
help: "Bias transcription toward given vocabulary/spelling (e.g. names, jargon)"),
OptDef(names: "--threads", datum: coThreads, metavar: "N",
diff --git a/src/transcribe.nim b/src/transcribe.nim
index a4a89100..45f8c197 100644
--- a/src/transcribe.nim
+++ b/src/transcribe.nim
@@ -207,11 +207,21 @@ proc whisperLanguage*(language: string): string =
func isParakeetModel*(model: string): bool =
"parakeet" in model.extractFilename.toLowerAscii
+func isShortFormParakeet(model: string): bool =
+ ## Whether `model` is the Japanese parakeet (parakeet-tdt_ctc-0.6b-ja). It
+ ## was trained on short clips of unbroken speech: over a longer segment, or
+ ## one with pauses in it, its decoder goes blank and whole sentences are
+ ## lost. NVIDIA's own runtime does the same.
+ isParakeetModel(model) and
+ "ja" in model.extractFilename.toLowerAscii.split({'-', '_', '.'})
+
func defaultQueue*(model: string): int =
## The longest segment, in seconds, to hand `model` at once. Whisper reads
## 30 second windows; parakeet's encoder takes the whole segment, and fewer
## segments mean fewer cuts through running speech.
- if isParakeetModel(model): 120 else: 30
+ if isShortFormParakeet(model): 8
+ elif isParakeetModel(model): 120
+ else: 30
# Apple SpeechAnalyzer backend (src/ae_speech.swift), selected with model "apple".
when defined(appleSpeech):
@@ -241,6 +251,7 @@ type OutCtx = object
splitWords: bool
index: int
pieces: seq[Piece] # where the segment being transcribed sits in the source
+ segLen: int # its length in samples; models round a last cue's end past it
# A whisper decoder stuck on a line emits it again in back-to-back cues, so
# the latest cue is held back until the next one shows it isn't a repeat.
mergeRepeats: bool
@@ -278,9 +289,10 @@ proc emit(oc: ptr OutCtx, text: string, t0ms, t1ms: int64) =
# A cue that ends where a piece does ends with that piece's last sample,
# not at the start of the next piece, which is a dropped silence later.
- let startMs = sourceSample(oc.pieces, t0ms * Rate div 1000) * 1000 div Rate
- let endMs = max(startMs,
- (sourceSample(oc.pieces, max(t1ms * Rate div 1000, 1) - 1) + 1) * 1000 div Rate)
+ let t0 = min(t0ms * Rate div 1000, oc.segLen)
+ let t1 = clamp(t1ms * Rate div 1000, 1, max(oc.segLen, 1))
+ let startMs = sourceSample(oc.pieces, t0) * 1000 div Rate
+ let endMs = max(startMs, (sourceSample(oc.pieces, t1 - 1) + 1) * 1000 div Rate)
if not oc.mergeRepeats:
writeCue(oc, s, startMs, endMs)
elif oc.held and s == oc.heldText and startMs - oc.heldEndMs <= RepeatGapMs:
@@ -327,6 +339,7 @@ proc whisperWorker(a: ptr WorkerArgs) {.thread.} =
if job.samples.len == 0: break
let startMs = sourceSample(job.pieces, 0) * 1000 div Rate
a.oc.pieces = move job.pieces
+ a.oc.segLen = job.samples.len
if a.debug:
stderr.writeLine(&"transcribing {job.samples.len} samples at {startMs} ms")
var cues: seq[Cue]
@@ -481,8 +494,10 @@ proc run*(readPacket: PacketSource, audioStream: ptr AVStream,
var pieces: seq[Piece]
# A model handed one or two words has too little audio to recognize them or
# their language, so utterances are joined, minus the silence between them,
- # until a segment is full. A live transcript can't wait for that.
- let pack = not (live or useApple or useParakeet)
+ # until a segment is full. A live transcript can't wait for that. Parakeet
+ # takes long segments with their pauses, except the short-form model.
+ let pack = not (live or useApple) and
+ (not useParakeet or isShortFormParakeet(model))
proc flushSeg() =
# Hand the utterance to the worker and keep capturing; blocks only if the