-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtt2text.py
55 lines (44 loc) · 1.49 KB
/
vtt2text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re
import datetime
import sys
import youtube_dl
import os
class Regexes:
timeRegex = r"\d\d\:\d\d\:\d\d\.\d\d\d\s-->\s\d\d\:\d\d\:\d\d\.\d\d\d(.*)"
titleRegex = r"WEBVTT\nKind: captions\nLanguage:.*\n"
twoLineRegex = r"\n\n"
unRegex1 = r"(\s)+\n"
unRegex2 = r"\n"
all = [timeRegex, titleRegex, twoLineRegex, unRegex1, unRegex2]
def download_subs(url, parent_folder):
youtube_dl_options = {
'writesubtitles': True,
'writeautomaticsub': True,
'youtube_include_dash_manifest': False,
'skip_download': True,
'outtmpl': f"{parent_folder}/%(title)s.%(ext)s"
}
with youtube_dl.YoutubeDL(youtube_dl_options) as youtube_dl_client:
title = youtube_dl_client.extract_info(url)["title"]
youtube_dl_client.download([url])
return title
def substitute(path):
with open(path, "r") as f:
content = f.read()
for regex in Regexes.all:
content = re.sub(regex, "", content)
return content.strip().split("\n")[0]
def overwrite(content, path):
current_datetime = datetime.datetime.now().strftime("%d-%m-%Y")
new_filename = f"{path[:len(path) - 7]}-{current_datetime}.txt"
with open(new_filename, "x") as f:
f.write(content)
def main():
url = "https://youtu.be/GBvlMvivCnI"
lang = "en"
parent_folder = os.getcwd()
title = download_subs(url, parent_folder)
path = f"{parent_folder}/{title}.{lang}.vtt"
content = substitute(path)
overwrite(content, path)
main()