-: All right, this is how to use the Whisper API
to transcribe a YouTube video.
We're going to just run this.
I'm just gonna hit command shift
or control shift if you're on a PC.
And this is gonna install OpenAI.
That's one of the things we need.
We're also going to run this as well.
This is YouTube DLP.
This is like YouTube DL,
if you guys have used that before.
But I found that it's more up to date, more frequently,
and YouTube occasionally changes the way that they stream
and that breaks YouTube DL.
I found this works, but you can also try YouTube DL,
works in a very similar way.
So I'm just gonna download this YouTube video.
This one is mine.
I own the copyright for this.
So I would say if you don't own the copyright for your video
that you're downloading, just be careful,
because you might be restricted in what you can do.
But it depends on the use case using the transcripts for,
in our case, what we're doing is
we're taking the YouTube video that I have
and we're just creating a transcript.
And we can use that transcript to post
and promote the YouTube video itself.
So while this is downloading,
this is basically just streaming
and then capturing the fragments.
I'm just gonna click here to go to OpenAI.
We need to get an API key.
So we're gonna create a new secret key.
Just gonna call this whisper.
I'm gonna delete this afterwards,
because you shouldn't share your API keys.
Cool.
So,
it looks like this is downloaded.
And actually if we look in our file system, yeah,
we can see this, the video here.
So now let's set up our API key.
Just gonna run this and then we can just paste it in there.
And that kind of saves it in the open AI library.
And now we need to, so we have this video,
and it's WebM format.
So we're gonna use ffmpeg,
which is a library for basically just changing
or manipulating video audio files.
And we're gonna use this,
we're gonna take in this video,
and then output it as like an MP3.
So you can get this file name.
Just paste this in here
and hopefully that runs.
Okay. Yeah, so that's not running, I think,
because this name is a little bit funny.
So let's just call this input.webm,
and then we'll just change this here.
Input.webm.
All right.
And this is basically just converting the video format
and stripping out just the MP3, like the, just the audio.
All right, so that's done.
And actually if we refresh we can see there's
output.mp3 here.
And now we're going to call the Whisper API.
So this is the easy part. (laughs)
I've asked it for the verbose JSON.
By default, it just gives you back the transcript,
but this will give you all the different
segments of the transcript.
So just gonna run this, you'll see what I mean.
And there is a cost to this that's very cheap, though,
negligible, like pennies, even for relatively long audio.
It shouldn't be too much of an issue.
But keep on top of your cost caps. (laughs)
Make sure that you don't rack up too much
of a fee if you're doing this for lots
of different podcasts or audio.
Wait for this to run.
(instructor sniffs)
Oh, here we go.
Now it's finished.
And look, it's a pretty big file.
So you can see it's broken into these chunks, right?
We have the start time, the end time, the actual text,
and then even the tokens and the log probability.
This is like a lot of information.
We don't need all of this information.
We can actually just get the text itself.
So if we just try this.
Yeah, here we go.
So this is the full text
and I can click there to expand it.
It's the full text of the video.
That's the full transcript,
but this doesn't have any timestamps.
What I wanted to show you is how to kinda
split out the timestamps themselves.
So I've gone through the segments.
So it's four segment in transcript segments,
and then I'm unbundling the start minutes and start seconds.
I'm using this div mod.
It's basically just divides the start number by 60
to get the number of minutes
and then the number of seconds afterwards.
So that'd be how many seconds are
left over after you divided.
And then I'm creating a timestamp.
So if I just run this,
you'll be able to see, here we go.
So the timestamp is formatted,
so there's always like a prevailing zero,
even if the minutes,
even if it's less than one minute, for example.
And same thing with the seconds as well.
Just to make the formatting more consistent.
And there we go.
We have kind of line by line transcript
with the seconds and the minutes.
All right, there's a lot you can do with this.
I think you can do real-time streaming as well.
It is open source as well as available via API.
So you could run this locally if you have this
set up on your server with a GPU.
But I think it's easier
and relatively cheap to use the API.
And now you can transcribe any audio you want.
I would say that one thing I would watch out for is
that it's not particularly good
when you have more than one speaker.
That's what I've found it.
It tends to work best for like tutorials or podcasts,
things where there's just one speaker at a time typically.