File size: 12,131 Bytes
ca002c5
 
 
 
 
 
c5e8270
b9044e8
ca002c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a216bdd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# coding=utf8
# Youtube Video Translator
# Developed by Ruslan Magana Vsevolodovna
# https://ruslanmv.com/

# importing all necessary libraries
import httpcore
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
import pathlib
import sys, os
from gtts import gTTS
import gradio as gr
import os
import speech_recognition as sr
from googletrans import Translator, constants
from pprint import pprint
from moviepy.editor import *
from pytube import YouTube
from youtube_transcript_api import YouTubeTranscriptApi
from utils import *

def download_video(url):
    print("Downloading...")
    local_file = (
        YouTube(url)
        .streams.filter(progressive=True, file_extension="mp4")
        .first()
        .download()
    )
    print("Downloaded")
    return local_file

def validate_youtube(url):
    #This creates a youtube objet
    try:
        yt = YouTube(url)  
    except Exception:
        print("Hi there URL seems invalid")
        return True
    #This will return the length of the video in sec as an int
    video_length = yt.length
    if    video_length > 600:
        print("Your video is larger than 10 minutes")
        return True
    else:
        print("Your video is less than 10 minutes")
        return False

def validate_url(url):
    import validators
    if not validators.url(url):
        print("Hi there URL seems invalid ")
        return True
    else:
        return False   


def cleanup():
    import pathlib
    import glob
    types = ('*.mp4', '*.wav') # the tuple of file types
    #Finding mp4 and wave files
    junks = []
    for files in types:
        junks.extend(glob.glob(files))
    try:    
        # Deleting those files
        for junk in junks:
            print("Deleting",junk)
            # Setting the path for the file to delete
            file = pathlib.Path(junk)
            # Calling the unlink method on the path
            file.unlink()               
    except Exception:
        print("I cannot delete the file because it is being used by another process")         

def getSize(filename):
    st = os.stat(filename)
    return st.st_size


def clean_transcript(transcript_list):
    script = ""
    for text in transcript_list:
        t = text["text"]
        if( (t != '[music]')  and  \
            (t != '[Music]')  and  \
            (t != '[музыка]') and  \
            (t != '[Музыка]') and  \
            (t != '[musik]')  and  \
            (t != '[Musik]')  and  \
            (t != '[musica]') and  \
            (t != '[Musica]') and  \
            (t != '[música]') and  \
            (t != '[Música]') and  \
            (t != '[音楽]')   and \
            (t != '[音乐]')     
          ) :
            script += t + " "
    return script
    
    
def get_transcript(url,desired_language):
    id_you= url[url.index("=")+1:]
    try: 
        # retrieve the available transcripts
        transcript_list = YouTubeTranscriptApi.list_transcripts(id_you)

    except Exception:
        print('TranscriptsDisabled:')
        is_translated = False
        return " ", " ", is_translated 

    lista=[]
    transcript_translation_languages=[]
    # iterate over all available transcripts
    for transcript in transcript_list:
        lista.extend([
        transcript.language_code,
        transcript.is_generated,
        transcript.is_translatable,
        transcript_translation_languages.append(transcript.translation_languages),
                     ])
    print(lista)
    n_size=int(len(lista)/4)
    print("There are {} avialable scripts".format(n_size))
    import numpy as np
    matrix = np.array(lista)
    shape = (n_size,4)
    matrix=matrix.reshape(shape)
    matrix=matrix.tolist()
    is_manually=False
    is_automatic=False
    for lista in matrix: 
        #print(lista)
        language_code=lista[0]
        is_generated=lista[1]
        is_translatable=lista[2]
        if not is_generated and is_translatable : 
            print("Script found manually generated")
            is_manually=True
            language_code_man=language_code
        if  is_generated and is_translatable :
            print("Script found automatic generated")
            is_automatic=True
            language_code_au=language_code
            
    if  is_manually:
        # we try filter for manually created transcripts
        print('We extract manually created transcripts')
        transcript = transcript_list.find_manually_created_transcript([language_code]) 
  
    elif is_automatic:
        print('We  extract generated transcript')
        # or automatically generated ones, but not translated
        transcript = transcript_list.find_generated_transcript([language_code])
    else:
        print('We try find the transcript')
        # we directly filter for the language you are looking for, using the transcript list
        transcript = transcript_list.find_transcript([language_code])

    is_translated = False
    if is_translatable :
        for available_trad in  transcript_translation_languages[0]:
            if available_trad['language_code']==desired_language:
                print("It was found the translation for lang:",desired_language)
                print('We translate directly the transcript')
                transcript_translated =  transcript.translate(desired_language)
                transcript_translated=transcript_translated.fetch()
                translated=clean_transcript(transcript_translated)
                is_translated = True
    script_translated = ""            
    if is_translated :
        script_translated = translated

    transcript=transcript.fetch()
    script = clean_transcript(transcript)
        
    return script, script_translated, is_translated

# Set environment variables
home_dir = os.getcwd()
temp_dir=os.path.join(home_dir, "temp")
#Create temp directory
pathlib.Path(temp_dir).mkdir(parents=True, exist_ok=True)
os.environ['home_dir'] = home_dir
os.environ['temp_dir'] = temp_dir

def video_to_translate(url,initial_language,final_language):
    print('Checking the url')
    check =validate_youtube(url)
    if check is True: return "./demo/tryagain2.mp4"

    #Internal definitions
    if initial_language == "English":
        lang_in='en-US'
        lang_api='en'
    elif initial_language == "Italian":
        lang_in='it-IT'
        lang_api='it'
    elif initial_language == "Chinese":
        lang_in='zh-CN'
        lang_api='zh'
    elif initial_language == "Spanish":
        lang_in='es-MX'
        lang_api='es'
    elif initial_language == "Russian":
        lang_in='ru-RU'
        lang_api='rus'
    elif initial_language == "German":
        lang_in='de-DE'
        lang_api='de'
    elif initial_language == "Japanese":
        lang_in='ja-JP'
        lang_api='ja'
    if final_language == "English":
        lang='en'
    elif final_language == "Italian":
        lang='it'
    elif final_language == "Spanish":
        lang='es'
    elif final_language == "Russian":
        lang='ru'
    elif final_language == "German":
        lang='de'
    elif final_language == "Vietnamese":
        lang='vi'
    elif final_language == "Japanese":
        lang='ja'        
    # Initial directory
    home_dir= os.getenv('home_dir')
    print('Initial directory:',home_dir)
    # Cleaning previous files
    cleanup()
    file_obj=download_video(url)
    print(file_obj)
# Insert Local Video File Path
    videoclip = VideoFileClip(file_obj)
    is_traduc=False
    # Trying to get transcripts

    text, trans, is_traduc = get_transcript(url,desired_language=lang)
    print("Transcript Found")

    if not is_traduc:
        print("No Transcript Found")
        # Trying to recognize audio
        # Insert Local Audio File Path
        videoclip.audio.write_audiofile("audio.wav",codec='pcm_s16le')
    # initialize the recognizer
        r = sr.Recognizer()
        # open the file
        with sr.AudioFile("audio.wav") as source:
            # listen for the data (load audio to memory)
            audio_data = r.record(source)
            # recognize (convert from speech to text)
            print("Recognize from ",lang_in)
            #There is a limit of 10 MB on all single requests sent to the API using local file
            size_wav=getSize("audio.wav")
            if  size_wav > 50000000:
                print("The wav is too large")
                audio_chunks=split_audio_wav("audio.wav")
                text=""
                for chunk in audio_chunks:
                    print("Converting audio to text",chunk)
                    try:
                        text_chunk= r.recognize_google(audio_data, language = lang_in)
                    except Exception:
                        print("This video cannot be recognized")
                        cleanup()
                        return "./demo/tryagain.mp4"
                    text=text+text_chunk+" "
                text=str(text)
                print(type(text))
                
            else:
                try:
                        text = r.recognize_google(audio_data, language = lang_in)
                except Exception:
                        print("This video cannot be recognized")
                        cleanup()
                        return "./demo/tryagain.mp4"
                
        #print(text)
        print("Destination language ",lang)

        # init the Google API translator
        translator = Translator()


        try:
            translation = translator.translate(text, dest=lang)
        except Exception:
            print("This text cannot be translated")
            cleanup()
            return "./demo/tryagain.mp4"
        
        #translation.text
        trans=translation.text

    myobj = gTTS(text=trans, lang=lang, slow=False) 
    myobj.save("audio.wav") 
    # loading audio file
    audioclip = AudioFileClip("audio.wav")
    
    # adding audio to the video clip
    new_audioclip = CompositeAudioClip([audioclip])
    videoclip.audio = new_audioclip
    new_video="video_translated_"+lang+".mp4"
  
    # Return back to main directory
    os.chdir(home_dir)
    print('Final directory',os.getcwd())

    videoclip.write_videofile(new_video)

    videoclip.close()
    del file_obj

    return new_video

initial_language = gr.inputs.Dropdown(["English","Italian","Japanese","Russian","Spanish","German"])
final_language = gr.inputs.Dropdown([ "Russian","Italian","Spanish","German","English","Japanese"])
url =gr.inputs.Textbox(label = "Enter the YouTube URL below:")


gr.Interface(fn = video_to_translate,
            inputs = [url,initial_language,final_language],
            outputs = 'video', 
            verbose = True,
            title = 'Video Youtube Translator',
            description = 'A simple application that translates Youtube small videos from English, Italian, Japanese, Russian, Spanish, and German  to  Italian, Spanish, Russian, English and Japanese.  Wait one minute to process.',
            article = 
                        '''<div>
                            <p style="text-align: center"> All you need to do is to paste the Youtube link and hit submit,, then wait for compiling. After that click on Play/Pause for listing to the video. The video is saved in an mp4 format.
                            The lenght video limit is 10 minutes. For more information visit <a href="https://ruslanmv.com/">ruslanmv.com</a>
                            </p>
                        </div>''',

           examples = [
                        ["https://www.youtube.com/watch?v=uLVRZE8OAI4", "English","Spanish"],
                        ["https://www.youtube.com/watch?v=fkGCLIQx1MI", "English","Russian"],
                        ["https://www.youtube.com/watch?v=6Q6hFtitthQ", "Italian","English"],
                        ["https://www.youtube.com/watch?v=s5XvjAC7ai8", "Russian","English"],
                        ["https://www.youtube.com/watch?v=qzzweIQoIOU", "Japanese","English"],
                        ["https://www.youtube.com/watch?v=nOGZvu6tJFE", "German","Spanish"]

                        ]           
            ).launch()