imseldrith commited on
Commit
415735d
·
1 Parent(s): 4128a8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -37
app.py CHANGED
@@ -1,39 +1,58 @@
1
- import gradio as gr
2
- import moviepy.editor as mp
3
- import numpy as np
4
- import IPython.display as ipd
5
- import librosa
6
- import soundfile as sf
7
  from gtts import gTTS
8
- from io import BytesIO
9
-
10
- # function to convert English audio to Hindi
11
- def convert_to_hindi(audio):
12
- tts = gTTS(audio, lang='hi')
13
- audio = tts.get_wav_data()
14
- return audio
15
-
16
- # function to extract audio from video and dub it to Hindi
17
- def dub_video(video_file):
18
- # extract audio from video
19
- video = mp.VideoFileClip(video_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  audio = video.audio
21
- audio_array = audio.to_soundarray()
22
-
23
- # convert audio to Hindi
24
- audio_text = librosa.core.to_text(audio_array)
25
- audio_hindi = convert_to_hindi(audio_text)
26
-
27
- # add Hindi audio to video
28
- audio_hindi = mp.AudioFileClip(BytesIO(audio_hindi))
29
- video_dubbed = video.set_audio(audio_hindi)
30
-
31
- return video_dubbed
32
-
33
- # Gradio interface
34
- def dub_video_interface(inputs):
35
- video_file = inputs[0]
36
- video_dubbed = dub_video(video_file)
37
- return video_dubbed
38
-
39
- gr.Interface(fn=dub_video_interface, inputs=gr.Video(),outputs=gr.Video()).launch(debug=True)
 
 
 
 
1
+ import os
2
+ import uuid
3
+ from flask import Flask, request, render_template, redirect, url_for
 
 
 
4
  from gtts import gTTS
5
+ from moviepy.editor import VideoFileClip, AudioFileClip
6
+ from tqdm import tqdm
7
+ import requests
8
+
9
+ app = Flask(__name__)
10
+ app.secret_key = os.environ.get('SECRET_KEY') or str(uuid.uuid4())
11
+
12
+ @app.route('/', methods=['GET', 'POST'])
13
+ def index():
14
+ if request.method == 'POST':
15
+ if 'video_file' in request.files:
16
+ video_file = request.files['video_file']
17
+ elif 'video_url' in request.form:
18
+ video_url = request.form['video_url']
19
+ response = requests.get(video_url)
20
+ video_file = response.content
21
+
22
+ language = request.form['language']
23
+ dubbed_video_file = generate_dubbed_video(video_file, language)
24
+
25
+ return redirect(url_for('download', video_id=dubbed_video_file))
26
+
27
+ return render_template('index.html')
28
+
29
+ @app.route('/download/<video_id>')
30
+ def download(video_id):
31
+ return render_template('download.html', video_id=video_id)
32
+
33
+ def generate_dubbed_video(video_file, language):
34
+ video_id = str(uuid.uuid4())
35
+ video = VideoFileClip(video_file)
36
  audio = video.audio
37
+ text = audio.to_soundarray().tobytes()
38
+
39
+ try:
40
+ tts = gTTS(text=text.decode(), lang=language)
41
+ except UnicodeDecodeError:
42
+ tts = gTTS(text=text.decode(errors='ignore'), lang=language)
43
+
44
+ tts.save(f'{video_id}.mp3')
45
+ audio_clip = AudioFileClip(f'{video_id}.mp3')
46
+
47
+ with tqdm(total=100, desc="Dubbing Progress", unit="%") as progress_bar:
48
+ for i, frame in enumerate(video.iter_frames()):
49
+ video.set_frame(i)
50
+ audio_frame = audio_clip.get_frame(i / video.fps)
51
+ video.audio = audio_frame
52
+ progress_bar.update(i / len(video.iter_frames()) * 100)
53
+
54
+ video.write_videofile(f'{video_id}.mp4', fps=video.fps)
55
+ return video_id
56
+
57
+ if __name__ == '__main__':
58
+ app.run(host="0.0.0.0",port=7860,debug=True)