Update generate.py
Browse files- generate.py +21 -16
generate.py
CHANGED
|
@@ -3,11 +3,10 @@ import os
|
|
| 3 |
import re
|
| 4 |
import time
|
| 5 |
import textwrap
|
| 6 |
-
import urllib.parse
|
| 7 |
import numpy as np
|
| 8 |
import requests
|
| 9 |
from PIL import Image, ImageDraw, ImageFont
|
| 10 |
-
from moviepy.editor import ImageClip, concatenate_videoclips, AudioFileClip
|
| 11 |
from io import BytesIO
|
| 12 |
import asyncio
|
| 13 |
import edge_tts
|
|
@@ -18,6 +17,13 @@ FPS = 30
|
|
| 18 |
DURATION_PER_LINE = 4
|
| 19 |
PEXELS_API_KEY = os.environ.get('PEXELS_API_KEY', '')
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def clean_script(text):
|
| 22 |
text = re.sub(r'\*\*.*?\*\*', '', text)
|
| 23 |
text = re.sub(r'\(.*?\)', '', text)
|
|
@@ -29,23 +35,24 @@ def clean_script(text):
|
|
| 29 |
def get_pexels_image(keyword):
|
| 30 |
try:
|
| 31 |
headers = {'Authorization': PEXELS_API_KEY}
|
| 32 |
-
clean_keyword = re.sub(r'#\w+', '', keyword).strip()[:50]
|
| 33 |
r = requests.get(
|
| 34 |
-
f'https://api.pexels.com/v1/search?query={
|
| 35 |
headers=headers, timeout=15
|
| 36 |
)
|
| 37 |
data = r.json()
|
| 38 |
if data.get('photos'):
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
img_response = requests.get(photo_url, timeout=15)
|
| 41 |
img = Image.open(BytesIO(img_response.content)).convert('RGB')
|
| 42 |
img = img.resize((WIDTH, HEIGHT))
|
| 43 |
overlay = Image.new('RGB', (WIDTH, HEIGHT), (0, 0, 0))
|
| 44 |
img = Image.blend(img, overlay, 0.5)
|
| 45 |
-
print(f"Pexels image fetched
|
| 46 |
return img
|
| 47 |
else:
|
| 48 |
-
print("No
|
| 49 |
return None
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Pexels error: {e}")
|
|
@@ -105,21 +112,19 @@ def generate_video(script, title, description):
|
|
| 105 |
sentences = sentences[:10]
|
| 106 |
print(f"Total clips: {len(sentences)}")
|
| 107 |
|
| 108 |
-
# Get Pexels background image
|
| 109 |
-
print("Fetching Pexels background...")
|
| 110 |
-
keyword = title.replace('#shorts', '').replace('Facts', '').replace('Trending India', 'India').strip()
|
| 111 |
-
bg_image = get_pexels_image(keyword)
|
| 112 |
-
if not bg_image:
|
| 113 |
-
# Fallback keyword
|
| 114 |
-
bg_image = get_pexels_image("India")
|
| 115 |
-
|
| 116 |
clips = []
|
| 117 |
for i, sentence in enumerate(sentences):
|
| 118 |
print(f"Creating clip {i+1}/{len(sentences)}: {sentence[:40]}")
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
audio_path = f'/app/audio_{i}.mp3'
|
| 121 |
has_audio = generate_tts(sentence, audio_path)
|
| 122 |
|
|
|
|
| 123 |
frame = make_text_frame(sentence, bg_image)
|
| 124 |
|
| 125 |
if has_audio and os.path.exists(audio_path):
|
|
@@ -169,4 +174,4 @@ if __name__ == '__main__':
|
|
| 169 |
video_id = upload.upload_video(title, description)
|
| 170 |
print(f"SUCCESS: https://youtube.com/shorts/{video_id}")
|
| 171 |
else:
|
| 172 |
-
print("Video generation failed!")
|
|
|
|
| 3 |
import re
|
| 4 |
import time
|
| 5 |
import textwrap
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import requests
|
| 8 |
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
+
from moviepy.editor import ImageClip, concatenate_videoclips, AudioFileClip
|
| 10 |
from io import BytesIO
|
| 11 |
import asyncio
|
| 12 |
import edge_tts
|
|
|
|
| 17 |
DURATION_PER_LINE = 4
|
| 18 |
PEXELS_API_KEY = os.environ.get('PEXELS_API_KEY', '')
|
| 19 |
|
| 20 |
+
KEYWORDS = [
|
| 21 |
+
"india city", "india technology", "india traffic",
|
| 22 |
+
"india government", "india future", "india electric car",
|
| 23 |
+
"india startup", "india innovation", "india growth",
|
| 24 |
+
"india 2026", "india street", "india economy"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
def clean_script(text):
|
| 28 |
text = re.sub(r'\*\*.*?\*\*', '', text)
|
| 29 |
text = re.sub(r'\(.*?\)', '', text)
|
|
|
|
| 35 |
def get_pexels_image(keyword):
|
| 36 |
try:
|
| 37 |
headers = {'Authorization': PEXELS_API_KEY}
|
|
|
|
| 38 |
r = requests.get(
|
| 39 |
+
f'https://api.pexels.com/v1/search?query={keyword}&per_page=10&orientation=portrait',
|
| 40 |
headers=headers, timeout=15
|
| 41 |
)
|
| 42 |
data = r.json()
|
| 43 |
if data.get('photos'):
|
| 44 |
+
import random
|
| 45 |
+
photo = random.choice(data['photos'])
|
| 46 |
+
photo_url = photo['src']['portrait']
|
| 47 |
img_response = requests.get(photo_url, timeout=15)
|
| 48 |
img = Image.open(BytesIO(img_response.content)).convert('RGB')
|
| 49 |
img = img.resize((WIDTH, HEIGHT))
|
| 50 |
overlay = Image.new('RGB', (WIDTH, HEIGHT), (0, 0, 0))
|
| 51 |
img = Image.blend(img, overlay, 0.5)
|
| 52 |
+
print(f"Pexels image fetched for '{keyword}' ✅")
|
| 53 |
return img
|
| 54 |
else:
|
| 55 |
+
print(f"No photos for: {keyword}")
|
| 56 |
return None
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Pexels error: {e}")
|
|
|
|
| 112 |
sentences = sentences[:10]
|
| 113 |
print(f"Total clips: {len(sentences)}")
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
clips = []
|
| 116 |
for i, sentence in enumerate(sentences):
|
| 117 |
print(f"Creating clip {i+1}/{len(sentences)}: {sentence[:40]}")
|
| 118 |
|
| 119 |
+
# Different Pexels image for each clip
|
| 120 |
+
keyword = KEYWORDS[i % len(KEYWORDS)]
|
| 121 |
+
bg_image = get_pexels_image(keyword)
|
| 122 |
+
|
| 123 |
+
# TTS audio
|
| 124 |
audio_path = f'/app/audio_{i}.mp3'
|
| 125 |
has_audio = generate_tts(sentence, audio_path)
|
| 126 |
|
| 127 |
+
# Create frame
|
| 128 |
frame = make_text_frame(sentence, bg_image)
|
| 129 |
|
| 130 |
if has_audio and os.path.exists(audio_path):
|
|
|
|
| 174 |
video_id = upload.upload_video(title, description)
|
| 175 |
print(f"SUCCESS: https://youtube.com/shorts/{video_id}")
|
| 176 |
else:
|
| 177 |
+
print("Video generation failed!")
|