Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf8
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
import speech_recognition as sr
|
| 6 |
+
from googletrans import Translator, constants
|
| 7 |
+
from pprint import pprint
|
| 8 |
+
#pip install moviepy
|
| 9 |
+
#pip3 install googletrans
|
| 10 |
+
from moviepy.editor import *
|
| 11 |
+
def video_to_translate(file_obj,initial_language,final_language):
|
| 12 |
+
# Insert Local Video File Path
|
| 13 |
+
videoclip = VideoFileClip(file_obj.name)
|
| 14 |
+
# Insert Local Audio File Path
|
| 15 |
+
videoclip.audio.write_audiofile("test.wav",codec='pcm_s16le')
|
| 16 |
+
# initialize the recognizer
|
| 17 |
+
r = sr.Recognizer()
|
| 18 |
+
|
| 19 |
+
if initial_language == "English":
|
| 20 |
+
lang_in='en-US'
|
| 21 |
+
elif initial_language == "Italian":
|
| 22 |
+
lang_in='it-IT'
|
| 23 |
+
elif initial_language == "Spanish":
|
| 24 |
+
lang_in='es-MX'
|
| 25 |
+
elif initial_language == "Russian":
|
| 26 |
+
lang_in='ru-RU'
|
| 27 |
+
elif initial_language == "German":
|
| 28 |
+
lang_in='de-DE'
|
| 29 |
+
elif initial_language == "Japanese":
|
| 30 |
+
lang_in='ja-JP'
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# open the file
|
| 35 |
+
with sr.AudioFile("test.wav") as source:
|
| 36 |
+
# listen for the data (load audio to memory)
|
| 37 |
+
audio_data = r.record(source)
|
| 38 |
+
# recognize (convert from speech to text)
|
| 39 |
+
text = r.recognize_google(audio_data, language = lang_in)
|
| 40 |
+
|
| 41 |
+
if final_language == "English":
|
| 42 |
+
lang='en'
|
| 43 |
+
elif final_language == "Italian":
|
| 44 |
+
lang='it'
|
| 45 |
+
elif final_language == "Spanish":
|
| 46 |
+
lang='es'
|
| 47 |
+
elif final_language == "Russian":
|
| 48 |
+
lang='ru'
|
| 49 |
+
elif final_language == "German":
|
| 50 |
+
lang='de'
|
| 51 |
+
|
| 52 |
+
print(lang)
|
| 53 |
+
# init the Google API translator
|
| 54 |
+
translator = Translator()
|
| 55 |
+
translation = translator.translate(text, dest=lang)
|
| 56 |
+
#translation.text
|
| 57 |
+
trans=translation.text
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
myobj = gTTS(text=trans, lang=lang, slow=False)
|
| 61 |
+
myobj.save("audio.wav")
|
| 62 |
+
|
| 63 |
+
# loading audio file
|
| 64 |
+
audioclip = AudioFileClip("audio.wav")
|
| 65 |
+
|
| 66 |
+
# adding audio to the video clip
|
| 67 |
+
new_audioclip = CompositeAudioClip([audioclip])
|
| 68 |
+
videoclip.audio = new_audioclip
|
| 69 |
+
videoclip.write_videofile("new_filename.mp4")
|
| 70 |
+
#return 'audio.wav'
|
| 71 |
+
return 'new_filename.mp4'
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
examples = [
|
| 75 |
+
[os.path.abspath("English-Steve-Jobs.mp4")],
|
| 76 |
+
[os.path.abspath("Russian-Putin.mp4")],
|
| 77 |
+
[os.path.abspath("Italian-Conte.mp4")],
|
| 78 |
+
[os.path.abspath("Japanese-DragonBall.mp4")]
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
initial_language = gr.inputs.Dropdown(["English","Italian","Japanese","Russian","Spanish","German"])
|
| 83 |
+
final_language = gr.inputs.Dropdown([ "Russian","Italian","Spanish","German","English"])
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
gr.Interface(fn = video_to_translate,
|
| 87 |
+
inputs = ['file',initial_language,final_language],
|
| 88 |
+
outputs = 'video',
|
| 89 |
+
verbose = True,
|
| 90 |
+
title = 'Video Translator',
|
| 91 |
+
description = 'A simple application that translate from English,Italian ,Japanese ,Russian ,Spanish and German video files to Italian, Spanish, Russian or English . Upload your own file, or click one of the examples to load them. Wait one minute to process.',
|
| 92 |
+
article =
|
| 93 |
+
'''<div>
|
| 94 |
+
<p style="text-align: center"> All you need to do is to upload the mp4 file and hit submit, then wait for compiling. After that click on Play/Pause for listing to the video. The video is saved in a mp4 format.
|
| 95 |
+
For more information visit <a href="https://ruslanmv.com/">ruslanmv.com</a>
|
| 96 |
+
</p>
|
| 97 |
+
</div>''',
|
| 98 |
+
examples=examples
|
| 99 |
+
).launch()
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|