-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautocollect_subs.py
108 lines (75 loc) · 2.87 KB
/
autocollect_subs.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import subprocess
import glob as glob
from sys import argv
directory=os.path.dirname(os.path.realpath(__file__))
os.chdir(directory) # change working directory to path of this script file
# make sure these exist will be called with subprocess
path_ffmpeg='ffmpeg.exe'
path_subfixer='subtitle-overlap-fixer.exe'
path_youtube_dl='youtube-dl.exe'
neededfiles=[path_ffmpeg,path_subfixer,path_youtube_dl]
for file in neededfiles:
if os.path.isfile(file):
print(file+" found.")
else:
print(file+" missing! aborting.")
exit()
print ("all needed files present")
#these folder names can be changed
folder_vtt="vtt_raw"
folder_srt_raw="srt_raw"
folder_srt_fixed="srt_fixed"
folder_infojson="json_info"
print(directory)
print(argv)
if len(argv)==1:
url = input("please provide url:")
if len(argv)==2:
url = argv[1]
# you can change the naming convention of the files as long as the file endings stay at the end"
# consider changing the download options of the subtitles eg ' '--write-auto-sub', '--sub-lang', 'en', '
callline=[path_youtube_dl, '--write-auto-sub', '--all-subs', '--sub-format', 'vtt', '--skip-download', '-o', "%(upload_date)s-%(uploader_id)s-%(id)s", '--write-info-json', '--skip-download',url]
# create folders if not presen
folders=[folder_vtt,folder_srt_raw,folder_srt_fixed,folder_infojson]
for path in folders:
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed, probalby allready there" % path)
else:
print ("Successfully created the directory %s " % path)
# dowload subtitles
subprocess.call(callline)
# move info json in correct folder
for file in glob.glob('*.info.json'):
try:
os.replace(file,folder_infojson+"/" +file)
except OSError as e:
print(e)
# move vtt in correct folder
for file in glob.glob('*.vtt'):
try:
os.replace(file,folder_vtt+"/" +file)
except OSError as e:
print(e)
# convert subs using ffmpeg
for raw_sub_file in glob.glob(folder_vtt+'/*.vtt'):
if os.name == 'nt':
#print(raw_sub_file)
subprocess.call([path_ffmpeg, '-i', raw_sub_file, folder_srt_raw+"\\"+raw_sub_file[len(folder_vtt):-4]+".srt"])
else:
subprocess.call(['ffmpeg', '-i', raw_sub_file, folder_srt_raw+"\\"+raw_sub_file[len(folder_vtt):-4] + ".srt"])
for file in glob.glob(folder_srt_raw+'/*.srt'):
if os.name == 'nt':
print(file)
subprocess.call([path_subfixer,file])
else:
subprocess.call(['subtitle-overlap-fixer',file])
#move fixed srt files
for file in glob.glob(folder_srt_raw+'/*.fixed.srt'):
print(file)
try:
os.replace(file,folder_srt_fixed+"\\" +file[len(folder_srt_raw):])
except OSError as e:
print(e)