Skip to content

Setup to use whisper‐ctranslate2 instead of whisper.cpp

Imran Khan edited this page Nov 6, 2023 · 1 revision

To use anything other than whisper.cpp, you have to override whisper-command function to return the correct command to use instead. Here is an idea for how to use whisper-ctranslate2 instead:

(defun whisper--ctranslate2-command (input-file)
  `("whisper-ctranslate2"
    ,@(when whisper-use-threads (list "--threads" (number-to-string whisper-use-threads)))
    "--task" ,(if whisper-translate "translate" "transcribe")
    "--model" ,whisper-model
    "--language" ,whisper-language
    "--output_dir" "/tmp/"
    "--output_format" "txt"
    ,input-file))

(advice-add 'whisper-command :override #'whisper--ctranslate2-command)

Note that in this case you might want to forego whisper.cpp automatic install and other runtime checks:

(setq whisper-install-whispercpp nil)

Additionally, you might want to do some post-processing to clean up the output. For example whisper-ctranslate2 always outputs timestamp and it looks like:

Detected language 'English' with probability 1.000000
[00:00.000 --> 00:03.880]  This is some text.
[00:07.880 --> 00:06.360]  This is more text.
Transcription results written to '/tmp' directory

To keep only the text, one could do something like:

(add-hook 'whisper-post-process-hook
          (lambda ()
            (save-excursion
              (goto-char (point-max))
              (delete-line)
              (goto-char (point-min))
              (delete-line)
              (while (not (eobp))
                (goto-char (pos-bol))
                (when (re-search-forward "\\]" (pos-eol) t 1)
                  (skip-chars-forward " " (pos-eol))
                  (delete-region (pos-bol) (point)))
                (forward-line 1)))))
Clone this wiki locally