nikkie-ftnextの日記

イベントレポートや読書メモを発信

YouTubeの文字起こし、youtube-transcript-apiで簡単に取得でき、LangChainでLLMに入力できる!

はじめに

夢見ていた nikkieです。

最近LangChainに執心ですが、今回は「こんなこともサクッとできちゃうの!?」という個人的ビッグニュースをアウトプットします🔥

目次

YouTubeの文字起こしをこんなに簡単に取得できちゃうの!?

話題の つくりながら学ぶ!AIアプリ開発入門 - LangChain & Streamlit による ChatGPT API 徹底活用 を眺めていたnikkie氏。

Chapter 07「Youtube動画の要約をしよう」を読んでいると

YouTubeの動画の文字起こし(transcript)をLangChainの入力ソースとしている!!!!

これは個人的に激アツです。
積んでた動画の数々、LLMの力で絶対消化が捗るでしょ、これ!!

素振り:LangChainでYouTube動画の文字起こしを取得する

試しに以下の動画1のtranscriptを取得してみます。

動作環境

文字起こし取得

2

>>> from langchain.document_loaders import YoutubeLoader
>>> loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=DIL82INALDI", language=["ja"])

>>> documents = loader.load()
>>> len(documents)
1
>>> type(documents)
<class 'list'>
>>> type(documents[0])
<class 'langchain.schema.document.Document'>
>>> documents[0]
Document(page_content='おはよう こんにちはお話しましょう こんにちは お話するのを楽しみにしています 何について話したいですか アニメの話がしたいです (略)', metadata={'source': 'DIL82INALDI'})

たったこれだけ!
これで文字起こしが取得できてる!!

ポイントはlanguage引数!(language=["ja"]
デフォルトでは英語("en")の文字起こしを取得しようとし3、提供されていないとNoTranscriptFoundを送出します(後述しますが、youtube-transcript-apiによります)

No transcripts were found for any of the requested language codes: ['en']

日本語の文字起こしは提供されているので、"ja"と指定しました4

ChatGPTに要約をお願い

>>> from langchain.document_loaders import YoutubeLoader
>>> loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=DIL82INALDI", language=["ja"])
>>> documents = loader.load()

>>> from langchain.chat_models import ChatOpenAI
>>> chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)

>>> from langchain.chains.summarize import load_summarize_chain
>>> summarize = load_summarize_chain(chat, chain_type="stuff", verbose=True)

>>> result = summarize.run(documents)
>>> result
'The conversation is about discussing anime, specifically the anime "お隣の天使様" (Onaji no Tenshi-sama). The anime is described as a comedy that follows angels who live in the heavens and fall to Earth, where they face various problems and interact with humans. The charm of the anime lies in its healing effect, with cute characters and a gentle storyline that warms the hearts of viewers. The anime also conveys messages about the importance of learning and connecting with humans. It is loved by people of all ages and genders. The characters in the anime are described as cute and their individuality adds to the appeal of the show. The warm human drama and heartwarming messages in the story also contribute to its healing effect. Overall, "お隣の天使様" is considered a representative healing anime that is loved by many.'

訳が英語で出てきているので、プロンプトエンジニアリングの余地ありですね5
verbose=True指定により「Write a concise summary of the following:」というプロンプトと分かりました。
いや〜、でも、YouTubeの文字起こしがたったこれだけで要約できるというのは夢が広がるな〜〜!!

youtube-transcript-api

立役者はこちらです!👏

>>> from youtube_transcript_api import YouTubeTranscriptApi
>>> transcripts = YouTubeTranscriptApi.get_transcript("DIL82INALDI", languages=["ja"])
>>> type(transcripts)
<class 'list'>
>>> len(transcripts)
58

>>> transcripts[0]
{'text': 'おはよう', 'start': 10.019, 'duration': 6.08}
>>> transcripts[1]
{'text': 'こんにちはお話しましょう', 'start': 12.42, 'duration': 3.679}
>>> transcripts[2]
{'text': 'こんにちは', 'start': 29.16, 'duration': 5.34}
>>> transcripts[3]
{'text': 'お話するのを楽しみにしています', 'start': 31.199, 'duration': 7.101}

こちらはlanguages引数(複数形)な点に注意!
動画の開始位置も合わせて取得できます(LangChainの方はtextを繋いでそうですね)。

CLIも提供されています!

% # jqをかませているのはensure_ascii=False相当の処理をするため
% youtube_transcript_api DIL82INALDI --languages ja --format json | jq '.' > transcripts.json

% # jqを使ってJSON Linesとしても保存できます
% youtube_transcript_api DIL82INALDI --languages ja --format json | jq -c '.[][]' > transcripts.jsonl

「神じゃん!」って思いますが、PyPIにはWarningが。

This code uses an undocumented part of the YouTube API, which is called by the YouTube web-client. So there is no guarantee that it won't stop working tomorrow, if they change how things work.

YouTube APIのドキュメントにない部分を使っていて、明日も動き続ける保証はないとのこと。
突然使えなくなるかもという点は考慮して使いましょう〜

終わりに

YouTubeの文字起こしを簡単に取得できることを知りました。

  • youtube-transcript-apiで取得できる!
  • LangChainのYoutubeLoaderで文字起こしをソースにして、LLMと繋いだアプリケーションが作れる!

魔法のような両ライブラリにただただ感謝です。
文字起こしが取得できると知り、わたしのAIカツ、熱く盛り上がってます!!


  1. たたーん♪ ChatGPTと"お隣の天使様"を話したのです!
  2. https://github.com/hwchase17/langchain/blob/v0.0.232/docs/extras/modules/data_connection/document_loaders/integrations/youtube_transcript.ipynb
  3. https://github.com/hwchase17/langchain/blob/v0.0.232/langchain/document_loaders/youtube.py#L149
  4. エラーメッセージでもja ("Japanese (auto-generated)")[TRANSLATABLE]がgenerateされていると案内されます
  5. からあげさんのアウトプットを参考に、句読点を追加してきれいにしたら要約結果も変わるかもしれませんね!