-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
52 lines (36 loc) · 1.69 KB
/
base.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
import convert
import sources
import subprocess
from pathlib import Path
from unidecode import unidecode
# youtube-dl - download the subtitles as .vtt files
urls_yt = sources.urls_yt()
for url in urls_yt:
subprocess.run(['youtube-dl', '-i', '--skip-download', '--write-auto-sub', '-o%(title)s %(id)s', url])
# ffmpeg - convert from .vtt to .srt
for file in [x for x in Path(__file__).parent.glob('**/*.vtt') if x.is_file()]:
subprocess.run('ffmpeg.exe -i "' + str(file.name) + '" -- "' + str(file.name[:-4]) + '".srt')
# unidecode - replace some characters if present
unidecoded_files = set()
for file in [x for x in Path(__file__).parent.glob('**/*.srt') if x.is_file()]:
with open(file, 'r', encoding='utf-8') as file_object:
data = file_object.read()
for line in data:
if line != unidecode(line):
data = data.replace(line, unidecode(line))
unidecoded_files.add(str(file.name))
with open(file, 'w', encoding='utf-8') as file_object:
file_object.write(data)
# convert - convert from .srt to .cnt
convert.convert()
# delete .vtt files
for file in [x for x in Path(__file__).parent.glob('**/*.vtt') if x.is_file()]:
file.unlink()
# delete .srt files
for file in [x for x in Path(__file__).parent.glob('**/*.srt') if x.is_file()]:
file.unlink()
# information
print(str(len(urls_yt)) + ' subtitle files requested')
print(str(len([x for x in Path(__file__).parent.glob('**/*.vtt') if x.is_file()])) + ' .vtt subtitle files in this folder')
print(str(len([x for x in Path(__file__).parent.glob('**/*.srt') if x.is_file()])) + ' .srt subtitle files in this folder')
print(str(len(unidecoded_files)) + ' subtitle files unidecoded')