Creating Whisper Video Captions: OpenAI VTT
Tags: cpp, a11y, ai
Published: 2023-03-28 11:57:55

---
title: "Creating Whisper Video Captions: OpenAI VTT"
published: "true"
description: "Creating Whisper video captions 📺 how you can generate video transcription files for free locally with OpenAI trained transcription 🤖 tech."
tags: "cplusplus, accessibility, ai"
canonical_url: "https://rodneylab.com/creating-whisper-video-captions/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0a9d4a8sj2pqc217he9.png"
---

## 🤫 What is Whisper?

In this post, we see how you can start creating Whisper video captions on your local machine, **without a GPU**.  Before we turn to that, there are a few terms to unwrap in there!  **Closed captions** are the text transcriptions which appear superimposed on videos.  They are fantastic for accessibility, and you should always try to include them.  You will doubtless be aware of recent advances in open source **artificial intelligence** (**AI**).  ChatGPT has recently stolen the spotlight, but before that we had the DALL-E, Midjourney, and Stable Diffusion AI image generation tools.  With so much hype surrounding those, you might have missed Whisper.

**Whisper** from OpenAI generates transcriptions from spoken audio in **English and other languages**.  You use Whisper&rsquo;s trained language models for transcription and can even translate audio in one language to a transcription in another.  Whisper represents a step change in speed and fidelity compared to other contemporary models.  YouTube offers a free service, which to honest I thought was marvellous. That was until I tried Whisper!  All models will get the odd word wrong, especially in an obscure context.  I noticed Whisper did this far less frequently than YouTube.  On top, for longer videos, I often had a long wait for the YouTube closed caption track to be generated.

## Where do GPUs come into it?

It turns out GPUs, the tech for generating realistic graphics on your games console, are also pretty good at machine learning tasks.  Training machine learning models as well as running them on a GPU can drastically cut the time needed for these tasks, compared to a CPU (the traditional principal processor in computers).

You do not need to buy an expensive GPU just to run your transcription! Services like <a href="https://www.paperspace.com/">Paperspace</a> offer (free and paid) GPU compute. This lets you run the <a href="https://github.com/openai/whisper">Whisper Python model</a> for example in web-based Jupyter notebooks.  That is exactly how I first used Whisper.  However, we will see, if your audio is not too long (or alternatively, you are patient) you can run C++ code locally on your CPU.  If you want to see how, read on!

## 🖥️ Running Whisper C++ on your local CPU

<a href="https://github.com/ggerganov/whisper.cpp">whisper.cpp</a> is a lightweight C++ implementation, by Georgi Gerganov, of the original Whisper Python model.  It is optimized to run on Apple Silicon processors, but also runs on Intel processors.  The app is CPU intensive, so not ideal for running on your 15-year-old laptop, already on its last legs!  To follow this guide, you need to be comfortable running code in the Terminal.  It is not essential that you know C++ but some previous experience compiling C++ code would help make setting things up a little easier.

## 🧱 What are we Building?

We will construct a pipeline for transforming MP4 video into **Web Video Text Track** closed caption files (WebVTT also **VTT**). You can upload VTT to YouTube, Mux or other services along with your video.

The steps we are going to follow:

1. Convert MP4 video into MP3 audio.
2. Convert the MP3 audio into wav (whisper.cpp currently only supports wav input).
3. Use whisper.cpp to generate a VTT file.
4. Manually correct the generated VTT file.

## ⚙️ Compiling whisper.cpp

Before you can use whisper.cpp, you need to clone the repo and compile the C++ code into a binary.  We use CMake to help build the binary.  <a href="https://cmake.org/">CMake is cross-platform tooling</a> useful when working with C++.  It generates a <a href="https://en.wikipedia.org/wiki/Make_file">make file</a>, setting compiler paths for any third-party libraries.  On macOS, you can install CMake with Homebrew.  We will also need to have <a href="https://github.com/ffmpeg/ffmpeg">FFmpeg</a> installed locally, so let&rsquo;s feed two birds with one scone!

```shell
brew install cmake ffmpeg
```

Then we can clone the whisper.cpp repo and build and compile the app from the Terminal:

```shell
git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cpp
mkdir build
cd build
cmake ..
make
```

If all went well, the executable app file will be at `build/bin/main`.  You can keep it there, while you are still testing and playing around. 

### Model Download

Whisper itself has various models.  Some are ported to whisper.cpp. These models differ in their sophistication and the languages they specialize in.  For **English** I found the **medium** model (**`medium.en`**) is very accurate and also runs in reasonable time.  If you need something quicker but potentially less accurate, try the small or tiny models.  We need to download the model *a priori*.  To download `medium.en` run:

```shell
sh ../models/download-ggml-model.sh medium.en
```

Open up the `models` directory of the repo to see a full list of available models.  That&rsquo;s all the setup out of the way, so next we can see how to create the captions.

## ☑️ Creating and Correcting the Whisper Captions

1. This step is for convenience; you will run your manual corrections (in the final step) against the MP3 generated here. We generate a wav file in the next step though for whisper.cpp to use as its input.
    ```shell
    ffmpeg -i video.mp4 -vn -codec:a libmp3lame -q 2 \
  video-audio.mp3
    ```
    Here <code>video.mp4</code> is the filename for our original video in MP4 format and <code>video-audio.mp3</code> is an audio track in MP3 format which FFmpeg generates for us.

2. At the time of writing, whisper.cpp only accepts 16k bitrate, mono (as opposed to stereo)audio input. This is a little more restrictive than the Python Whisper model. However, conversion is straightforward from the Terminal using FFmpeg.
    ```shell
    ffmpeg -i video-audio.mp3 -ar 16000 -ac 1 \
  -c:a pcm_s16le video-audio.wav
    ```
    Here <code>video-audio.mp3</code> is the input MP3 audio file and <code>video-audio.wav</code> is the output wav which FFmpeg generates for us.
3. Generate captions with whisper.cpp.
    ```shell
    ./bin/main -f video-audio.wav -ovtt \
  -m ../models/ggml-medium.en.bin
    ```
    <code>-ovtt</code> tells the whisper.cpp app we want VTT output, and the <code>-m</code> flag tells the app which model to use. Update the path here if you downloaded a different model earlier. whisper.cpp will output a <code>jfk.wav.vtt</code> file with the captions. You will need this in the next step.

4. The whisper.cpp output VTT will typically need some manual corrections, like correcting some name spellings, as an example. <a href="https://www.happyscribe.com/subtitle-tools/online-subtitle-editor/free">Happy Scribe</a> is a free, online tool which syncs the VTT track to the playback, making manual corrections a breeze. Add your VTT file using the dialogue which appears when the page first loads. You will see it loaded in the bottom half of the screen with timestamps. This is the editor. Next, in the top half, click <strong>Select video file</strong>. This will accept MP3. The editor is fairly intuitive. If you click a caption, playback jumps automatically to that point in the track. When you are happy with edits, click the download icon in the top right corner, and you can save the corrected captions in SRT or VTT format.

![Creating Whisper Video Captions: Screen capture shows input dialogue for uploading a V T T or S R T file on the Happy Scribe site`](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wawq678gf5x59exegtqr.png)

## 🙌🏽 Creating Whisper Video Captions: Wrapping Up

In this post, had a brief look at how to get going on creating Whisper video captions.  In particular, we saw :
- that **whisper.cpp is an alternative to Whisper Python** and can run without a GPU
- how to **compile and build whisper.cpp**
- how you can use **Happy Scribe manually to correct** AI generated captions


We skimmed over some detail in the post, so please let me know if any aspect could do with more clarification.

## 🙏🏽 Creating Whisper Video Captions: Feedback

Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please <a aria-label="Support Rodney Lab via Buy me a Coffee" href="https://rodneylab.com/giving/">consider supporting me through Buy me a Coffee</a>.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via <TwitterMessageLink>@askRodney</TwitterMessageLink> on Twitter, <a href="https://toot.community/@rodney">@rodney@toot.community</a> on Mastodon and also the <a aria-label="Contact Rodney Lab via Telegram" href="https://matrix.to/#/%23rodney:matrix.org">#rodney</a> Element Matrix room. Also, see <a aria-label="Get in touch with Rodney Lab" href="https://rodneylab.com/contact/">further ways to get in touch with Rodney Lab</a>. I post regularly on <a aria-label="See posts on Astro" href="https://rodneylab.com/tags/astro/">Astro</a> as well as <a aria-label="C++: open C++ post list" href="https://rodneylab.com/tags/c++/">C++</a>. Also, <a aria-label="Subscribe to the Rodney Lab newsletter" href="https://newsletter.rodneylab.com/issue/latest-issue">subscribe to the newsletter to keep up-to-date</a> with our latest projects.