Create video_processor.py
Browse files- video_processor.py +88 -0
video_processor.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from moviepy.editor import *
|
| 4 |
+
from moviepy.config import change_settings
|
| 5 |
+
from elevenlabs import generate, save
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
# Configure moviepy to use ImageMagick if needed
|
| 9 |
+
change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
|
| 10 |
+
|
| 11 |
+
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 12 |
+
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 13 |
+
|
| 14 |
+
def create_video(video_script, audio_script, summary):
|
| 15 |
+
"""Generate complete educational video"""
|
| 16 |
+
try:
|
| 17 |
+
# Step 1: Generate AI voiceover
|
| 18 |
+
audio = generate(
|
| 19 |
+
text=audio_script,
|
| 20 |
+
voice="Rachel",
|
| 21 |
+
model="eleven_monolingual_v2",
|
| 22 |
+
api_key=ELEVENLABS_API_KEY
|
| 23 |
+
)
|
| 24 |
+
audio_path = os.path.join(tempfile.gettempdir(), "narration.mp3")
|
| 25 |
+
save(audio, audio_path)
|
| 26 |
+
|
| 27 |
+
# Step 2: Parse video script for scenes
|
| 28 |
+
scenes = []
|
| 29 |
+
for scene_text in video_script.split("\n\n"):
|
| 30 |
+
if "[Visual]:" in scene_text:
|
| 31 |
+
scene = {
|
| 32 |
+
"visual": scene_text.split("[Visual]:")[1].split("[Voiceover]:")[0].strip(),
|
| 33 |
+
"voiceover": scene_text.split("[Voiceover]:")[1].strip()
|
| 34 |
+
}
|
| 35 |
+
scenes.append(scene)
|
| 36 |
+
|
| 37 |
+
# Step 3: Get Pexels footage for each scene
|
| 38 |
+
video_clips = []
|
| 39 |
+
for scene in scenes:
|
| 40 |
+
# Get stock footage
|
| 41 |
+
pexels_response = requests.get(
|
| 42 |
+
f"https://api.pexels.com/videos/search?query={scene['visual']}&per_page=1",
|
| 43 |
+
headers={"Authorization": PEXELS_API_KEY}
|
| 44 |
+
)
|
| 45 |
+
video_url = pexels_response.json()['videos'][0]['video_files'][0]['link']
|
| 46 |
+
|
| 47 |
+
# Download and process clip
|
| 48 |
+
clip = VideoFileClip(video_url).subclip(0, 8) # 8 sec per scene
|
| 49 |
+
clip = clip.resize(height=1080) # Standardize size
|
| 50 |
+
video_clips.append(clip)
|
| 51 |
+
|
| 52 |
+
# Step 4: Combine all clips
|
| 53 |
+
final_video = concatenate_videoclips(video_clips)
|
| 54 |
+
|
| 55 |
+
# Step 5: Add audio
|
| 56 |
+
audio_clip = AudioFileClip(audio_path)
|
| 57 |
+
final_video = final_video.set_audio(audio_clip)
|
| 58 |
+
|
| 59 |
+
# Step 6: Add subtitles and watermark
|
| 60 |
+
def add_subtitle(txt):
|
| 61 |
+
return TextClip(
|
| 62 |
+
txt, fontsize=40, color='white',
|
| 63 |
+
font='Arial-Bold', stroke_color='black', stroke_width=1
|
| 64 |
+
).set_position(('center', 'bottom')).set_duration(8)
|
| 65 |
+
|
| 66 |
+
subtitles = [add_subtitle(scene['voiceover']) for scene in scenes]
|
| 67 |
+
subtitles = concatenate_videoclips(subtitles)
|
| 68 |
+
|
| 69 |
+
watermark = (TextClip("MentorMindz", fontsize=30, color='white')
|
| 70 |
+
.set_position(('right', 'top'))
|
| 71 |
+
.set_duration(final_video.duration))
|
| 72 |
+
|
| 73 |
+
final_video = CompositeVideoClip([final_video, subtitles, watermark])
|
| 74 |
+
|
| 75 |
+
# Save final video
|
| 76 |
+
output_path = os.path.join(tempfile.gettempdir(), "final_output.mp4")
|
| 77 |
+
final_video.write_videofile(
|
| 78 |
+
output_path,
|
| 79 |
+
codec='libx264',
|
| 80 |
+
audio_codec='aac',
|
| 81 |
+
fps=24
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Upload to storage (in production use S3/Cloudinary)
|
| 85 |
+
return f"https://your-storage.com/{output_path.split('/')[-1]}"
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
raise Exception(f"Video creation failed: {str(e)}")
|