| import os |
| import requests |
| from moviepy.editor import * |
| from moviepy.config import change_settings |
| from elevenlabs import generate, save |
| import tempfile |
|
|
| |
| change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"}) |
|
|
| PEXELS_API_KEY = os.getenv("PEXELS_API_KEY") |
| ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY") |
|
|
| def create_video(video_script, audio_script, summary): |
| """Generate complete educational video""" |
| try: |
| |
| audio = generate( |
| text=audio_script, |
| voice="Rachel", |
| model="eleven_monolingual_v2", |
| api_key=ELEVENLABS_API_KEY |
| ) |
| audio_path = os.path.join(tempfile.gettempdir(), "narration.mp3") |
| save(audio, audio_path) |
| |
| |
| scenes = [] |
| for scene_text in video_script.split("\n\n"): |
| if "[Visual]:" in scene_text: |
| scene = { |
| "visual": scene_text.split("[Visual]:")[1].split("[Voiceover]:")[0].strip(), |
| "voiceover": scene_text.split("[Voiceover]:")[1].strip() |
| } |
| scenes.append(scene) |
| |
| |
| video_clips = [] |
| for scene in scenes: |
| |
| pexels_response = requests.get( |
| f"https://api.pexels.com/videos/search?query={scene['visual']}&per_page=1", |
| headers={"Authorization": PEXELS_API_KEY} |
| ) |
| video_url = pexels_response.json()['videos'][0]['video_files'][0]['link'] |
| |
| |
| clip = VideoFileClip(video_url).subclip(0, 8) |
| clip = clip.resize(height=1080) |
| video_clips.append(clip) |
| |
| |
| final_video = concatenate_videoclips(video_clips) |
| |
| |
| audio_clip = AudioFileClip(audio_path) |
| final_video = final_video.set_audio(audio_clip) |
| |
| |
| def add_subtitle(txt): |
| return TextClip( |
| txt, fontsize=40, color='white', |
| font='Arial-Bold', stroke_color='black', stroke_width=1 |
| ).set_position(('center', 'bottom')).set_duration(8) |
| |
| subtitles = [add_subtitle(scene['voiceover']) for scene in scenes] |
| subtitles = concatenate_videoclips(subtitles) |
| |
| watermark = (TextClip("MentorMindz", fontsize=30, color='white') |
| .set_position(('right', 'top')) |
| .set_duration(final_video.duration)) |
| |
| final_video = CompositeVideoClip([final_video, subtitles, watermark]) |
| |
| |
| output_path = os.path.join(tempfile.gettempdir(), "final_output.mp4") |
| final_video.write_videofile( |
| output_path, |
| codec='libx264', |
| audio_codec='aac', |
| fps=24 |
| ) |
| |
| |
| return f"https://your-storage.com/{output_path.split('/')[-1]}" |
| |
| except Exception as e: |
| raise Exception(f"Video creation failed: {str(e)}") |