Automatically Transcribe YouTube Videos with OpenAI Whisper

// #ai#openai#python#tutorial // 2 comments

OpenAI is on everyone's lips, but this is not about their recent Chatbot but about a language model for transcribing audio they released back in September. This post will show how to apply it on YouTube videos to generate a full transcript of the spoken words.

Install Dependencies

Install the Python packages for Whisper, PyTube and Pandas. Whisper should be installed from GitHub to pick up the latest commit. PyTube is available on PyPi, but it has a lot of open issues and pull requests. So installing it from GitHub allows us to cherry-pick some PRs if needed later on.

pip install git+ pip install git+ pip install pandas

Download the YouTube Video

We will use PyTube's YouTube class to download the given video_url as audio file locally. The URL must be a valid watch URL. I would suggest to use a short video (around 5 minutes) so you don't have to wait too long for the results.

from pytube import YouTube video_url = "<>" audio_file = YouTube(video_url).streams.filter(only_audio=True).first().download(filename="audio.mp4")

Load the Whisper Model

This will load the tiny Whisper language model. It's a multi-lingual model that is relatively fast. It's also available as English-only model as tiny.en. There are more language models available that are larger and more accurate.

import whisper whisper_model = whisper.load_model("tiny")

Transcribe the Video

This will run the language model on the provided audio file.

transcription = whisper_model.transcribe(audio_file)

Display the Transcription

This will display the transcription result in segments with start and end time. The full concatenated string is available as transcription['text']

import pandas as pd # print as DataFrame df = pd.DataFrame(transcription['segments'], columns=['start', 'end', 'text']) print(df) # or, print as String print(transcription['text'])

This will print the following table:

00.09.7Is there cool small projects like archive sanity and so on that you're thinking about the
19.712.96world, the ML world can anticipate?
212.9616.32There's some always like some fun side projects.
316.3217.72Archive sanity is one.
417.7221.8Basically like there's way too many archive papers, how can I organize it and recommend
521.823.2papers and so on.
623.225.8I transcribed all of your podcasts.
725.829.92What did you learn from that experience from transcribing the process?
829.9233.92Like you like consuming audiobooks and podcasts and so on.
933.9239.92Here's a process that achieves closer to human level performance and annotation.
1140.9245.92Well I definitely was surprised that transcription with opening as whisper was working so well.
1245.9250.56Compared to what I'm familiar with from Siri and like a few other systems I guess, it works
1350.5651.56so well.
1451.5656.2And that's what gave me some energy to like try it out and I thought it could be fun to
1556.257.56run on podcasts.
1657.5662.04It's kind of not obvious to me why whisper is so much better compared to anything else
1762.0464.76because I feel like there should be a lot of incentive for a lot of companies to produce
1864.7667.72transcription systems and that they've done so over a long time.
1967.7269.36Whisper is not a super exotic model.
2069.3671.16It's a transformer.
2171.1675.08It takes smell spectrograms and you know it just outputs tokens of text.
2275.0876.56It's not crazy.
2376.5679.24The model and everything has been around for a long time.
2479.2480.56I'm not actually 100% sure why.

How to Run It

I put all this code into an interactive Jupyter Notebook on Colab, so you you can try it out without having to install all of this.

Jupyter Notebook

The complete code is also available as GitHub repository, so you can simply clone it and run it locally.