Spaces:
Paused
Paused
| from flask import Flask, render_template, request, redirect, url_for, send_file | |
| import moviepy.editor as mp | |
| import cv2 | |
| from gtts import gTTS | |
| import os | |
| import progress | |
| import uuid | |
| import requests | |
| from bs4 import BeautifulSoup | |
| app = Flask(__name__) | |
| def index(): | |
| return render_template('index.html') | |
| def dub(): | |
| # Get the video source from the user | |
| source = request.form.get('source') | |
| if source == 'url': | |
| url = request.form.get('url') | |
| # Download the video from the URL | |
| response = requests.get(url) | |
| file = "video.mp4" | |
| with open(file, "wb") as f: | |
| f.write(response.content) | |
| else: | |
| file = request.files['file'] | |
| filename = str(uuid.uuid4().hex) + ".mp4" | |
| file.save(filename) | |
| # Load the video using moviepy | |
| video = mp.VideoFileClip(file) | |
| # Create the audio in Hindi using gTTS | |
| audio = gTTS(video.audio.fps, 'hi') | |
| audio_filename = str(uuid.uuid4().hex) + ".mp3" | |
| audio.save(audio_filename) | |
| # Replace the audio of the video with the Hindi audio | |
| video.audio = mp.AudioFileClip(audio_filename) | |
| # Save the dubbed video | |
| dubbed_filename = str(uuid.uuid4().hex) + ".mp4" | |
| video.write_videofile(dubbed_filename, progress_bar=progress.bar.IncrementalBar) | |
| # Delete the temporary audio and video files | |
| os.remove(file) | |
| os.remove(audio_filename) | |
| return redirect(url_for('download', filename=dubbed_filename)) | |
| def download(filename): | |
| return send_file(filename, as_attachment=True) | |
| if __name__ == '__main__': | |
| app.run(host="0.0.0.0",port=7860,debug=True) |