artificialguybr commited on
Commit
b6ee570
1 Parent(s): 8e84c68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -4
app.py CHANGED
@@ -30,14 +30,29 @@ os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC)
30
  model_size = "small"
31
  model = WhisperModel(model_size, device="cuda", compute_type="int8")
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def process_video(radio, video, target_language):
34
  if target_language is None:
35
  return gr.Interface.Warnings("Please select a Target Language for Dubbing.")
36
 
37
  run_uuid = uuid.uuid4().hex[:6]
38
-
39
  output_filename = f"{run_uuid}_resized_video.mp4"
40
- #ffmpeg.input(video).output(output_filename, vf='scale=-1:720:force_original_aspect_ratio=decrease').run()
41
  ffmpeg.input(video).output(output_filename, vf='scale=-2:720').run()
42
 
43
  video_path = output_filename
@@ -45,6 +60,14 @@ def process_video(radio, video, target_language):
45
  if not os.path.exists(video_path):
46
  return f"Error: {video_path} does not exist."
47
 
 
 
 
 
 
 
 
 
48
  ffmpeg.input(video_path).output(f"{run_uuid}_output_audio.wav", acodec='pcm_s24le', ar=48000, map='a').run()
49
 
50
  #y, sr = sf.read(f"{run_uuid}_output_audio.wav")
@@ -87,8 +110,15 @@ def process_video(radio, video, target_language):
87
 
88
  video_path_fix = video_path
89
 
90
- cmd = f"python Wav2Lip/inference.py --checkpoint_path 'Wav2Lip/checkpoints/wav2lip_gan.pth' --face {shlex.quote(video_path_fix)} --audio '{run_uuid}_output_synth.wav' --pads {pad_top} {pad_bottom} {pad_left} {pad_right} --resize_factor {rescaleFactor} --nosmooth --outfile '{run_uuid}_output_video.mp4'"
91
- subprocess.run(cmd, shell=True)
 
 
 
 
 
 
 
92
 
93
  if not os.path.exists(f"{run_uuid}_output_video.mp4"):
94
  raise FileNotFoundError(f"Error: {run_uuid}_output_video.mp4 was not generated.")
 
30
  model_size = "small"
31
  model = WhisperModel(model_size, device="cuda", compute_type="int8")
32
 
33
+ def check_for_faces(video_path):
34
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
35
+ cap = cv2.VideoCapture(video_path)
36
+
37
+ while True:
38
+ ret, frame = cap.read()
39
+ if not ret:
40
+ break
41
+
42
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
43
+ faces = face_cascade.detectMultiScale(gray, 1.1, 4)
44
+
45
+ if len(faces) > 0:
46
+ return True
47
+
48
+ return False
49
+
50
  def process_video(radio, video, target_language):
51
  if target_language is None:
52
  return gr.Interface.Warnings("Please select a Target Language for Dubbing.")
53
 
54
  run_uuid = uuid.uuid4().hex[:6]
 
55
  output_filename = f"{run_uuid}_resized_video.mp4"
 
56
  ffmpeg.input(video).output(output_filename, vf='scale=-2:720').run()
57
 
58
  video_path = output_filename
 
60
  if not os.path.exists(video_path):
61
  return f"Error: {video_path} does not exist."
62
 
63
+ # Move the duration check here
64
+ video_info = ffmpeg.probe(video_path)
65
+ video_duration = float(video_info['streams'][0]['duration'])
66
+
67
+ if video_duration > 60:
68
+ os.remove(video_path) # Delete the resized video
69
+ return gr.Interface.Warnings("Video duration exceeds 1 minute. Please upload a shorter video.")
70
+
71
  ffmpeg.input(video_path).output(f"{run_uuid}_output_audio.wav", acodec='pcm_s24le', ar=48000, map='a').run()
72
 
73
  #y, sr = sf.read(f"{run_uuid}_output_audio.wav")
 
110
 
111
  video_path_fix = video_path
112
 
113
+ has_face = check_for_faces(video_path)
114
+
115
+ if has_face:
116
+ cmd = f"python Wav2Lip/inference.py --checkpoint_path 'Wav2Lip/checkpoints/wav2lip_gan.pth' --face {shlex.quote(video_path)} --audio '{run_uuid}_output_synth.wav' --pads {pad_top} {pad_bottom} {pad_left} {pad_right} --resize_factor {rescaleFactor} --nosmooth --outfile '{run_uuid}_output_video.mp4'"
117
+ subprocess.run(cmd, shell=True)
118
+ else:
119
+ # Merge audio with the original video without running Wav2Lip
120
+ cmd = f"ffmpeg -i {video_path} -i {run_uuid}_output_synth.wav -c:v copy -c:a aac -strict experimental {run_uuid}_output_video.mp4"
121
+ subprocess.run(cmd, shell=True)
122
 
123
  if not os.path.exists(f"{run_uuid}_output_video.mp4"):
124
  raise FileNotFoundError(f"Error: {run_uuid}_output_video.mp4 was not generated.")