From 0c867779a114956fcf5b90d8f2c25608f504ceae Mon Sep 17 00:00:00 2001 From: Somdev Sangwan Date: Mon, 29 May 2023 13:39:44 +0530 Subject: [PATCH] prompt user for savefile --- core/utils.py | 7 ++++--- run.py | 9 +++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/core/utils.py b/core/utils.py index 8cdeb0e..c8d5481 100644 --- a/core/utils.py +++ b/core/utils.py @@ -28,12 +28,13 @@ def extract_frames(input_path, output_dir): os.system(f"ffmpeg -i {input_path} '{output_dir}/%04d.png'") -def add_audio(current_dir, output_dir, target_path, keep_frames): +def add_audio(current_dir, output_dir, target_path, keep_frames, output_file): video = target_path.split("/")[-1] video_name = video.split(".")[0] - os.system(f"ffmpeg -i {output_dir}/output.mp4 -i {output_dir}/{video} -c:v copy -map 0:v:0 -map 1:a:0 -y {current_dir}/swapped-{video_name}.mp4") + save_to = output_file if output_file else current_dir + "/swapped-" + video_name + ".mp4" + os.system(f"ffmpeg -i {output_dir}/output.mp4 -i {output_dir}/{video} -c:v copy -map 0:v:0 -map 1:a:0 -y {save_to}") if not os.path.isfile(current_dir + "/swapped-" + video_name + ".mp4"): - shutil.move(output_dir + "/output.mp4", current_dir + "/swapped-" + video_name + ".mp4") + shutil.move(output_dir + "/output.mp4", save_to) if not keep_frames: shutil.rmtree(output_dir) diff --git a/run.py b/run.py index f798f47..7236bd8 100644 --- a/run.py +++ b/run.py @@ -5,6 +5,7 @@ import os from pathlib import Path import tkinter as tk from tkinter import filedialog +from tkinter.filedialog import asksaveasfilename from core.processor import process_video, process_img from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames import webbrowser @@ -52,6 +53,10 @@ def toggle_fps_limit(): args['keep_fps'] = limit_fps.get() != True +def save_file(): + args['output_file'] = asksaveasfilename(initialfile='output.mp4', defaultextension=".mp4", filetypes=[("All Files","*.*"),("Videos","*.mp4")]) + + def start(): if not args['source_img'] or not os.path.isfile(args['source_img']): print("\n[WARNING] Please select an image containing a face.") @@ -83,7 +88,7 @@ def start(): )) start_processing() create_video(video_name, fps, output_dir) - add_audio(current_dir, output_dir, target_path, args['keep_frames']) + add_audio(current_dir, output_dir, target_path, args['keep_frames'], args['output_file']) print("\n\nVideo saved as:", current_dir + "/swapped-" + video_name + ".mp4", "\n\n") @@ -115,6 +120,6 @@ if __name__ == "__main__": fps_checkbox.select() # Start button - start_button = tk.Button(window, text="Start", bg="green", command=start) + start_button = tk.Button(window, text="Start", bg="green", command=lambda: [save_file(), start()]) start_button.pack(side=tk.BOTTOM, padx=10, pady=10) window.mainloop()