Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from gtts import gTTS
|
2 |
import gradio as gr
|
3 |
import os
|
@@ -5,8 +6,6 @@ import speech_recognition as sr
|
|
5 |
from googletrans import Translator, constants
|
6 |
from pprint import pprint
|
7 |
from moviepy.editor import *
|
8 |
-
import os
|
9 |
-
os.system('ls')
|
10 |
def video_to_translate(file_obj,initial_language,final_language):
|
11 |
# Insert Local Video File Path
|
12 |
videoclip = VideoFileClip(file_obj.name)
|
@@ -33,6 +32,7 @@ def video_to_translate(file_obj,initial_language,final_language):
|
|
33 |
audio_data = r.record(source)
|
34 |
# recognize (convert from speech to text)
|
35 |
text = r.recognize_google(audio_data, language = lang_in)
|
|
|
36 |
if final_language == "English":
|
37 |
lang='en'
|
38 |
elif final_language == "Italian":
|
@@ -46,38 +46,29 @@ def video_to_translate(file_obj,initial_language,final_language):
|
|
46 |
elif final_language == "Japanese":
|
47 |
lang='ja'
|
48 |
elif final_language == "Chinese":
|
49 |
-
lang='zh-CN'
|
50 |
-
|
51 |
-
print("-----------Step 1-----------")
|
52 |
print(lang)
|
53 |
# init the Google API translator
|
54 |
translator = Translator()
|
55 |
-
print("-----------Step 2-----------")
|
56 |
-
print("text=",text)
|
57 |
-
|
58 |
-
text= "this pandemic is shaking "
|
59 |
translation = translator.translate(text, dest=lang)
|
60 |
-
print("-----------Step 3-----------")
|
61 |
#translation.text
|
62 |
trans=translation.text
|
63 |
-
myobj = gTTS(text=trans, lang=lang, slow=False)
|
64 |
-
print("-----------Step 4-----------")
|
65 |
myobj.save("audio.wav")
|
66 |
# loading audio file
|
67 |
-
print("-----------Step 5-----------")
|
68 |
audioclip = AudioFileClip("audio.wav")
|
|
|
69 |
# adding audio to the video clip
|
70 |
new_audioclip = CompositeAudioClip([audioclip])
|
71 |
videoclip.audio = new_audioclip
|
72 |
-
print("-----------Step 6-----------")
|
73 |
videoclip.write_videofile("new_filename.mp4")
|
74 |
#return 'audio.wav'
|
75 |
-
print("-----------Step Final-----------")
|
76 |
return 'new_filename.mp4'
|
77 |
|
78 |
initial_language = gr.inputs.Dropdown(["English","Italian","Japanese","Russian","Spanish","German"])
|
79 |
final_language = gr.inputs.Dropdown([ "Russian","Italian","Spanish","German","English","Japanese","Chinese"])
|
80 |
|
|
|
81 |
gr.Interface(fn = video_to_translate,
|
82 |
inputs = ['file',initial_language,final_language],
|
83 |
outputs = 'video',
|
@@ -90,10 +81,10 @@ gr.Interface(fn = video_to_translate,
|
|
90 |
For more information visit <a href="https://ruslanmv.com/">ruslanmv.com</a>
|
91 |
</p>
|
92 |
</div>''',
|
93 |
-
examples=[['obama.mp4',"English",'Spanish']
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
).launch()
|
|
|
1 |
+
# coding=utf8
|
2 |
from gtts import gTTS
|
3 |
import gradio as gr
|
4 |
import os
|
|
|
6 |
from googletrans import Translator, constants
|
7 |
from pprint import pprint
|
8 |
from moviepy.editor import *
|
|
|
|
|
9 |
def video_to_translate(file_obj,initial_language,final_language):
|
10 |
# Insert Local Video File Path
|
11 |
videoclip = VideoFileClip(file_obj.name)
|
|
|
32 |
audio_data = r.record(source)
|
33 |
# recognize (convert from speech to text)
|
34 |
text = r.recognize_google(audio_data, language = lang_in)
|
35 |
+
|
36 |
if final_language == "English":
|
37 |
lang='en'
|
38 |
elif final_language == "Italian":
|
|
|
46 |
elif final_language == "Japanese":
|
47 |
lang='ja'
|
48 |
elif final_language == "Chinese":
|
49 |
+
lang='zh-CN'
|
|
|
|
|
50 |
print(lang)
|
51 |
# init the Google API translator
|
52 |
translator = Translator()
|
|
|
|
|
|
|
|
|
53 |
translation = translator.translate(text, dest=lang)
|
|
|
54 |
#translation.text
|
55 |
trans=translation.text
|
56 |
+
myobj = gTTS(text=trans, lang=lang, slow=False)
|
|
|
57 |
myobj.save("audio.wav")
|
58 |
# loading audio file
|
|
|
59 |
audioclip = AudioFileClip("audio.wav")
|
60 |
+
|
61 |
# adding audio to the video clip
|
62 |
new_audioclip = CompositeAudioClip([audioclip])
|
63 |
videoclip.audio = new_audioclip
|
|
|
64 |
videoclip.write_videofile("new_filename.mp4")
|
65 |
#return 'audio.wav'
|
|
|
66 |
return 'new_filename.mp4'
|
67 |
|
68 |
initial_language = gr.inputs.Dropdown(["English","Italian","Japanese","Russian","Spanish","German"])
|
69 |
final_language = gr.inputs.Dropdown([ "Russian","Italian","Spanish","German","English","Japanese","Chinese"])
|
70 |
|
71 |
+
|
72 |
gr.Interface(fn = video_to_translate,
|
73 |
inputs = ['file',initial_language,final_language],
|
74 |
outputs = 'video',
|
|
|
81 |
For more information visit <a href="https://ruslanmv.com/">ruslanmv.com</a>
|
82 |
</p>
|
83 |
</div>''',
|
84 |
+
#examples=[['obama.mp4',"English",'Spanish'],
|
85 |
+
# ['obama.mp4',"English",'Italian'],
|
86 |
+
# ['obama.mp4',"English",'German'],
|
87 |
+
# ['obama.mp4',"English",'Japanese'],
|
88 |
+
# ['obama.mp4',"English",'Chinese']
|
89 |
+
# ]
|
90 |
).launch()
|