Deepro Bardhan commited on
Commit
655b242
·
1 Parent(s): e354383

added ffmeg functionality

Browse files
Files changed (2) hide show
  1. app.py +33 -1
  2. packages.txt +1 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import cv2
4
  import numpy as np
5
  import shutil
 
6
  from SinglePhoto import FaceSwapper
7
 
8
  wellcomingMessage = """
@@ -49,6 +50,7 @@ def swap_video(src_img, src_idx, video, dst_idx):
49
  frames_dir = "VideoSwapping/video_frames"
50
  swapped_dir = "VideoSwapping/swapped_frames"
51
  output_video_path = "VideoSwapping/output_tmp_output_video.mp4"
 
52
 
53
  os.makedirs(os.path.dirname(src_path), exist_ok=True)
54
  os.makedirs(os.path.dirname(dst_video_path), exist_ok=True)
@@ -83,6 +85,14 @@ def swap_video(src_img, src_idx, video, dst_idx):
83
  frames_to_video(swapped_dir, output_video_path, fps)
84
  log += f"Combined swapped frames into video {output_video_path}\n"
85
 
 
 
 
 
 
 
 
 
86
  try:
87
  if os.path.exists(src_path):
88
  os.remove(src_path)
@@ -96,7 +106,29 @@ def swap_video(src_img, src_idx, video, dst_idx):
96
  except Exception as cleanup_error:
97
  log += f"Cleanup error: {cleanup_error}\n"
98
 
99
- return output_video_path, log
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx):
102
  log = ""
 
3
  import cv2
4
  import numpy as np
5
  import shutil
6
+ import subprocess
7
  from SinglePhoto import FaceSwapper
8
 
9
  wellcomingMessage = """
 
50
  frames_dir = "VideoSwapping/video_frames"
51
  swapped_dir = "VideoSwapping/swapped_frames"
52
  output_video_path = "VideoSwapping/output_tmp_output_video.mp4"
53
+ final_output_path = "VideoSwapping/output_with_audio.mp4"
54
 
55
  os.makedirs(os.path.dirname(src_path), exist_ok=True)
56
  os.makedirs(os.path.dirname(dst_video_path), exist_ok=True)
 
85
  frames_to_video(swapped_dir, output_video_path, fps)
86
  log += f"Combined swapped frames into video {output_video_path}\n"
87
 
88
+ # Add audio from original video
89
+ ok, audio_log = add_audio_to_video(dst_video_path, output_video_path, final_output_path)
90
+ if ok:
91
+ log += f"Added audio to {final_output_path}\n"
92
+ else:
93
+ log += f"Audio muxing failed: {audio_log}\n"
94
+ final_output_path = output_video_path # fallback to video without audio
95
+
96
  try:
97
  if os.path.exists(src_path):
98
  os.remove(src_path)
 
106
  except Exception as cleanup_error:
107
  log += f"Cleanup error: {cleanup_error}\n"
108
 
109
+ return final_output_path, log
110
+
111
+ def add_audio_to_video(original_video_path, video_no_audio_path, output_path):
112
+ """
113
+ Uses ffmpeg to mux audio from original_video_path into video_no_audio_path.
114
+ """
115
+ cmd = [
116
+ "ffmpeg",
117
+ "-y",
118
+ "-i", video_no_audio_path,
119
+ "-i", original_video_path,
120
+ "-c:v", "copy",
121
+ "-c:a", "aac",
122
+ "-map", "0:v:0",
123
+ "-map", "1:a:0?",
124
+ "-shortest",
125
+ output_path
126
+ ]
127
+ try:
128
+ subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
129
+ return True, ""
130
+ except subprocess.CalledProcessError as e:
131
+ return False, e.stderr.decode()
132
 
133
  def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx):
134
  log = ""
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg