fix windows paths, finally ig

This commit is contained in:
Somdev Sangwan 2023-05-29 21:24:40 +05:30
parent f94a79a885
commit de34810b85
2 changed files with 22 additions and 15 deletions

View File

@ -3,7 +3,14 @@ import shutil
sep = "/" sep = "/"
if os.name == "nt": if os.name == "nt":
sep = "/" sep = "\\"
def path(string):
if sep == "\\":
return string.replace("/", "\\")
return string
def run_command(command, mode="silent"): def run_command(command, mode="silent"):
if mode == "debug": if mode == "debug":
@ -11,6 +18,7 @@ def run_command(command, mode="silent"):
return os.popen(command).read() return os.popen(command).read()
def detect_fps(input_path): def detect_fps(input_path):
input_path = path(input_path)
output = os.popen(f'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate "{input_path}"').read() output = os.popen(f'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate "{input_path}"').read()
if "/" in output: if "/" in output:
try: try:
@ -21,24 +29,28 @@ def detect_fps(input_path):
def set_fps(input_path, output_path, fps): def set_fps(input_path, output_path, fps):
input_path, output_path = path(input_path), path(output_path)
os.system(f'ffmpeg -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') os.system(f'ffmpeg -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"')
def create_video(video_name, fps, output_dir): def create_video(video_name, fps, output_dir):
output_dir = path(output_dir)
os.system(f'ffmpeg -framerate {fps} -pattern_type glob -i "{output_dir}{sep}*.png" -c:v libx264 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') os.system(f'ffmpeg -framerate {fps} -pattern_type glob -i "{output_dir}{sep}*.png" -c:v libx264 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"')
def extract_frames(input_path, output_dir): def extract_frames(input_path, output_dir):
input_path, output_dir = path(input_path), path(output_dir)
os.system(f'ffmpeg -i "{input_path}" "{output_dir}{sep}%04d.png"') os.system(f'ffmpeg -i "{input_path}" "{output_dir}{sep}%04d.png"')
def add_audio(output_dir, target_path, keep_frames, output_file): def add_audio(output_dir, target_path, keep_frames, output_file):
video = target_path.split(sep)[-1] video = target_path.split(sep)[-1]
video_name = video.split(".")[0] video_name = video.split(".")[0]
save_to = output_file if output_file else output_dir + f"{sep}swapped-" + video_name + ".mp4" save_to = output_file if output_file else output_dir + f"/swapped-" + video_name + ".mp4"
os.system(f'ffmpeg -i "{output_dir}{sep}output.mp4" -i "{output_dir}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to}"') save_to_ff, output_dir_ff = path(save_to), path(output_dir)
os.system(f'ffmpeg -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"')
if not os.path.isfile(save_to): if not os.path.isfile(save_to):
shutil.move(output_dir + f"{sep}output.mp4", save_to) shutil.move(output_dir + f"/output.mp4", save_to)
if not keep_frames: if not keep_frames:
shutil.rmtree(output_dir) shutil.rmtree(output_dir)

17
run.py
View File

@ -35,9 +35,6 @@ parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_fr
for name, value in vars(parser.parse_args()).items(): for name, value in vars(parser.parse_args()).items():
args[name] = value args[name] = value
sep = "/" # used for filepaths e.g. /root/path/output.mp4
if sys.platform.startswith("win") or os.name == 'nt':
sep = "/" # for windows
def start_processing(): def start_processing():
if args['gpu']: if args['gpu']:
@ -89,27 +86,25 @@ def start():
if is_img(target_path): if is_img(target_path):
process_img(args['source_img'], target_path) process_img(args['source_img'], target_path)
return return
video_name = target_path.split(sep)[-1].split(".")[0] video_name = target_path.split("/")[-1].split(".")[0]
output_dir = target_path.replace(target_path.split(sep)[-1], "") + sep + video_name output_dir = target_path.replace(target_path.split("/")[-1], "") + "/" + video_name
if sep == "\\":
output_dir = output_dir.lstrip("\\").rstrip("\\")
Path(output_dir).mkdir(exist_ok=True) Path(output_dir).mkdir(exist_ok=True)
fps = detect_fps(target_path) fps = detect_fps(target_path)
if not args['keep_fps'] and fps > 30: if not args['keep_fps'] and fps > 30:
this_path = output_dir + sep + video_name + ".mp4" this_path = output_dir + "/" + video_name + ".mp4"
set_fps(target_path, this_path, 30) set_fps(target_path, this_path, 30)
target_path, fps = this_path, 30 target_path, fps = this_path, 30
else: else:
shutil.copy(target_path, output_dir) shutil.copy(target_path, output_dir)
extract_frames(target_path, output_dir) extract_frames(target_path, output_dir)
args['frame_paths'] = tuple(sorted( args['frame_paths'] = tuple(sorted(
glob.glob(output_dir + f"{sep}*.png"), glob.glob(output_dir + f"/*.png"),
key=lambda x: int(x.split(sep)[-1].replace(".png", "")) key=lambda x: int(x.split("/")[-1].replace(".png", ""))
)) ))
start_processing() start_processing()
create_video(video_name, fps, output_dir) create_video(video_name, fps, output_dir)
add_audio(output_dir, target_path, args['keep_frames'], args['output_file']) add_audio(output_dir, target_path, args['keep_frames'], args['output_file'])
save_path = args['output_file'] if args['output_file'] else output_dir + sep + video_name + ".mp4" save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4"
print("\n\nVideo saved as:", save_path, "\n\n") print("\n\nVideo saved as:", save_path, "\n\n")