The automatic english captions extracted from youtube contains not the information in a readable form and duplicated text information.
welcome<00:00:01.790><c> my</c><00:00:02.790><c> name</c><c.colorCCCCCC><00:00:02.820><c> is</c><00:00:03.210><c> Helga</c></c><c.colorE5E5E5><00:00:03.449><c> Vieira</c><00:00:03.929><c> and</c><00:00:04.080><c> this</c></c>
00:00:04.670 --> 00:00:04.680 align:start position:0%
welcome my name<c.colorCCCCCC> is Helga</c><c.colorE5E5E5> Vieira and this
</c>
My code:
def captions_test02(url):
ydl = youtube_dl.YoutubeDL({'writesubtitles': True, 'allsubtitles': True, 'writeautomaticsub': True})
res = ydl.extract_info(url, download=False)
if res['requested_subtitles'] and res['requested_subtitles']['en']:
print('Grabbing vtt file from ' + res['requested_subtitles']['en']['url'])
response = requests.get(res['requested_subtitles']['en']['url'], stream=True)
f1 = open("testfile01.txt", "w")
f1.write(response.text)
f1.close()
if len(res['subtitles']) > 0:
print('manual captions')
else:
print('automatic_captions')
else:
print('Youtube Video does not have any english captions')
if __name__ == '__main__':
captions_test02("https://www.youtube.com/watch?v=tCTqNZW0wIk&t=2s")
Any suggestions to get a proper transcript? Starting point: https://shkspr.mobi/blog/2018/09/convert-webvtt-to-a-transcript-using-python/
To eliminate the timestamps and get a better transcript, you can use regex:
def captions_test02(url):
ydl = youtube_dl.YoutubeDL({'writesubtitles': True, 'allsubtitles': True, 'writeautomaticsub': True})
res = ydl.extract_info(url, download=False)
if res['requested_subtitles'] and res['requested_subtitles']['en']:
print('Grabbing vtt file from ' + res['requested_subtitles']['en']['url'])
response = requests.get(res['requested_subtitles']['en']['url'], stream=True)
f1 = open("testfile01.txt", "w")
new = re.sub(r'\d{2}\W\d{2}\W\d{2}\W\d{3}\s\W{3}\s\d{2}\W\d{2}\W\d{2}\W\d{3}','',response.text)
f1.write(new)
f1.close()
if len(res['subtitles']) > 0:
print('manual captions')
else:
print('automatic_captions')
else:
print('Youtube Video does not have any english captions')
if __name__ == '__main__':
captions_test02("https://www.youtube.com/watch?v=d1CDP6sMuLA")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With