HomePythonPython extract cc, subtitle, transcript from YouTube video - Py code example Python extract cc, subtitle, transcript from YouTube video - Py code example Makarablue June 02, 2022 0 Extract transcript using Python First you need to install youtube_transcript_api by command belowpip install youtube_transcript_apiExample :from youtube_transcript_api import YouTubeTranscriptApi import datetime import io yt_id = "LkT6W6DqyuQ" str = YouTubeTranscriptApi.get_transcript(yt_id,languages=['en-US','en']) now = datetime.datetime.now().strftime('%Y%m-%d%H-%M%S-') def getText(): str = YouTubeTranscriptApi.get_transcript(yt_id,languages=['en-US','en']) now = datetime.datetime.now().strftime('%Y%m-%d%H-%M%S-') transcript = '' index = 0 for value in str: for key,val in value.items(): if key == 'text': clean_val = val clean_val = clean_val.replace("[Music]", "") clean_val = clean_val.replace(" i ", " I ") clean_val = clean_val.replace("i ", "I ") clean_val = clean_val.replace(" i' ", " I' ") clean_val = clean_val.replace("i'm", "I'm") clean_val = clean_val.replace("[ __ ]", "") if index == 1: clean_val = clean_val[0].upper()+clean_val[1:] # if index == 25: # clean_val = clean_val+'. ' # index = 0 transcript += " "+clean_val index = index + 1 file_name = "txt/text-file.txt" file = io.open(file_name, 'w', encoding="utf-8") file.write(transcript) file.close() getText()Explanation : The example code above is using YouTubeTranscriptApi to grep transcript from YouTube by video id that will get result like image belowthen prepare that text from each line to full statement. Finally save the file to txt/text-file.txt Tags Coding Python Newer Older