from youtube_transcript_api import YouTubeTranscriptApi def getTranscript(videoId): try: srt = YouTubeTranscriptApi.get_transcript(videoId) fname = "transcripts/" + videoId + ".txt" with open(fname, "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["text"]).replace("\xa0", " ").replace(" — ", " ")) return True except: print("A transcript error has occurred") return False