jdepoix / youtube-transcript-api

This is a python API which allows you to get the transcript/subtitles for a given YouTube video. It also works for automatically generated subtitles and it does not require an API key nor a headless browser, like other selenium based solutions do!
MIT License
2.58k stars 284 forks source link

Could not retrieve a transcript for the video using transcript_list = YouTubeTranscriptApi.list_transcripts(video_id) #180

Closed LaserBen2023 closed 1 year ago

LaserBen2023 commented 1 year ago

I am trying to use the YouTubeTranscriptApi.list_transcripts() function to retrieve the transcript for a YouTube video, but I am receiving the following error:

  File "/Users/mac-user/Documents/youtube-transcript-test.py", line 11, in <module>
    transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/youtube_transcript_api/_api.py", line 71, in list_transcripts
    return TranscriptListFetcher(http_client).fetch(video_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/youtube_transcript_api/_transcripts.py", line 47, in fetch
    self._extract_captions_json(self._fetch_video_html(video_id), video_id)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/youtube_transcript_api/_transcripts.py", line 59, in _extract_captions_json
    raise TranscriptsDisabled(video_id)
youtube_transcript_api._errors.TranscriptsDisabled: 
Could not retrieve a transcript for the video https://www.youtube.com/watch?v='-OAa9k0zCDg'! This is most likely caused by:

Subtitles are disabled for this video

I have double-checked that subtitles are enabled for the video, and I am certain that a transcript should be available. Screenshot 2023-01-03 at 10 45 00 I am using version 0.5.0 of the youtube_transcript_api package. I have included the code I am using to reproduce the error below:

from youtube_transcript_api import YouTubeTranscriptApi

# The URL of the YouTube video
youtube_url = "https://www.youtube.com/watch?v=-OAa9k0zCDg"

# Extract the video ID from the URL
video_id = "'" + youtube_url.split("v=")[1] + "'"

# retrieve the available transcripts
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)

# iterate over all available transcripts
for transcript in transcript_list:

    # the Transcript object provides metadata properties
    print(
        transcript.video_id,
        transcript.language,
        transcript.language_code,
        # whether it has been manually created or generated by YouTube
        transcript.is_generated,
        # whether this transcript can be translated or not
        transcript.is_translatable,
        # a list of languages the transcript can be translated to
        transcript.translation_languages,
    )

    # fetch the actual transcript data
    print(transcript.fetch())

    # translating the transcript will return another transcript object
    print(transcript.translate('en').fetch())

# you can also directly filter for the language you are looking for, using the transcript list
transcript = transcript_list.find_transcript(['en'])  

# or just filter for manually created transcripts  
transcript = transcript_list.find_manually_created_transcript(['en'])  

# or automatically generated ones  
transcript = transcript_list.find_generated_transcript(['en'])

I would appreciate any help in resolving this issue. Thank you!

jdepoix commented 1 year ago

Hi @LaserBen2023, in the following line

video_id = "'" + youtube_url.split("v=")[1] + "'"

you are surrounding the video with '. However, the id of the video you are looking for is -OAa9k0zCDg and not '-OAa9k0zCDg', therefore, YouTube can't find it. Just replace that line with

video_id = youtube_url.split("v=")[1]

and it should work.

Hope this helps! I am closing this issue, as this is not an issue with the module.

LaserBen2023 commented 1 year ago

thank you for your help