from flask import Flask, request, send_file
from moviepy.editor import VideoFileClip, AudioFileClip
import requests, os, uuid
app = Flask(__name__)
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# 🔑 Replace this with your real ElevenLabs API key
ELEVENLABS_API_KEY = "YOUR_ELEVENLABS_API_KEY"
VOICE_MAP = {
"female1": "pNInz6obpgDQGcFmaJgB",
"female2": "21m00Tcm4TlvDq8ikWAM",
"female3": "AZnzlk1XvdvUeBnXmlld",
"female4": "b0ef0e06-aeab-11ed-afa1-0242ac120002",
"female5": "EXAVITQu4vr4xnSDxMaL",
"male1": "TxGEqnHWrfWFTfGW9XjX",
"male2": "VR6AewLTigWG4xSOukaG",
"male3": "ErXwobaYiN019PkySvjV",
"male4": "D38z5RcWu1voky8WS1ja",
"male5": "onwK4eW90p33ChLG9b0s"
}
@app.route('/', methods=['GET'])
def home():
return '''
AI Video Voice Changer
🎙️ Upload Video & Change Voice
Final video will download automatically after processing.
'''
@app.route('/upload', methods=['POST'])
def upload():
try:
video = request.files['video']
voice_key = request.form['voice']
voice_id = VOICE_MAP.get(voice_key)
filename = str(uuid.uuid4())
video_path = os.path.join(UPLOAD_FOLDER, filename + ".mp4")
audio_path = os.path.join(UPLOAD_FOLDER, filename + "_voice.mp3")
final_path = os.path.join(UPLOAD_FOLDER, filename + "_final.mp4")
video.save(video_path)
# 🧠 For demo: use fixed text (use Whisper later to extract)
text = "This is an AI-generated voice-over using ElevenLabs for your uploaded video."
# 🔁 Generate voice using ElevenLabs
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
headers = {
"xi-api-key": ELEVENLABS_API_KEY,
"Content-Type": "application/json"
}
data = {
"text": text,
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5
}
}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
return f"Voice generation failed: {response.text}", 500
with open(audio_path, 'wb') as f:
f.write(response.content)
# 🎬 Replace audio in video
clip = VideoFileClip(video_path)
new_audio = AudioFileClip(audio_path).set_duration(clip.duration)
final = clip.set_audio(new_audio)
final.write_videofile(final_path, codec="libx264", audio_codec="aac")
return send_file(final_path, as_attachment=True)
except Exception as e:
return f"❌ Error: {str(e)}", 500
if __name__ == '__main__':
app.run(debug=True)