V 1.1 - tqdm progress
This commit is contained in:
1 parent
5ac5b6615b
commit
ff610350fc
1 file changed
+141
-113
@@ -33,134 +33,162 @@ def mux_audio_into_output(Quality_Video, audio_file, output_dir):
|
|||||||
return final_output
|
return final_output
|
||||||
|
|
||||||
def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offset: Optional[int] = None) -> bool:
|
def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offset: Optional[int] = None) -> bool:
|
||||||
|
steps = [
|
||||||
|
"Probe video",
|
||||||
|
"Compute offset",
|
||||||
|
"Extract pre beginning",
|
||||||
|
"Extract german audio",
|
||||||
|
"Extract post end",
|
||||||
|
"Combine audio",
|
||||||
|
"Mux",
|
||||||
|
"Cleanup",
|
||||||
|
"Final check",
|
||||||
|
]
|
||||||
|
|
||||||
print("probe_video")
|
weights = [
|
||||||
# --- Probe video ---
|
1,
|
||||||
fps, duration, _, vstart = probe_video(German_Video)
|
3,
|
||||||
fps2, duration2, _, vstart2 = probe_video(Quality_Video)
|
1,
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
]
|
||||||
|
|
||||||
if duration <= 0:
|
with tqdm(total=len(steps), desc=Quality_Video.name, leave=False) as pbar:
|
||||||
raise ValueError("Duration of German_Video must be greater than 0")
|
for step, w in zip(steps, weights):
|
||||||
if duration2 <= 0:
|
pbar.set_description(step)
|
||||||
raise ValueError("Duration of Quality_Video must be greater than 0")
|
if step == "Probe video":
|
||||||
|
# --- Probe video ---
|
||||||
|
fps, duration, _, vstart = probe_video(German_Video)
|
||||||
|
fps2, duration2, _, vstart2 = probe_video(Quality_Video)
|
||||||
|
|
||||||
print("Compute offset")
|
if duration <= 0:
|
||||||
# --- Offset ---
|
raise ValueError("Duration of German_Video must be greater than 0")
|
||||||
if offset == None:
|
if duration2 <= 0:
|
||||||
offset0 = corelate_videos(Quality_Video, German_Video, fps2)
|
raise ValueError("Duration of Quality_Video must be greater than 0")
|
||||||
else:
|
|
||||||
offset0 = offset
|
|
||||||
if offset0 > duration or offset0 > duration2:
|
|
||||||
raise ValueError("Offset needs to be smaller than the video durations")
|
|
||||||
print(f"Offset: {offset0}s")
|
|
||||||
|
|
||||||
print("Extract pre beginning")
|
if step == "Compute offset":
|
||||||
german_audio_file = output_dir / f"{Path(Quality_Video).stem}_german_audio.mka"
|
# --- Offset ---
|
||||||
quality_audio_file1 = output_dir / f"{Path(Quality_Video).stem}_original_audio1.mka"
|
if offset == None:
|
||||||
quality_audio_file2 = output_dir / f"{Path(Quality_Video).stem}_original_audio2.mka"
|
offset0 = corelate_videos(Quality_Video, German_Video, fps2)
|
||||||
|
else:
|
||||||
|
offset0 = offset
|
||||||
|
if offset0 > duration or offset0 > duration2:
|
||||||
|
raise ValueError("Offset needs to be smaller than the video durations")
|
||||||
|
pbar.set_postfix_str(f"Offset: {offset0}s")
|
||||||
|
|
||||||
# Clear old faulty files, ger is gearantied to be replaced
|
if step == "Extract pre beginning":
|
||||||
if os.path.isfile(quality_audio_file1):
|
german_audio_file = output_dir / f"{Path(Quality_Video).stem}_german_audio.mka"
|
||||||
os.remove(quality_audio_file1)
|
quality_audio_file1 = output_dir / f"{Path(Quality_Video).stem}_original_audio1.mka"
|
||||||
if os.path.isfile(quality_audio_file2):
|
quality_audio_file2 = output_dir / f"{Path(Quality_Video).stem}_original_audio2.mka"
|
||||||
os.remove(quality_audio_file2)
|
|
||||||
|
|
||||||
### Get the first part till german starts (if german does not start before file)
|
# Clear old faulty files, ger is gearantied to be replaced
|
||||||
start2 = vstart2
|
if os.path.isfile(quality_audio_file1):
|
||||||
end2 = start2 + offset0
|
os.remove(quality_audio_file1)
|
||||||
if end2 - start2 > 0.05:
|
if os.path.isfile(quality_audio_file2):
|
||||||
if DEBUG: print(f"Cutting audio ({quality_audio_file1}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s")
|
os.remove(quality_audio_file2)
|
||||||
try:
|
|
||||||
extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file1)
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
raise BrokenPipeError(
|
|
||||||
f"{e}\n"
|
|
||||||
f"Trying to extract Audio from {Quality_Video} {start2}-{end2} "
|
|
||||||
f"to {quality_audio_file2} to have a duration of {end2-start2}, "
|
|
||||||
f"while the audio has a length of {get_audio_duration(Quality_Video)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Extract german audio")
|
### Get the first part till german starts (if german does not start before file)
|
||||||
|
start2 = vstart2
|
||||||
|
end2 = start2 + offset0
|
||||||
|
if end2 - start2 > 0.05:
|
||||||
|
if DEBUG: print(f"Cutting audio ({quality_audio_file1}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s")
|
||||||
|
try:
|
||||||
|
extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file1)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
raise BrokenPipeError(
|
||||||
|
f"{e}\n"
|
||||||
|
f"Trying to extract Audio from {Quality_Video} {start2}-{end2} "
|
||||||
|
f"to {quality_audio_file2} to have a duration of {end2-start2}, "
|
||||||
|
f"while the audio has a length of {get_audio_duration(Quality_Video)}"
|
||||||
|
)
|
||||||
|
|
||||||
### Get the German Audio
|
if step == "Extract german audio":
|
||||||
if offset0 < 0:
|
|
||||||
start = vstart - offset0
|
|
||||||
else:
|
|
||||||
start = vstart
|
|
||||||
end = min(vstart + duration, start + duration2)
|
|
||||||
if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s")
|
|
||||||
try:
|
|
||||||
extract_audio_segment_file(German_Video, start, end, german_audio_file)
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
raise BrokenPipeError(
|
|
||||||
f"{e}\n"
|
|
||||||
f"Trying to extract Audio from {German_Video} {start}-{end} "
|
|
||||||
f"to {german_audio_file} to have a duration of {end-start}, "
|
|
||||||
f"while the audio has a length of {get_audio_duration(German_Video)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Extract post end")
|
### Get the German Audio
|
||||||
|
if offset0 < 0:
|
||||||
|
start = vstart - offset0
|
||||||
|
else:
|
||||||
|
start = vstart
|
||||||
|
end = min(vstart + duration, start + duration2)
|
||||||
|
if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s")
|
||||||
|
try:
|
||||||
|
extract_audio_segment_file(German_Video, start, end, german_audio_file)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
raise BrokenPipeError(
|
||||||
|
f"{e}\n"
|
||||||
|
f"Trying to extract Audio from {German_Video} {start}-{end} "
|
||||||
|
f"to {german_audio_file} to have a duration of {end-start}, "
|
||||||
|
f"while the audio has a length of {get_audio_duration(German_Video)}"
|
||||||
|
)
|
||||||
|
|
||||||
### Get the end from the first original audio if it is longer than the german part
|
if step == "Extract post end":
|
||||||
start2 = vstart2 + end
|
|
||||||
# end2 = start2 + (duration2 - duration - (n1 / fps))
|
|
||||||
end2 = vstart2 + get_audio_duration(Quality_Video)
|
|
||||||
if end2 - start2 > 0.5:
|
|
||||||
if DEBUG: print(f"Cutting audio ({quality_audio_file2}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s")
|
|
||||||
try:
|
|
||||||
extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file2)
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
raise BrokenPipeError(
|
|
||||||
f"{e}\n"
|
|
||||||
f"Trying to extract Audio from {Quality_Video} {start2}-{end2} "
|
|
||||||
f"to {quality_audio_file2} to have a duration of {end2-start2}, "
|
|
||||||
f"while the audio has a length of {get_audio_duration(Quality_Video)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Combine audio")
|
### Get the end from the first original audio if it is longer than the german part
|
||||||
|
start2 = vstart2 + end
|
||||||
|
# end2 = start2 + (duration2 - duration - (n1 / fps))
|
||||||
|
end2 = vstart2 + get_audio_duration(Quality_Video)
|
||||||
|
if end2 - start2 > 0.5:
|
||||||
|
if DEBUG: print(f"Cutting audio ({quality_audio_file2}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s")
|
||||||
|
try:
|
||||||
|
extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file2)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
raise BrokenPipeError(
|
||||||
|
f"{e}\n"
|
||||||
|
f"Trying to extract Audio from {Quality_Video} {start2}-{end2} "
|
||||||
|
f"to {quality_audio_file2} to have a duration of {end2-start2}, "
|
||||||
|
f"while the audio has a length of {get_audio_duration(Quality_Video)}"
|
||||||
|
)
|
||||||
|
|
||||||
# Combine the audio files
|
if step == "Combine audio":
|
||||||
if os.path.isfile(quality_audio_file1):
|
|
||||||
stick_together_audio(quality_audio_file1, german_audio_file, german_audio_file)
|
|
||||||
if os.path.isfile(quality_audio_file2):
|
|
||||||
stick_together_audio(german_audio_file, quality_audio_file2, german_audio_file)
|
|
||||||
|
|
||||||
print("Mux")
|
# Combine the audio files
|
||||||
|
if os.path.isfile(quality_audio_file1):
|
||||||
|
stick_together_audio(quality_audio_file1, german_audio_file, german_audio_file)
|
||||||
|
if os.path.isfile(quality_audio_file2):
|
||||||
|
stick_together_audio(german_audio_file, quality_audio_file2, german_audio_file)
|
||||||
|
|
||||||
# --- Mux trimmed audio ---
|
if step == "Mux":
|
||||||
final_file = mux_audio_into_output(Quality_Video, german_audio_file, output_dir)
|
|
||||||
|
|
||||||
print("Cleanup")
|
# --- Mux trimmed audio ---
|
||||||
|
final_file = mux_audio_into_output(Quality_Video, german_audio_file, output_dir)
|
||||||
|
|
||||||
if os.path.isfile(quality_audio_file1):
|
if step == "Cleanup":
|
||||||
os.remove(quality_audio_file1)
|
|
||||||
if os.path.isfile(quality_audio_file2):
|
|
||||||
os.remove(quality_audio_file2)
|
|
||||||
|
|
||||||
print("Final check")
|
if os.path.isfile(quality_audio_file1):
|
||||||
try:
|
os.remove(quality_audio_file1)
|
||||||
files_acceptable, op_ed_offset, stream_num = check_intro_outro_sync(final_file)
|
if os.path.isfile(quality_audio_file2):
|
||||||
except:
|
os.remove(quality_audio_file2)
|
||||||
new_name = final_file.with_name(f"[NO CHAPTER]_{final_file.name}")
|
|
||||||
final_file.rename(new_name)
|
|
||||||
if os.path.isfile(german_audio_file):
|
|
||||||
os.remove(path=german_audio_file)
|
|
||||||
return False
|
|
||||||
|
|
||||||
if files_acceptable and isinstance(op_ed_offset, float) and isinstance(stream_num, int):
|
if step == "Final check":
|
||||||
print(f"✔ {final_file.name} | offset={op_ed_offset:.4f}")
|
try:
|
||||||
if np.abs(op_ed_offset) > (1/fps2) and os.path.isfile(german_audio_file):
|
files_acceptable, op_ed_offset, stream_num = check_intro_outro_sync(final_file)
|
||||||
replace_with_shifted_mka(final_file, german_audio_file, stream_num, op_ed_offset)
|
except:
|
||||||
if os.path.isfile(german_audio_file):
|
new_name = final_file.with_name(f"[NO CHAPTER]_{final_file.name}")
|
||||||
os.remove(path=german_audio_file)
|
final_file.rename(new_name)
|
||||||
return True
|
if os.path.isfile(german_audio_file):
|
||||||
else:
|
os.remove(path=german_audio_file)
|
||||||
print(f"✖ FAILED: {final_file.name}")
|
return False
|
||||||
if os.path.isfile(german_audio_file):
|
|
||||||
os.remove(path=german_audio_file)
|
if files_acceptable and isinstance(op_ed_offset, float) and isinstance(stream_num, int):
|
||||||
new_name = final_file.with_name(f"[FAILED]_{final_file.name}")
|
tqdm.write(f"✔ {final_file.name} | offset={op_ed_offset:.4f}")
|
||||||
final_file.rename(new_name)
|
if np.abs(op_ed_offset) > (1/fps2) and os.path.isfile(german_audio_file):
|
||||||
return False
|
replace_with_shifted_mka(final_file, german_audio_file, stream_num, op_ed_offset)
|
||||||
|
if os.path.isfile(german_audio_file):
|
||||||
|
os.remove(path=german_audio_file)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
tqdm.write(f"✖ FAILED: {final_file.name}")
|
||||||
|
if os.path.isfile(german_audio_file):
|
||||||
|
os.remove(path=german_audio_file)
|
||||||
|
new_name = final_file.with_name(f"[FAILED]_{final_file.name}")
|
||||||
|
final_file.rename(new_name)
|
||||||
|
return False
|
||||||
|
|
||||||
|
pbar.update(w)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -171,7 +199,7 @@ def main():
|
|||||||
|
|
||||||
episodes = range(start_num, episode_num + 1)
|
episodes = range(start_num, episode_num + 1)
|
||||||
|
|
||||||
for i in episodes:
|
for i in tqdm(episodes, desc="Episodes"):
|
||||||
german_pattern = f"Folder0/S01E{i:02}*.mkv"
|
german_pattern = f"Folder0/S01E{i:02}*.mkv"
|
||||||
if os.path.isfile(german_pattern):
|
if os.path.isfile(german_pattern):
|
||||||
German_Video = [german_pattern]
|
German_Video = [german_pattern]
|
||||||
@@ -192,9 +220,9 @@ def main():
|
|||||||
if not process_one(Path(German_Video[0]), Path(Quality_Video[0]), Path(output_dir)):
|
if not process_one(Path(German_Video[0]), Path(Quality_Video[0]), Path(output_dir)):
|
||||||
fault_catch.append([Path(German_Video[0]), Path(Quality_Video[0])])
|
fault_catch.append([Path(German_Video[0]), Path(Quality_Video[0])])
|
||||||
if fault_catch:
|
if fault_catch:
|
||||||
print("\nFailed Videos:")
|
tqdm.write("\nFailed Videos:")
|
||||||
for ger, orig in fault_catch:
|
for ger, orig in fault_catch:
|
||||||
print(f"{ger} + {orig}")
|
tqdm.write(f"{ger} + {orig}")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) == 3:
|
if len(sys.argv) == 3:
|
||||||
|
|||||||
Reference in new issue
Block a user