prompt user for savefile

This commit is contained in:
Somdev Sangwan 2023-05-29 13:39:44 +05:30
parent e6c21a4bdd
commit 0c867779a1
2 changed files with 11 additions and 5 deletions

View File

@ -28,12 +28,13 @@ def extract_frames(input_path, output_dir):
os.system(f"ffmpeg -i {input_path} '{output_dir}/%04d.png'") 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 = target_path.split("/")[-1]
video_name = video.split(".")[0] 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"): 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: if not keep_frames:
shutil.rmtree(output_dir) shutil.rmtree(output_dir)

9
run.py
View File

@ -5,6 +5,7 @@ import os
from pathlib import Path from pathlib import Path
import tkinter as tk import tkinter as tk
from tkinter import filedialog from tkinter import filedialog
from tkinter.filedialog import asksaveasfilename
from core.processor import process_video, process_img from core.processor import process_video, process_img
from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames
import webbrowser import webbrowser
@ -52,6 +53,10 @@ def toggle_fps_limit():
args['keep_fps'] = limit_fps.get() != True 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(): def start():
if not args['source_img'] or not os.path.isfile(args['source_img']): if not args['source_img'] or not os.path.isfile(args['source_img']):
print("\n[WARNING] Please select an image containing a face.") print("\n[WARNING] Please select an image containing a face.")
@ -83,7 +88,7 @@ def start():
)) ))
start_processing() start_processing()
create_video(video_name, fps, output_dir) 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") print("\n\nVideo saved as:", current_dir + "/swapped-" + video_name + ".mp4", "\n\n")
@ -115,6 +120,6 @@ if __name__ == "__main__":
fps_checkbox.select() fps_checkbox.select()
# Start button # 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) start_button.pack(side=tk.BOTTOM, padx=10, pady=10)
window.mainloop() window.mainloop()