imseldrith commited on
Commit
7a33b3e
·
1 Parent(s): 1c3ba84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -31
app.py CHANGED
@@ -1,40 +1,60 @@
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
- # run the Gradio interface
40
- gr.Interface(fn=dub_video_interface, inputs=gr.Video(label="Upload a video"), outputs=gr.Video(label="Dubbed Video"),title="Auto Dubbing").launch()
 
1
+ import os
2
+ import uuid
3
+ import requests
4
+
5
+ from flask import Flask, render_template, request, redirect, url_for, flash
 
6
  from gtts import gTTS
7
+ from moviepy.editor import VideoFileClip, AudioFileClip
8
 
9
+ app = Flask(__name__)
10
+ app.secret_key = "secret_key"
 
 
 
11
 
12
+ def generate_dubbed_video(video_file, language):
13
+ # Extract audio from video
14
+ video = VideoFileClip(video_file)
 
15
  audio = video.audio
16
+ audio_file = f"{uuid.uuid4()}.mp3"
17
+ audio.write_audiofile(audio_file)
18
+
19
+ # Generate dubbed audio
20
+ tts = gTTS(text=audio.to_soundarray().tobytes().decode(), lang=language)
21
+ dubbed_audio_file = f"{uuid.uuid4()}.mp3"
22
+ tts.save(dubbed_audio_file)
23
 
24
+ # Replace video audio with dubbed audio
25
+ dubbed_audio = AudioFileClip(dubbed_audio_file)
26
+ dubbed_video = video.set_audio(dubbed_audio)
27
+ dubbed_video_file = f"{uuid.uuid4()}.mp4"
28
+ dubbed_video.write_videofile(dubbed_video_file)
29
 
30
+ # Clean up temporary audio and video files
31
+ os.remove(audio_file)
32
+ os.remove(dubbed_audio_file)
33
+
34
+ return dubbed_video_file
35
+
36
+ @app.route("/", methods=["GET", "POST"])
37
+ def index():
38
+ if request.method == "POST":
39
+ if "video_url" in request.form:
40
+ video_url = request.form["video_url"]
41
+ video_file = f"{uuid.uuid4()}.mp4"
42
+ response = requests.get(video_url)
43
+ open(video_file, "wb").write(response.content)
44
+ elif "video_file" in request.files:
45
+ video_file = request.files["video_file"]
46
+ video_file.save(f"{uuid.uuid4()}.mp4")
47
+
48
+ language = request.form.get("language")
49
+ dubbed_video_file = generate_dubbed_video(video_file, language)
50
+ flash("Dubbed video generated successfully!")
51
+ return redirect(url_for("download", dubbed_video_file=dubbed_video_file))
52
 
53
+ return render_template("index.html")
54
 
55
+ @app.route("/download/<dubbed_video_file>")
56
+ def download(dubbed_video_file):
57
+ return redirect(url_for("static", filename=dubbed_video_file))
 
 
58
 
59
+ if __name__ == "__main__":
60
+ app.run(host="0.0.0.0",port=7860,debug=True)