V 1.1 - tqdm progress
This commit is contained in:
1 parent
e00fa1ca80
commit
1d73c869da
1 file changed
+43
-15
@@ -33,8 +33,34 @@ def mux_audio_into_output(Quality_Video, audio_file, output_dir):
|
||||
return final_output
|
||||
|
||||
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 = [
|
||||
1,
|
||||
3,
|
||||
1,
|
||||
2,
|
||||
1,
|
||||
2,
|
||||
2,
|
||||
1,
|
||||
2,
|
||||
]
|
||||
|
||||
with tqdm(total=len(steps), desc=Quality_Video.name, leave=False) as pbar:
|
||||
for step, w in zip(steps, weights):
|
||||
pbar.set_description(step)
|
||||
if step == "Probe video":
|
||||
# --- Probe video ---
|
||||
fps, duration, _, vstart = probe_video(German_Video)
|
||||
fps2, duration2, _, vstart2 = probe_video(Quality_Video)
|
||||
@@ -44,7 +70,7 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
|
||||
if duration2 <= 0:
|
||||
raise ValueError("Duration of Quality_Video must be greater than 0")
|
||||
|
||||
print("Compute offset")
|
||||
if step == "Compute offset":
|
||||
# --- Offset ---
|
||||
if offset == None:
|
||||
offset0 = corelate_videos(Quality_Video, German_Video, fps2)
|
||||
@@ -52,9 +78,9 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
|
||||
offset0 = offset
|
||||
if offset0 > duration or offset0 > duration2:
|
||||
raise ValueError("Offset needs to be smaller than the video durations")
|
||||
print(f"Offset: {offset0}s")
|
||||
pbar.set_postfix_str(f"Offset: {offset0}s")
|
||||
|
||||
print("Extract pre beginning")
|
||||
if step == "Extract pre beginning":
|
||||
german_audio_file = output_dir / f"{Path(Quality_Video).stem}_german_audio.mka"
|
||||
quality_audio_file1 = output_dir / f"{Path(Quality_Video).stem}_original_audio1.mka"
|
||||
quality_audio_file2 = output_dir / f"{Path(Quality_Video).stem}_original_audio2.mka"
|
||||
@@ -80,7 +106,7 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
|
||||
f"while the audio has a length of {get_audio_duration(Quality_Video)}"
|
||||
)
|
||||
|
||||
print("Extract german audio")
|
||||
if step == "Extract german audio":
|
||||
|
||||
### Get the German Audio
|
||||
if offset0 < 0:
|
||||
@@ -99,7 +125,7 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
|
||||
f"while the audio has a length of {get_audio_duration(German_Video)}"
|
||||
)
|
||||
|
||||
print("Extract post end")
|
||||
if step == "Extract post end":
|
||||
|
||||
### Get the end from the first original audio if it is longer than the german part
|
||||
start2 = vstart2 + end
|
||||
@@ -117,7 +143,7 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
|
||||
f"while the audio has a length of {get_audio_duration(Quality_Video)}"
|
||||
)
|
||||
|
||||
print("Combine audio")
|
||||
if step == "Combine audio":
|
||||
|
||||
# Combine the audio files
|
||||
if os.path.isfile(quality_audio_file1):
|
||||
@@ -125,19 +151,19 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
|
||||
if os.path.isfile(quality_audio_file2):
|
||||
stick_together_audio(german_audio_file, quality_audio_file2, german_audio_file)
|
||||
|
||||
print("Mux")
|
||||
if step == "Mux":
|
||||
|
||||
# --- Mux trimmed audio ---
|
||||
final_file = mux_audio_into_output(Quality_Video, german_audio_file, output_dir)
|
||||
|
||||
print("Cleanup")
|
||||
if step == "Cleanup":
|
||||
|
||||
if os.path.isfile(quality_audio_file1):
|
||||
os.remove(quality_audio_file1)
|
||||
if os.path.isfile(quality_audio_file2):
|
||||
os.remove(quality_audio_file2)
|
||||
|
||||
print("Final check")
|
||||
if step == "Final check":
|
||||
try:
|
||||
files_acceptable, op_ed_offset, stream_num = check_intro_outro_sync(final_file)
|
||||
except:
|
||||
@@ -148,20 +174,22 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
|
||||
return False
|
||||
|
||||
if files_acceptable and isinstance(op_ed_offset, float) and isinstance(stream_num, int):
|
||||
print(f"✔ {final_file.name} | offset={op_ed_offset:.4f}")
|
||||
tqdm.write(f"✔ {final_file.name} | offset={op_ed_offset:.4f}")
|
||||
if np.abs(op_ed_offset) > (1/fps2) and os.path.isfile(german_audio_file):
|
||||
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:
|
||||
print(f"✖ FAILED: {final_file.name}")
|
||||
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():
|
||||
output_dir = "./output_dir"
|
||||
@@ -171,7 +199,7 @@ def main():
|
||||
|
||||
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"
|
||||
if os.path.isfile(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)):
|
||||
fault_catch.append([Path(German_Video[0]), Path(Quality_Video[0])])
|
||||
if fault_catch:
|
||||
print("\nFailed Videos:")
|
||||
tqdm.write("\nFailed Videos:")
|
||||
for ger, orig in fault_catch:
|
||||
print(f"{ger} + {orig}")
|
||||
tqdm.write(f"{ger} + {orig}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) == 3:
|
||||
|
||||
Reference in new issue
Block a user