-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrsubsync.py
96 lines (77 loc) · 3.64 KB
/
rsubsync.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
import fnmatch
import os
import subprocess
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--lang',
dest='language_code',
required=True,
help="Specify a single language code of the subtitles you want to sync (ie. 'es', 'en'...)")
parser.add_argument('--path',
dest='path',
default='.',
required=False,
help="Specify the path to sync. It will be '.' by default (current directory)")
args = parser.parse_args()
SETTINGS = {
"SUBSYNC_PATH": 'subsync',
"PATH_TO_SYNC": args.path,
"VIDEO_EXTENSIONS": ['.mkv', '.mp4', '.avi'],
"SUBTITLE_EXTENSION_TO_SYNC": f'.{args.language_code}.srt',
"TEMP_OUTPUT_NAME": 'output.srt',
"OLD_SUFFIX": '.old',
"FAILED_SUFFIX": '.failed'
}
def get_subtitle(root, filename):
return os.path.join(root, filename)
def get_file_base(root, filename):
return os.path.join(root, filename.split(SETTINGS['SUBTITLE_EXTENSION_TO_SYNC'])[0])
def get_matching_video_extension(filename):
video_match = None
for extension in SETTINGS['VIDEO_EXTENSIONS']:
video_file_to_check = f"{filename}{extension}"
if os.path.exists(video_file_to_check):
video_match = video_file_to_check
break
return video_match
def execute_subsync_process(video, subtitle, output_subtitle, output_file):
return subprocess.call([SETTINGS['SUBSYNC_PATH'], video, "-i", subtitle, "-o", output_subtitle], stderr=output_file)
def replace_subtitle(subtitle, output):
os.rename(subtitle,
f"{subtitle}{SETTINGS['OLD_SUFFIX']}")
os.rename(output, subtitle)
os.remove(
f"{subtitle}{SETTINGS['FAILED_SUFFIX']}")
def main():
if os.path.exists(SETTINGS['PATH_TO_SYNC']):
print(f"Starting sync process in path: {SETTINGS['PATH_TO_SYNC']}...")
for root, dirnames, filenames in os.walk(f"{SETTINGS['PATH_TO_SYNC']}"):
for filename in fnmatch.filter(filenames, f"*{SETTINGS['SUBTITLE_EXTENSION_TO_SYNC']}"):
subtitle = get_subtitle(root, filename)
file_base = get_file_base(root, filename)
video_match = get_matching_video_extension(file_base)
if video_match is not None:
if os.path.exists(f"{subtitle}{SETTINGS['OLD_SUFFIX']}"):
print("Sub already synced")
elif os.path.exists(f"{subtitle}{SETTINGS['FAILED_SUFFIX']}"):
print("This video failed previously, won't try again")
else:
try:
print(f"Starting sync process of: {filename}")
output_filename = os.path.join(
root, SETTINGS['TEMP_OUTPUT_NAME'], )
failed_file = f"{subtitle}{SETTINGS['FAILED_SUFFIX']}"
with open(failed_file, "w+") as outfile:
process_code = execute_subsync_process(video_match, subtitle,
output_filename, outfile)
if process_code != 0:
raise Exception
replace_subtitle(subtitle, output_filename)
except:
print(
f"There has been an error in the syncing process of: ${filename}")
else:
print(f"There's no video file for: {filename}")
else:
print("Specified path does not exist")
main()