Spaces:
Build error
Build error
Rename app.file to app.py
Browse files- app.file → app.py +43 -26
app.file → app.py
RENAMED
|
@@ -1,19 +1,26 @@
|
|
|
|
|
|
|
|
| 1 |
from pytube import YouTube
|
| 2 |
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 3 |
from transformers import pipeline, WhisperProcessor, WhisperForConditionalGeneration
|
| 4 |
-
import librosa
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def download_video(url):
|
| 7 |
yt = YouTube(url)
|
| 8 |
stream = yt.streams.filter(only_audio=True).first()
|
| 9 |
-
stream.download(filename="video_audio.mp4")
|
| 10 |
-
return "video_audio.mp4"
|
| 11 |
|
| 12 |
def extract_audio(video_file):
|
| 13 |
video = VideoFileClip(video_file)
|
| 14 |
audio = video.audio
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
def speech_to_text(audio_file):
|
| 19 |
processor = WhisperProcessor.from_pretrained("openai/whisper-small")
|
|
@@ -31,30 +38,40 @@ def translate_to_hindi(text):
|
|
| 31 |
def text_to_speech(text, output_file="hindi_output.wav"):
|
| 32 |
synthesizer = pipeline("text-to-speech", model="facebook/mms-tts-hin")
|
| 33 |
speech = synthesizer(text)
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
def merge_audio_to_video(video_file, audio_file, output_video="hindi_dubbed.mp4"):
|
| 38 |
video = VideoFileClip(video_file)
|
| 39 |
audio = AudioFileClip(audio_file)
|
|
|
|
| 40 |
video = video.set_audio(audio)
|
| 41 |
-
video.write_videofile(
|
| 42 |
-
return
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, send_from_directory, redirect, url_for
|
| 2 |
+
import os
|
| 3 |
from pytube import YouTube
|
| 4 |
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 5 |
from transformers import pipeline, WhisperProcessor, WhisperForConditionalGeneration
|
| 6 |
+
import librosa
|
| 7 |
+
import scipy
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
app.config['UPLOAD_FOLDER'] = 'static'
|
| 11 |
|
| 12 |
def download_video(url):
|
| 13 |
yt = YouTube(url)
|
| 14 |
stream = yt.streams.filter(only_audio=True).first()
|
| 15 |
+
stream.download(filename=os.path.join(app.config['UPLOAD_FOLDER'], "video_audio.mp4"))
|
| 16 |
+
return os.path.join(app.config['UPLOAD_FOLDER'], "video_audio.mp4")
|
| 17 |
|
| 18 |
def extract_audio(video_file):
|
| 19 |
video = VideoFileClip(video_file)
|
| 20 |
audio = video.audio
|
| 21 |
+
audio_file = os.path.join(app.config['UPLOAD_FOLDER'], "output_audio.wav")
|
| 22 |
+
audio.write_audiofile(audio_file)
|
| 23 |
+
return audio_file
|
| 24 |
|
| 25 |
def speech_to_text(audio_file):
|
| 26 |
processor = WhisperProcessor.from_pretrained("openai/whisper-small")
|
|
|
|
| 38 |
def text_to_speech(text, output_file="hindi_output.wav"):
|
| 39 |
synthesizer = pipeline("text-to-speech", model="facebook/mms-tts-hin")
|
| 40 |
speech = synthesizer(text)
|
| 41 |
+
output_path = os.path.join(app.config['UPLOAD_FOLDER'], output_file)
|
| 42 |
+
scipy.io.wavfile.write(output_path, rate=speech["sampling_rate"], data=speech["audio"])
|
| 43 |
+
return output_path
|
| 44 |
|
| 45 |
def merge_audio_to_video(video_file, audio_file, output_video="hindi_dubbed.mp4"):
|
| 46 |
video = VideoFileClip(video_file)
|
| 47 |
audio = AudioFileClip(audio_file)
|
| 48 |
+
output_path = os.path.join(app.config['UPLOAD_FOLDER'], output_video)
|
| 49 |
video = video.set_audio(audio)
|
| 50 |
+
video.write_videofile(output_path)
|
| 51 |
+
return output_path
|
| 52 |
+
|
| 53 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 54 |
+
def index():
|
| 55 |
+
if request.method == 'POST':
|
| 56 |
+
video_url = request.form['video_url']
|
| 57 |
+
video_file = download_video(video_url)
|
| 58 |
+
audio_file = extract_audio(video_file)
|
| 59 |
+
transcription = speech_to_text(audio_file)
|
| 60 |
+
hindi_text = translate_to_hindi(transcription)
|
| 61 |
+
hindi_audio = text_to_speech(hindi_text)
|
| 62 |
+
output_video = merge_audio_to_video(video_file, hindi_audio)
|
| 63 |
+
return redirect(url_for('result', filename=os.path.basename(output_video)))
|
| 64 |
+
return render_template('index.html')
|
| 65 |
+
|
| 66 |
+
@app.route('/result/<filename>')
|
| 67 |
+
def result(filename):
|
| 68 |
+
return render_template('result.html', filename=filename)
|
| 69 |
+
|
| 70 |
+
@app.route('/static/<filename>')
|
| 71 |
+
def serve_file(filename):
|
| 72 |
+
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
| 73 |
+
|
| 74 |
+
if __name__ == '__main__':
|
| 75 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 76 |
+
app.run(host='0.0.0.0', port=5000)
|
| 77 |
|