|
import streamlit as st |
|
from moviepy.editor import VideoFileClip |
|
import whisper |
|
from translate import Translator |
|
from gtts import gTTS |
|
import tempfile |
|
import os |
|
import numpy as np |
|
|
|
|
|
try: |
|
whisper_model = whisper.load_model("base") |
|
except AttributeError: |
|
st.error("Whisper model could not be loaded. Ensure that Whisper is installed from GitHub.") |
|
|
|
|
|
LANGUAGES = { |
|
'English': 'en', |
|
'Tamil': 'ta', |
|
'Sinhala': 'si', |
|
'French': 'fr', |
|
} |
|
|
|
st.title("AI Video Translator with Whisper and GTTS") |
|
|
|
|
|
video_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"]) |
|
|
|
if video_file: |
|
|
|
target_language = st.selectbox("Select the target language for translation", list(LANGUAGES.keys())) |
|
|
|
|
|
if st.button("Translate Video"): |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video: |
|
temp_video.write(video_file.read()) |
|
temp_video_path = temp_video.name |
|
|
|
|
|
try: |
|
video = VideoFileClip(temp_video_path) |
|
audio_path = tempfile.mktemp(suffix=".wav") |
|
video.audio.write_audiofile(audio_path) |
|
except Exception as e: |
|
st.error(f"Error extracting audio from video: {e}") |
|
os.remove(temp_video_path) |
|
st.stop() |
|
|
|
|
|
def transcribe_audio_in_chunks(audio_path, model, chunk_length=30): |
|
audio_clip = whisper.load_audio(audio_path) |
|
audio_duration = whisper.get_duration(audio_clip) |
|
segments = [] |
|
|
|
for start in np.arange(0, audio_duration, chunk_length): |
|
end = min(start + chunk_length, audio_duration) |
|
segment = audio_clip[int(start * 16000):int(end * 16000)] |
|
result = model.transcribe(segment) |
|
segments.append(result['text']) |
|
|
|
return ' '.join(segments) |
|
|
|
|
|
try: |
|
original_text = transcribe_audio_in_chunks(audio_path, whisper_model) |
|
st.write("Original Transcription:", original_text) |
|
|
|
|
|
translator = Translator(to_lang=LANGUAGES[target_language]) |
|
translated_text = translator.translate(original_text) |
|
st.write(f"Translated Text ({target_language}):", translated_text) |
|
|
|
|
|
tts = gTTS(text=translated_text, lang=LANGUAGES[target_language]) |
|
audio_output_path = tempfile.mktemp(suffix=".mp3") |
|
tts.save(audio_output_path) |
|
|
|
|
|
st.success("Translation successful!") |
|
st.audio(audio_output_path, format="audio/mp3") |
|
except Exception as e: |
|
st.error(f"Error during transcription/translation: {e}") |
|
|
|
|
|
os.remove(temp_video_path) |
|
os.remove(audio_path) |
|
os.remove(audio_output_path) |
|
|