imseldrith commited on
Commit
625fdcb
·
1 Parent(s): cb7c94c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -38
app.py CHANGED
@@ -1,59 +1,64 @@
1
- from flask import Flask, render_template, request, redirect, url_for, send_file
2
  import moviepy.editor as mp
 
3
  import cv2
4
  from gtts import gTTS
5
- import os
6
- import progress
7
- import uuid
8
  import requests
9
- from bs4 import BeautifulSoup
10
 
11
  app = Flask(__name__)
 
12
 
13
- @app.route('/')
14
  def index():
15
- return render_template('index.html')
16
 
17
- @app.route('/dub', methods=['POST'])
18
  def dub():
19
- # Get the video source from the user
20
- source = request.form.get('source')
21
- if source == 'url':
22
- url = request.form.get('url')
23
- # Download the video from the URL
 
 
 
 
24
  response = requests.get(url)
25
- file = "video.mp4"
26
- with open(file, "wb") as f:
27
  f.write(response.content)
28
- else:
29
- file = request.files['file']
30
- filename = str(uuid.uuid4().hex) + ".mp4"
31
- file.save(filename)
32
-
33
- # Load the video using moviepy
34
- video = mp.VideoFileClip(file)
35
 
36
- # Create the audio in Hindi using gTTS
37
- audio = gTTS(video.audio.fps, 'hi')
38
- audio_filename = str(uuid.uuid4().hex) + ".mp3"
39
- audio.save(audio_filename)
 
 
40
 
41
- # Replace the audio of the video with the Hindi audio
42
- video.audio = mp.AudioFileClip(audio_filename)
 
 
 
 
 
 
43
 
44
- # Save the dubbed video
45
- dubbed_filename = str(uuid.uuid4().hex) + ".mp4"
46
- video.write_videofile(dubbed_filename, progress_bar=progress.bar.IncrementalBar)
47
 
48
- # Delete the temporary audio and video files
49
- os.remove(file)
50
- os.remove(audio_filename)
 
51
 
52
- return redirect(url_for('download', filename=dubbed_filename))
53
 
54
- @app.route('/download/<filename>')
55
  def download(filename):
56
- return send_file(filename, as_attachment=True)
57
 
58
- if __name__ == '__main__':
59
  app.run(host="0.0.0.0",port=7860,debug=True)
 
1
+ from flask import Flask, request, render_template, send_file
2
  import moviepy.editor as mp
3
+ import os
4
  import cv2
5
  from gtts import gTTS
6
+ import numpy as np
7
+ from progress.bar import Bar
 
8
  import requests
 
9
 
10
  app = Flask(__name__)
11
+ app.config["TEMP_FOLDER"] = "temp"
12
 
13
+ @app.route("/")
14
  def index():
15
+ return render_template("index.html")
16
 
17
+ @app.route("/dub", methods=["POST"])
18
  def dub():
19
+ # Check if a video was uploaded
20
+ if "video" in request.files:
21
+ video = request.files["video"]
22
+ filename = video.filename
23
+ video.save(os.path.join(app.config["TEMP_FOLDER"], filename))
24
+ else:
25
+ # Get video from URL
26
+ url = request.form["url"]
27
+ filename = url.split("/")[-1]
28
  response = requests.get(url)
29
+ with open(os.path.join(app.config["TEMP_FOLDER"], filename), "wb") as f:
 
30
  f.write(response.content)
 
 
 
 
 
 
 
31
 
32
+ # Load video
33
+ clip = mp.VideoFileClip(os.path.join(app.config["TEMP_FOLDER"], filename))
34
+
35
+ # Get audio
36
+ audio = clip.audio
37
+ n = len(audio.fps)
38
 
39
+ bar = Bar("Dubbing", max=n)
40
+ text = ""
41
+ for i in range(n):
42
+ bar.next()
43
+ frame = audio.get_frame(i/audio.fps)
44
+ gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
45
+ text += " ".join([str(x) for x in gray.flatten()]) + " "
46
+ bar.finish()
47
 
48
+ # Convert audio to text-to-speech in Hindi
49
+ tts = gTTS(text, lang="hi")
50
+ tts.save("temp/dubbed_audio.mp3")
51
 
52
+ # Add dubbed audio to video
53
+ dubbed_audio = mp.AudioFileClip("temp/dubbed_audio.mp3")
54
+ dubbed_video = clip.set_audio(dubbed_audio)
55
+ dubbed_video.write_videofile(os.path.join(app.config["TEMP_FOLDER"], "dubbed_" + filename))
56
 
57
+ return render_template("dub.html", filename="dubbed_" + filename)
58
 
59
+ @app.route("/download/<filename>")
60
  def download(filename):
61
+ return send_file(os.path.join(app.config["TEMP_FOLDER"], filename), as_attachment=True)
62
 
63
+ if __name__ == "__main__":
64
  app.run(host="0.0.0.0",port=7860,debug=True)