Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3640

How to extract Youtube Video transcripts using Youtube API on Python

$
0
0

I am trying to extract Youtube Video transcripts using Youtube API or other Python package.

I found the code on google and tried it.

# importing the modulefrom youtube_transcript_api import YouTubeTranscriptApi# retrieve the available transcriptstranscript_list = YouTubeTranscriptApi.list_transcripts('pxiP-HJLCx0')# iterate over all available transcriptsfor 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 listtranscript = transcript_list.find_transcript(['en'])# or just filter for manually created transcriptstranscript = transcript_list.find_manually_created_transcript(['en'])# importing modulesfrom youtube_transcript_api import YouTubeTranscriptApi# using the srt variable with the list of dictionaries# obtained by the .get_transcript() functionsrt = YouTubeTranscriptApi.get_transcript("pxiP-HJLCx0")# creating or overwriting a file "subtitles.txt" with# the info inside the context managerwith open("subtitles.txt", "w") as f:        # iterating through each element of list srt    for i in srt:        # writing each element of srt on a new line        f.write("{}\n".format(i))

It does extract transcripts but problem is that it contains not just text but also start and duration time. Since there are no punctuation markts, the sentences are not separated, and it appears as if the words are just listed in a row, making it difficult for text analysis.

{'text': "in this video I'm going to tell you the", 'start': 0.0, 'duration': 4.74}{'text': 'best laptops for students now for this', 'start': 2.159, 'duration': 5.16}{'text': 'one my team and I went absolutely nuts', 'start': 4.74, 'duration': 4.68}{'text': 'we got in pretty much every viable', 'start': 7.319, 'duration': 4.801}{'text': "student laptop think I'm joking I am not", 'start': 9.42, 'duration': 6.299}{'text': 'we tested an epic 15 laptops everything', 'start': 12.12, 'duration': 5.52}

How can I fix this problem? Thank you


Viewing all articles
Browse latest Browse all 3640

Trending Articles