Spaces:
Running
Running
Deepro Bardhan
commited on
Commit
·
20dbbf0
1
Parent(s):
2c0a9f6
added copy to gdrive
Browse files
app.py
CHANGED
|
@@ -341,7 +341,7 @@ def add_audio_to_video(original_video_path, video_no_audio_path, output_path):
|
|
| 341 |
except subprocess.CalledProcessError as e:
|
| 342 |
return False, e.stderr.decode()
|
| 343 |
|
| 344 |
-
def swap_video(src_img, src_idx, video, dst_idx, delete_frames_dir=True, add_audio=True, progress=gr.Progress()):
|
| 345 |
log = ""
|
| 346 |
start_time = time.time()
|
| 347 |
src_path = "VideoSwapping/data_src.jpg"
|
|
@@ -413,6 +413,15 @@ def swap_video(src_img, src_idx, video, dst_idx, delete_frames_dir=True, add_aud
|
|
| 413 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 414 |
progress(0.8, desc="Muxing audio")
|
| 415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
if add_audio:
|
| 417 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
| 418 |
if ok:
|
|
@@ -444,7 +453,7 @@ def swap_video(src_img, src_idx, video, dst_idx, delete_frames_dir=True, add_aud
|
|
| 444 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 445 |
return final_output_path, log
|
| 446 |
|
| 447 |
-
def swap_video_all_faces(src_img, video, num_faces_to_swap, delete_frames_dir=True, add_audio=True, progress=gr.Progress()):
|
| 448 |
log = ""
|
| 449 |
start_time = time.time()
|
| 450 |
src_path = "VideoSwappingAllFaces/data_src.jpg"
|
|
@@ -524,6 +533,15 @@ def swap_video_all_faces(src_img, video, num_faces_to_swap, delete_frames_dir=Tr
|
|
| 524 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 525 |
progress(0.8, desc="Muxing audio")
|
| 526 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
if add_audio:
|
| 528 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
| 529 |
if ok:
|
|
@@ -555,7 +573,7 @@ def swap_video_all_faces(src_img, video, num_faces_to_swap, delete_frames_dir=Tr
|
|
| 555 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 556 |
return final_output_path, log
|
| 557 |
|
| 558 |
-
def swap_video_custom_mapping(src_imgs, video, mapping_str, delete_frames_dir=True, add_audio=True, progress=gr.Progress()):
|
| 559 |
log = ""
|
| 560 |
start_time = time.time()
|
| 561 |
src_dir = "CustomVideoSwap/src"
|
|
@@ -693,7 +711,7 @@ def swap_video_custom_mapping(src_imgs, video, mapping_str, delete_frames_dir=Tr
|
|
| 693 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 694 |
return final_output_path, log
|
| 695 |
|
| 696 |
-
def swap_single_src_multi_video(src_img, dst_videos, dst_indices, delete_frames_dir=True, add_audio=True, progress=gr.Progress(track_tqdm=True)):
|
| 697 |
"""
|
| 698 |
Swaps a single source image onto multiple videos, with a mapping for destination face indices.
|
| 699 |
Each video is processed one by one.
|
|
@@ -775,6 +793,15 @@ def swap_single_src_multi_video(src_img, dst_videos, dst_indices, delete_frames_
|
|
| 775 |
frames_to_video(swapped_dir, output_video_path, fps)
|
| 776 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 777 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 778 |
# Mux audio if requested
|
| 779 |
if add_audio:
|
| 780 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, output_video_with_audio)
|
|
@@ -885,7 +912,8 @@ with gr.Blocks() as demo:
|
|
| 885 |
gr.Video(label="Target Video"),
|
| 886 |
gr.Number(value=1, label="Destination Face Index"),
|
| 887 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 888 |
-
gr.Checkbox(label="Add audio from original video", value=True)
|
|
|
|
| 889 |
],
|
| 890 |
outputs=[
|
| 891 |
gr.Video(label="Swapped Video"),
|
|
@@ -900,7 +928,8 @@ with gr.Blocks() as demo:
|
|
| 900 |
gr.Video(label="Target Video"),
|
| 901 |
gr.Number(value=1, label="Number of Faces to Swap"),
|
| 902 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 903 |
-
gr.Checkbox(label="Add audio from original video", value=True)
|
|
|
|
| 904 |
],
|
| 905 |
outputs=[
|
| 906 |
gr.Video(label="Swapped Video"),
|
|
@@ -915,7 +944,8 @@ with gr.Blocks() as demo:
|
|
| 915 |
gr.Video(label="Target Video"),
|
| 916 |
gr.Textbox(label="Mapping (comma-separated, e.g. 2,1,3)"),
|
| 917 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 918 |
-
gr.Checkbox(label="Add audio from original video", value=True)
|
|
|
|
| 919 |
],
|
| 920 |
outputs=[
|
| 921 |
gr.Video(label="Swapped Video"),
|
|
@@ -930,7 +960,8 @@ with gr.Blocks() as demo:
|
|
| 930 |
gr.File(label="Target Videos (select multiple)", file_count="multiple", type="filepath"),
|
| 931 |
gr.Textbox(label="Destination Face Indices (comma-separated, e.g. 1,2,1)"),
|
| 932 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 933 |
-
gr.Checkbox(label="Add audio from original video", value=True)
|
|
|
|
| 934 |
],
|
| 935 |
outputs=[
|
| 936 |
gr.Gallery(label="Swapped Videos", type="filepath"),
|
|
|
|
| 341 |
except subprocess.CalledProcessError as e:
|
| 342 |
return False, e.stderr.decode()
|
| 343 |
|
| 344 |
+
def swap_video(src_img, src_idx, video, dst_idx, delete_frames_dir=True, add_audio=True, copy_to_drive=False, progress=gr.Progress()):
|
| 345 |
log = ""
|
| 346 |
start_time = time.time()
|
| 347 |
src_path = "VideoSwapping/data_src.jpg"
|
|
|
|
| 413 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 414 |
progress(0.8, desc="Muxing audio")
|
| 415 |
|
| 416 |
+
# Copy to Google Drive if requested
|
| 417 |
+
if copy_to_drive:
|
| 418 |
+
drive_path = "/content/drive/MyDrive/" + os.path.basename(output_video_path)
|
| 419 |
+
try:
|
| 420 |
+
shutil.copy(output_video_path, drive_path)
|
| 421 |
+
log += f"Copied swapped video without audio to Google Drive: {drive_path}\n"
|
| 422 |
+
except Exception as e:
|
| 423 |
+
log += f"Failed to copy to Google Drive: {e}\n"
|
| 424 |
+
|
| 425 |
if add_audio:
|
| 426 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
| 427 |
if ok:
|
|
|
|
| 453 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 454 |
return final_output_path, log
|
| 455 |
|
| 456 |
+
def swap_video_all_faces(src_img, video, num_faces_to_swap, delete_frames_dir=True, add_audio=True, copy_to_drive=False, progress=gr.Progress()):
|
| 457 |
log = ""
|
| 458 |
start_time = time.time()
|
| 459 |
src_path = "VideoSwappingAllFaces/data_src.jpg"
|
|
|
|
| 533 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 534 |
progress(0.8, desc="Muxing audio")
|
| 535 |
|
| 536 |
+
# Copy to Google Drive if requested
|
| 537 |
+
if copy_to_drive:
|
| 538 |
+
drive_path = "/content/drive/MyDrive/" + os.path.basename(output_video_path)
|
| 539 |
+
try:
|
| 540 |
+
shutil.copy(output_video_path, drive_path)
|
| 541 |
+
log += f"Copied swapped video without audio to Google Drive: {drive_path}\n"
|
| 542 |
+
except Exception as e:
|
| 543 |
+
log += f"Failed to copy to Google Drive: {e}\n"
|
| 544 |
+
|
| 545 |
if add_audio:
|
| 546 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
|
| 547 |
if ok:
|
|
|
|
| 573 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 574 |
return final_output_path, log
|
| 575 |
|
| 576 |
+
def swap_video_custom_mapping(src_imgs, video, mapping_str, delete_frames_dir=True, add_audio=True, copy_to_drive=False, progress=gr.Progress()):
|
| 577 |
log = ""
|
| 578 |
start_time = time.time()
|
| 579 |
src_dir = "CustomVideoSwap/src"
|
|
|
|
| 711 |
log += f"Elapsed time: {elapsed:.2f} seconds\n"
|
| 712 |
return final_output_path, log
|
| 713 |
|
| 714 |
+
def swap_single_src_multi_video(src_img, dst_videos, dst_indices, delete_frames_dir=True, add_audio=True, copy_to_drive=False, progress=gr.Progress(track_tqdm=True)):
|
| 715 |
"""
|
| 716 |
Swaps a single source image onto multiple videos, with a mapping for destination face indices.
|
| 717 |
Each video is processed one by one.
|
|
|
|
| 793 |
frames_to_video(swapped_dir, output_video_path, fps)
|
| 794 |
log += f"Combined swapped frames into video {output_video_path}\n"
|
| 795 |
|
| 796 |
+
# Copy to Google Drive if requested
|
| 797 |
+
if copy_to_drive:
|
| 798 |
+
drive_path = "/content/drive/MyDrive/" + os.path.basename(output_video_path)
|
| 799 |
+
try:
|
| 800 |
+
shutil.copy(output_video_path, drive_path)
|
| 801 |
+
log += f"Copied swapped video without audio to Google Drive: {drive_path}\n"
|
| 802 |
+
except Exception as e:
|
| 803 |
+
log += f"Failed to copy to Google Drive: {e}\n"
|
| 804 |
+
|
| 805 |
# Mux audio if requested
|
| 806 |
if add_audio:
|
| 807 |
ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, output_video_with_audio)
|
|
|
|
| 912 |
gr.Video(label="Target Video"),
|
| 913 |
gr.Number(value=1, label="Destination Face Index"),
|
| 914 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 915 |
+
gr.Checkbox(label="Add audio from original video", value=True),
|
| 916 |
+
gr.Checkbox(label="Copy swapped video (no audio) to Google Drive root", value=False)
|
| 917 |
],
|
| 918 |
outputs=[
|
| 919 |
gr.Video(label="Swapped Video"),
|
|
|
|
| 928 |
gr.Video(label="Target Video"),
|
| 929 |
gr.Number(value=1, label="Number of Faces to Swap"),
|
| 930 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 931 |
+
gr.Checkbox(label="Add audio from original video", value=True),
|
| 932 |
+
gr.Checkbox(label="Copy swapped video (no audio) to Google Drive root", value=False)
|
| 933 |
],
|
| 934 |
outputs=[
|
| 935 |
gr.Video(label="Swapped Video"),
|
|
|
|
| 944 |
gr.Video(label="Target Video"),
|
| 945 |
gr.Textbox(label="Mapping (comma-separated, e.g. 2,1,3)"),
|
| 946 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 947 |
+
gr.Checkbox(label="Add audio from original video", value=True),
|
| 948 |
+
gr.Checkbox(label="Copy swapped video (no audio) to Google Drive root", value=False)
|
| 949 |
],
|
| 950 |
outputs=[
|
| 951 |
gr.Video(label="Swapped Video"),
|
|
|
|
| 960 |
gr.File(label="Target Videos (select multiple)", file_count="multiple", type="filepath"),
|
| 961 |
gr.Textbox(label="Destination Face Indices (comma-separated, e.g. 1,2,1)"),
|
| 962 |
gr.Checkbox(label="Delete video_frames directory after processing", value=True),
|
| 963 |
+
gr.Checkbox(label="Add audio from original video", value=True),
|
| 964 |
+
gr.Checkbox(label="Copy swapped video (no audio) to Google Drive root", value=False)
|
| 965 |
],
|
| 966 |
outputs=[
|
| 967 |
gr.Gallery(label="Swapped Videos", type="filepath"),
|