artificialguybr commited on
Commit
233c677
1 Parent(s): e4f2be5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -66
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
- try:
24
- with tempfile.TemporaryDirectory() as temp_dir:
25
- output_filename = os.path.join(temp_dir, "resized_video.mp4")
26
-
27
- if high_quality:
28
- ffmpeg.input(video).output(output_filename, vf='scale=-1:720').run()
29
- video_path = output_filename
30
- else:
31
- video_path = video
32
-
33
- if not os.path.exists(video_path):
34
- return {"error": f"{video_path} does not exist."}
35
-
36
- with tempfile.TemporaryDirectory() as temp_dir:
37
- audio_output = os.path.join(temp_dir, "output_audio.wav")
38
-
39
- try:
40
- ffmpeg.input(video_path).output(audio_output, acodec='pcm_s24le', ar=48000, map='a').run()
41
- except ffmpeg.Error as e:
42
- stderr_output = e.stderr.decode('utf-8') if e.stderr else "Unknown error"
43
- return {"error": f"FFmpeg error: {stderr_output}"}
44
-
45
-
46
- y, sr = sf.read("output_audio.wav")
47
- y = y.astype(np.float32)
48
- y_denoised = wiener(y)
49
- sf.write("output_audio_denoised.wav", y_denoised, sr)
50
-
51
- sound = AudioSegment.from_file("output_audio_denoised.wav", format="wav")
52
- sound = sound.apply_gain(0) # Reduce gain by 5 dB
53
- sound = sound.low_pass_filter(3000).high_pass_filter(100)
54
- sound.export("output_audio_processed.wav", format="wav")
55
-
56
- shell_command = f"ffmpeg -y -i output_audio_processed.wav -af lowpass=3000,highpass=100 output_audio_final.wav".split(" ")
57
- subprocess.run([item for item in shell_command], capture_output=False, text=True, check=True)
58
-
59
- model = whisper.load_model("base")
60
- result = model.transcribe("output_audio_final.wav")
61
- whisper_text = result["text"]
62
- whisper_language = result['language']
63
-
64
- 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'}
65
- target_language_code = language_mapping[target_language]
66
- translator = Translator()
67
- translated_text = translator.translate(whisper_text, src=whisper_language, dest=target_language_code).text
68
-
69
- tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1")
70
- tts.to('cuda') # Replacing deprecated gpu=True
71
- tts.tts_to_file(translated_text, speaker_wav='output_audio_final.wav', file_path="output_synth.wav", language=target_language_code)
72
-
73
- pad_top = 0
74
- pad_bottom = 15
75
- pad_left = 0
76
- pad_right = 0
77
- rescaleFactor = 1
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,