Spaces:
Running
on
Zero
Running
on
Zero
artificialguybr
commited on
Commit
•
233c677
1
Parent(s):
e4f2be5
Update app.py
Browse files
app.py
CHANGED
@@ -20,72 +20,62 @@ st = os.stat('ffmpeg')
|
|
20 |
os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC)
|
21 |
|
22 |
def process_video(video, high_quality, target_language):
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
video_path_fix = video_path
|
80 |
-
|
81 |
-
cmd = f"python Wav2Lip/inference.py --checkpoint_path '/Wav2Lip/checkpoints/wav2lip_gan.pth' --face {shlex.quote(video_path_fix)} --audio 'output_synth.wav' --pads {pad_top} {pad_bottom} {pad_left} {pad_right} --resize_factor {rescaleFactor} --nosmooth --outfile 'output_video.mp4'"
|
82 |
-
subprocess.run(cmd, shell=True)
|
83 |
-
# Debugging Step 3: Check if output video exists
|
84 |
-
if not os.path.exists("output_video.mp4"):
|
85 |
-
return "Error: output_video.mp4 was not generated."
|
86 |
-
return "output_video.mp4" # Return the file path directly
|
87 |
-
except Exception as e:
|
88 |
-
return {"error": f"An unexpected error occurred: {str(e)}"} # Keep the error as a dictionary
|
89 |
|
90 |
iface = gr.Interface(
|
91 |
fn=process_video,
|
|
|
20 |
os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC)
|
21 |
|
22 |
def process_video(video, high_quality, target_language):
|
23 |
+
output_filename = "resized_video.mp4"
|
24 |
+
if high_quality:
|
25 |
+
ffmpeg.input(video).output(output_filename, vf='scale=-1:720').run()
|
26 |
+
video_path = output_filename
|
27 |
+
else:
|
28 |
+
video_path = video
|
29 |
+
|
30 |
+
# Debugging Step 1: Check if video_path exists
|
31 |
+
if not os.path.exists(video_path):
|
32 |
+
return f"Error: {video_path} does not exist."
|
33 |
+
|
34 |
+
ffmpeg.input(video_path).output('output_audio.wav', acodec='pcm_s24le', ar=48000, map='a').run()
|
35 |
+
|
36 |
+
y, sr = sf.read("output_audio.wav")
|
37 |
+
y = y.astype(np.float32)
|
38 |
+
y_denoised = wiener(y)
|
39 |
+
sf.write("output_audio_denoised.wav", y_denoised, sr)
|
40 |
+
|
41 |
+
sound = AudioSegment.from_file("output_audio_denoised.wav", format="wav")
|
42 |
+
sound = sound.apply_gain(0) # Reduce gain by 5 dB
|
43 |
+
sound = sound.low_pass_filter(3000).high_pass_filter(100)
|
44 |
+
sound.export("output_audio_processed.wav", format="wav")
|
45 |
+
|
46 |
+
shell_command = f"ffmpeg -y -i output_audio_processed.wav -af lowpass=3000,highpass=100 output_audio_final.wav".split(" ")
|
47 |
+
subprocess.run([item for item in shell_command], capture_output=False, text=True, check=True)
|
48 |
+
|
49 |
+
model = whisper.load_model("base")
|
50 |
+
result = model.transcribe("output_audio_final.wav")
|
51 |
+
whisper_text = result["text"]
|
52 |
+
whisper_language = result['language']
|
53 |
+
|
54 |
+
language_mapping = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Italian': 'it', 'Portuguese': 'pt', 'Polish': 'pl', 'Turkish': 'tr', 'Russian': 'ru', 'Dutch': 'nl', 'Czech': 'cs', 'Arabic': 'ar', 'Chinese (Simplified)': 'zh-cn'}
|
55 |
+
target_language_code = language_mapping[target_language]
|
56 |
+
translator = Translator()
|
57 |
+
translated_text = translator.translate(whisper_text, src=whisper_language, dest=target_language_code).text
|
58 |
+
|
59 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1")
|
60 |
+
tts.to('cuda') # Replacing deprecated gpu=True
|
61 |
+
tts.tts_to_file(translated_text, speaker_wav='output_audio_final.wav', file_path="output_synth.wav", language=target_language_code)
|
62 |
+
|
63 |
+
pad_top = 0
|
64 |
+
pad_bottom = 15
|
65 |
+
pad_left = 0
|
66 |
+
pad_right = 0
|
67 |
+
rescaleFactor = 1
|
68 |
+
|
69 |
+
# Debugging Step 2: Remove quotes around the video path
|
70 |
+
video_path_fix = video_path
|
71 |
+
|
72 |
+
cmd = f"python Wav2Lip/inference.py --checkpoint_path '/Wav2Lip/checkpoints/wav2lip_gan.pth' --face {shlex.quote(video_path_fix)} --audio 'output_synth.wav' --pads {pad_top} {pad_bottom} {pad_left} {pad_right} --resize_factor {rescaleFactor} --nosmooth --outfile 'output_video.mp4'"
|
73 |
+
subprocess.run(cmd, shell=True)
|
74 |
+
# Debugging Step 3: Check if output video exists
|
75 |
+
if not os.path.exists("output_video.mp4"):
|
76 |
+
return "Error: output_video.mp4 was not generated."
|
77 |
+
|
78 |
+
return "output_video.mp4"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
iface = gr.Interface(
|
81 |
fn=process_video,
|