This repository was archived by the owner on Apr 18, 2023. It is now read-only.
forked from Secksdendfordff/Anime4K-Encoder-4.0.1-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.py
128 lines (115 loc) · 4.25 KB
/
patch.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import sys
from extract_audio import extract_audio, show_convert_audio_menu
from extract_subs import extract_subs
from mux import clean_up, mux
from utils import clear
def patch(debug: bool, input_files: "list[str]", skip_menus: dict,
outname: str):
"""
Run extract audio and extract subs on the original input files,
and finally mux the encoded file that already exists in the output
directory with the extracted subs and audio track files.
Args:
input_files: list of input media files
skip_menus: menu skipping options passed from command line
outname: output directory
"""
if not os.path.exists(outname):
print("error: output directory does not exist at {0}".format(outname))
sys.exit(-2)
if not os.path.isdir(outname):
print("error: output must be a directory".format(outname))
sys.exit(-2)
successful_inputs = {}
failed_inputs = []
if not debug:
clear()
show_convert_audio_menu(debug, skip_menus)
for input_file in input_files:
input_file_name = os.path.basename(input_file)
encoded_input_file = os.path.join(outname, input_file_name)
if not os.path.exists(encoded_input_file):
print(
"error: could not locate encoded file, skipping input file={0}".format(
input_file))
if not debug:
clear()
else:
print()
if len(successful_inputs) > 0:
print("Completed input files:\n {0}".format("\n ".join(
[os.path.basename(successful_input) for successful_input in
successful_inputs]
)))
print()
print("Processing input file={0}".format(input_file))
print("Starting mode subs")
extracted_subs = False
try:
extracted_subs = extract_subs(debug, input_file, "")
except Exception as ex:
print(ex)
extracted_subs = False
if not extracted_subs:
print(
"Failed to extract subtitles for file={0}".format(input_file)
)
print("Skipping...")
failed_inputs.append(input_file)
clean_up()
continue
print()
print("Starting mode audio".format(input_file))
extracted_audio = False
try:
extracted_audio = extract_audio(debug, input_file, "", skip_menus)
except Exception as ex:
print(ex)
extracted_audio = False
if not extracted_audio:
print(
"Failed to extract audio for file={0}".format(input_file)
)
print("Skipping...")
failed_inputs.append(input_file)
clean_up()
continue
print()
new_output = os.path.splitext(encoded_input_file)[0] + "-mux.mkv"
print("Starting mode mux for input={0}, output={1}".format(
encoded_input_file,
new_output))
try:
mux(debug, encoded_input_file, new_output)
except Exception as e:
print("Failed to mux file={0}".format(encoded_input_file))
print(e)
if os.path.isfile(new_output):
print("Deleting compiled file={0}".format(new_output))
os.remove(new_output)
failed_inputs.append(input_file)
print("Skipping...")
clean_up()
continue
os.remove(encoded_input_file)
os.rename(new_output, encoded_input_file)
new_output = encoded_input_file
print("Successfully compiled file={0}".format(new_output))
successful_inputs[input_file] = new_output
if not debug:
clear()
else:
print()
if len(successful_inputs) > 0:
print(
"All of the following input files have been compiled:")
for input_path, output_path in successful_inputs.items():
print(" {0} => {1}".format(input_path, output_path))
if len(failed_inputs) > 0:
print()
if len(failed_inputs) == len(input_files):
print("Failed to compile all input files.")
else:
print("Failed to compile the following unencoded input files:")
print("\n".join(failed_inputs))