Youtube Subtitle Extractor using Python
In this article, I am going to explain the steps involved in transcript subtitles from youtube in a text file.
Sometimes, we feel it's better to read a piece of documentation instead of watching a YouTube video to get information. So, this article might be useful for you to transcript the subtitles from a YouTube video in a text file.
Installation
Using the command below, you can install the python API, allowing you to get the transcript/subtitles for a given YouTube video.
pip install youtube_transcript_api
Procedure
Firstly import YouTubeTranscript from youtube_transcript.
from youtube_transcript_api import YouTubeTranscriptApi
Store subtitles obtained by the .get_transcript() function in a variable named srt as a list of dictionaries.
srt = YouTubeTranscriptApi.get_transcript("kvTV2ZmVpmg&t=32s")
Creating or overwriting a file "subtitles.txt" with the info inside the context manager and then it iterates through each element of list srt. Finally, it writes each element of srt on a new line in the “subtitles.txt” text file
with open("subtitles.txt", "w") as f:
for i in srt:
f.write("{}\n".format(i))
Successfully we’ve transcripted subtitles from YouTube in a text file.
Thanks for reading!!