質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.32%
YouTube

YouTubeとはユーザーがビデオをアップロード・共有・閲覧できるビデオ共有ウェブサイトです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

154閲覧

クラウド上からyoutube-transcript-apiを実行したい

T-N-

総合スコア13

YouTube

YouTubeとはユーザーがビデオをアップロード・共有・閲覧できるビデオ共有ウェブサイトです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

1クリップ

投稿2025/03/11 22:19

実現したいこと

クラウド上(Renderを使用予定)から、指定したYoutube動画の字幕を取得したいです。
Ruby On RailsからPythonで作成したスクリプトを呼びだすことで実現しようとしています。

現在はCodespaces上でRailsの環境を作成し、そこで作成をしています。

発生している問題・分からないこと

字幕取得部分でエラーが発生します。
エラー文を読む限り、Youtubeはクラウドからのリクエストをブロックしているため、処理ができないのではないかと思っています。

エラーメッセージ

error

1Started POST "/translate" for 10.240.2.99 at 2025-03-11 22:08:10 +0000 2Cannot render console from 10.240.2.99! Allowed networks: 127.0.0.0/127.255.255.255, ::1 3Processing by HomeController#translate as HTML 4 Parameters: {"youtube_url"=>"https://www.youtube.com/watch?v=xUMlxinV-MA"} 5Traceback (most recent call last): 6 File "/workspaces/translation-youtube/translation_youtube/lib/python_scripts/python_scripts.py", line 51, in <module> 7 print(getSubtitlesWithApi()) 8 ^^^^^^^^^^^^^^^^^^^^^ 9 File "/workspaces/translation-youtube/translation_youtube/lib/python_scripts/python_scripts.py", line 36, in getSubtitlesWithApi 10 transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ja']) 11 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 12 File "/home/codespace/.python/current/lib/python3.12/site-packages/youtube_transcript_api/_api.py", line 300, in get_transcript 13 cls.list_transcripts(video_id, proxies, cookies) 14 File "/home/codespace/.python/current/lib/python3.12/site-packages/youtube_transcript_api/_api.py", line 201, in list_transcripts 15 return ytt_api.list(video_id) 16 ^^^^^^^^^^^^^^^^^^^^^^ 17 File "/home/codespace/.python/current/lib/python3.12/site-packages/youtube_transcript_api/_api.py", line 129, in list 18 return self._fetcher.fetch(video_id) 19 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 File "/home/codespace/.python/current/lib/python3.12/site-packages/youtube_transcript_api/_transcripts.py", line 349, in fetch 21 self._extract_captions_json(self._fetch_video_html(video_id), video_id), 22 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 23 File "/home/codespace/.python/current/lib/python3.12/site-packages/youtube_transcript_api/_transcripts.py", line 363, in _extract_captions_json 24 self._assert_playability(video_data.get("playabilityStatus"), video_id) 25 File "/home/codespace/.python/current/lib/python3.12/site-packages/youtube_transcript_api/_transcripts.py", line 382, in _assert_playability 26 raise RequestBlocked(video_id) 27youtube_transcript_api._errors.RequestBlocked: 28Could not retrieve a transcript for the video https://www.youtube.com/watch?v=BzJHh5IZV2o! This is most likely caused by: 29 30YouTube is blocking requests from your IP. This usually is due to one of the following reasons: 31- You have done too many requests and your IP has been blocked by YouTube 32- You are doing requests from an IP belonging to a cloud provider (like AWS, Google Cloud Platform, Azure, etc.). Unfortunately, most IPs from cloud providers are blocked by YouTube. 33 34There are two things you can do to work around this: 351. Use proxies to hide your IP address, as explained in the "Working around IP bans" section of the README (https://github.com/jdepoix/youtube-transcript-api?tab=readme-ov-file#working-around-ip-bans-requestblocked-or-ipblocked-exception). 362. (NOT RECOMMENDED) If you authenticate your requests using cookies, you will be able to continue doing requests for a while. However, YouTube will eventually permanently ban the account that you have used to authenticate with! So only do this if you don't mind your account being banned! 37 38If you are sure that the described cause is not responsible for this error and that a transcript should be retrievable, please create an issue at https://github.com/jdepoix/youtube-transcript-api/issues. Please add which version of youtube_transcript_api you are using and provide the information needed to replicate the error. Also make sure that there are no open issues which already describe your problem! 39 Rendering text template 40 Rendered text template (Duration: 0.0ms | Allocations: 10) 41Completed 200 OK in 1099ms (Views: 1.1ms | ActiveRecord: 0.0ms | Allocations: 584)

該当のソースコード

Python

1def getSubtitlesWithApi(): 2 # 対象のYouTube動画のIDを指定 3 video_id = 'BzJHh5IZV2o' 4 5 # 日本語の字幕を取得 6 transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ja']) 7 8 # 字幕データを表示 9 print(transcript) 10 11 # 字幕テキストを結合して一つの文字列にする 12 transcript_text = ' '.join([item['text'] for item in transcript]) 13 14 # 加工後のテキストを表示 15 return "Subtitles" + transcript_text

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

特に有用は結果は得られませんでした。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

juner

2025/03/12 02:05 編集

ブロックされているのは クラウドからの リクエスト というよりも、クラウドからの視聴ですかね。(YouTubeAPI 自体はクラウドからでも使うことはできるが、今回のAPIは視聴中に使うことができる非公開APIなので)
T-N-

2025/03/12 11:11

コメントいただきありがとうございます。 字幕の取得は、視聴中のAPIでしか行えないということでしょうかね。
juner

2025/03/12 23:08

事実上はそうですが、そういう視聴中以外の利用を想定していない【非公開API】だからでしょうね。
T-N-

2025/03/14 00:19

そうなのですね。。 他の方法を考えます。 ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.32%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問