|
import os |
|
import time |
|
import tempfile |
|
import google.generativeai as genai |
|
import requests |
|
from flask import Flask, request, render_template, send_from_directory, url_for, flash |
|
from moviepy.video.io.VideoFileClip import VideoFileClip |
|
from moviepy.audio.io.AudioFileClip import AudioFileClip |
|
from werkzeug.utils import secure_filename |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") |
|
TTS_API_URL = os.getenv("TTS_API_URL") |
|
|
|
|
|
if not GEMINI_API_KEY: |
|
raise ValueError("SECURITY ERROR: GEMINI_API_KEY secret not found! Please set it as an environment variable.") |
|
if not TTS_API_URL: |
|
raise ValueError("CONFIGURATION ERROR: TTS_API_URL secret not found! Please set it as an environment variable.") |
|
|
|
|
|
genai.configure(api_key=GEMINI_API_KEY) |
|
|
|
|
|
UPLOAD_FOLDER = 'uploads' |
|
DOWNLOAD_FOLDER = 'downloads' |
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True) |
|
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True) |
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
|
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER |
|
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 |
|
app.secret_key = 'supersecretkey' |
|
|
|
|
|
|
|
VOICE_CHOICES = { |
|
"Male (Charon)": "Charon", |
|
"Female (Zephyr)": "Zephyr" |
|
} |
|
|
|
GEMINI_PROMPT = """ |
|
You are an AI scriptwriter. Your task is to watch the provided video and transcribe ALL spoken dialogue into a SINGLE, CONTINUOUS block of modern, colloquial Tamil. |
|
|
|
**CRITICAL INSTRUCTIONS:** |
|
1. **Single Script:** Combine all dialogue into one continuous script. |
|
2. **NO Timestamps or Speaker Labels:** Do NOT include any timestamps or speaker identifiers. |
|
3. **Incorporate Performance:** Add English style prompts (e.g., `Say happily:`, `Whisper mysteriously:`) and performance tags (e.g., `[laugh]`, `[sigh]`) directly into the text for an expressive narration. |
|
|
|
**EXAMPLE OUTPUT:** |
|
Say happily: வணக்கம்! [laugh] எப்படி இருக்கீங்க? Whisper mysteriously: அந்த ரகசியம் எனக்கு மட்டும் தான் தெரியும். Shout angrily: உடனே இங்கிருந்து போ! |
|
""" |
|
|
|
|
|
|
|
def generate_tamil_script(video_file_path): |
|
"""Generates a single, continuous Tamil script from the video using Gemini.""" |
|
print("Uploading file to Gemini for transcription...") |
|
video_file = genai.upload_file(video_file_path) |
|
|
|
print("Waiting for Gemini file processing...") |
|
while video_file.state.name == "PROCESSING": |
|
time.sleep(5) |
|
video_file = genai.get_file(video_file.name) |
|
if video_file.state.name != "ACTIVE": |
|
raise Exception(f"Gemini file processing failed: {video_file.state.name}") |
|
|
|
print("Generating narrator script...") |
|
model = genai.GenerativeModel(model_name="models/gemini-2.5-flash") |
|
response = model.generate_content([GEMINI_PROMPT, video_file]) |
|
genai.delete_file(video_file.name) |
|
|
|
if response.text: |
|
return " ".join(response.text.strip().splitlines()) |
|
raise Exception("No valid script was generated by Gemini.") |
|
|
|
def generate_single_audio_track(dialogue_text, voice_name, is_cheerful, output_path): |
|
"""Generates one audio track for the entire script via TTS API.""" |
|
print(f"Requesting audio from TTS API (Voice: {voice_name}, Cheerful: {is_cheerful})") |
|
payload = {"text": dialogue_text, "voice_name": voice_name, "cheerful": is_cheerful} |
|
response = requests.post(TTS_API_URL, json=payload, timeout=300) |
|
if response.status_code == 200: |
|
with open(output_path, "wb") as f: |
|
f.write(response.content) |
|
print(f"Audio track saved to {output_path}") |
|
return True |
|
raise Exception(f"Error from TTS API: {response.status_code} - {response.text}") |
|
|
|
def replace_video_audio(video_path, new_audio_path, output_path): |
|
""" |
|
Replaces the audio of a video with a new audio file. |
|
This function is robustly designed to ensure file handles are always closed. |
|
""" |
|
print("Replacing video audio using MoviePy...") |
|
video_clip = None |
|
audio_clip = None |
|
final_clip = None |
|
try: |
|
video_clip = VideoFileClip(video_path) |
|
audio_clip = AudioFileClip(new_audio_path) |
|
|
|
|
|
final_clip = video_clip.set_audio(audio_clip) |
|
|
|
|
|
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", logger='bar') |
|
print(f"Successfully created final video at {output_path}") |
|
|
|
except Exception as e: |
|
print(f"FATAL ERROR in replace_video_audio: {e}") |
|
raise |
|
finally: |
|
|
|
if video_clip: |
|
video_clip.close() |
|
if audio_clip: |
|
audio_clip.close() |
|
if final_clip: |
|
final_clip.close() |
|
|
|
|
|
|
|
@app.route('/', methods=['GET']) |
|
def index(): |
|
"""Renders the main upload page.""" |
|
return render_template('index.html') |
|
|
|
@app.route('/process', methods=['POST']) |
|
def process_video(): |
|
"""Handles video upload, processing, and renders the result.""" |
|
if 'video' not in request.files: |
|
flash("No video file selected. Please choose a file to upload.") |
|
return render_template('index.html') |
|
|
|
file = request.files['video'] |
|
if file.filename == '': |
|
flash("No video file selected. Please choose a file to upload.") |
|
return render_template('index.html') |
|
|
|
try: |
|
filename = secure_filename(file.filename) |
|
upload_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) |
|
file.save(upload_path) |
|
|
|
voice_choice = request.form['voice_choice'] |
|
is_cheerful = 'cheerful' in request.form |
|
voice_name = VOICE_CHOICES[voice_choice] |
|
|
|
|
|
script = generate_tamil_script(upload_path) |
|
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio: |
|
temp_audio_path = temp_audio.name |
|
|
|
generate_single_audio_track(script, voice_name, is_cheerful, temp_audio_path) |
|
|
|
final_video_name = f"dubbed_{filename}" |
|
final_video_path = os.path.join(app.config['DOWNLOAD_FOLDER'], final_video_name) |
|
|
|
replace_video_audio(upload_path, temp_audio_path, final_video_path) |
|
|
|
|
|
os.remove(temp_audio_path) |
|
|
|
return render_template('index.html', |
|
result_video=url_for('serve_video', filename=final_video_name), |
|
script=script) |
|
|
|
except Exception as e: |
|
print(f"An error occurred during processing: {e}") |
|
flash(f"An error occurred: {e}") |
|
return render_template('index.html') |
|
|
|
@app.route('/downloads/<filename>') |
|
def serve_video(filename): |
|
"""Serves the final dubbed video for display on the webpage.""" |
|
return send_from_directory(app.config['DOWNLOAD_FOLDER'], filename) |
|
|
|
|
|
if __name__ == '__main__': |
|
app.run(host="0.0.0.0", port=7860) |