# YouTube動画をfaster_whisperを使って高速に文字起こししよう

Source: https://satotaiki.com/YouTube%E5%8B%95%E7%94%BB%E3%82%92faster_whisper%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E9%AB%98%E9%80%9F%E3%81%AB%E6%96%87%E5%AD%97%E8%B5%B7%E3%81%93%E3%81%97%E3%81%97%E3%82%88%E3%81%86
Original source: https://note.com/satoooh/n/n1536f357a502

Content-Signal: ai-train=no, search=yes, ai-input=yes

OpenAI がリリースした、文字起こしが出来る Whisper の高速版、faster_whisper を使って、YouTube 動画を google colab で高速に文字起こしする方法を解説します。

## 条件

- Google Colab で実行する
    - ランタイム > ランタイムのタイプを変更 > ハードウェア アクセラレータを GPU にすること
- YouTube 動画のダウンロードには yt-dlp を用いる
    - https://github.com/yt-dlp/yt-dlp
- 文字起こしには faster-whisper を用いる
    - https://github.com/guillaumekln/faster-whisper
    - 公式の whisper より省メモリ、同精度で最大4倍高速

## 手順

以下の手順を google colab で実行してください。

まず、yt-dlp, faster-whisper をインストールします。

```
!pip install yt-dlp
!pip install faster-whisper
```

yt-dlp を用いて対象の動画をダウンロードします。url に youtube リンクを入れてください。

```
from yt_dlp import YoutubeDL

audio_name = "audio.mp3"  #@param {type: "string"}

ydl_opts = {
    "outtmpl": audio_name,
    "format": "bestaudio"
}

url = "https://youtu.be/0t8CH5BAgLY"  #@param {type: "string"}

with YoutubeDL(ydl_opts) as ydl:
    ydl.download([url])
```

https://youtu.be/0t8CH5BAgLY

HIKAKINさんのこの動画を文字起こししてみます。

次に、faster-whisper を用いて文字起こしをします。文字起こし結果を出力しながら、ダウンロード用のファイルへの書き込みも行います。

```
from faster_whisper import WhisperModel

model_size = "large-v2"
model = WhisperModel(model_size, device="cuda", compute_type="float16")

segments, info = model.transcribe(audio=audio_name, beam_size=5)

print(f"Detected language {info.language} with probability {info.language_probability}")

f = open("output.txt", "w", encoding="UTF-8")
for segment in segments:
    print(f"[{segment.start} -> {segment.end}] {segment.text}")
    f.write(f"{segment.text}\n")
f.close()
```

[[https://assets.st-note.com/img/1680327515399-iDnKplduPW.png]]

最後に、出来上がったファイルをダウンロードします。

```
from google.colab import files
files.download("output.txt")
```

以上で処理は完了です。煮るなり焼くなり好きに使ってください。

有料部分にはすぐに使える colab のリンクを載せておきます。
自分のドライブにコピーして使ってみてください。

#note
