実現したいこと
クラウド上(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等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
特に有用は結果は得られませんでした。
補足
特になし
あなたの回答
tips
プレビュー