Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,31 @@
|
|
1 |
"""
|
2 |
-
Full Code: Orbit Video Engine with
|
3 |
-
|
4 |
-
This script
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
"""
|
12 |
|
13 |
-
#
|
14 |
from kokoro import KPipeline
|
15 |
-
|
16 |
import soundfile as sf
|
17 |
import torch
|
18 |
import os, time, random, math, json, tempfile, shutil, re, requests
|
19 |
-
from moviepy.editor import VideoFileClip, AudioFileClip, ImageClip, concatenate_videoclips,
|
|
|
20 |
import moviepy.video.fx.all as vfx
|
21 |
import moviepy.config as mpy_config
|
22 |
from pydub import AudioSegment
|
23 |
-
from
|
24 |
-
from PIL import Image, ImageDraw, ImageFont
|
25 |
import numpy as np
|
26 |
from bs4 import BeautifulSoup
|
27 |
from urllib.parse import quote
|
@@ -30,24 +34,25 @@ from gtts import gTTS
|
|
30 |
import cv2
|
31 |
import gradio as gr
|
32 |
|
33 |
-
#
|
34 |
-
pipeline = KPipeline(lang_code='a') #
|
35 |
mpy_config.change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
|
36 |
|
37 |
PEXELS_API_KEY = 'BhJqbcdm9Vi90KqzXKAhnEHGsuFNv4irXuOjWtT761U49lRzo03qBGna'
|
38 |
OPENROUTER_API_KEY = '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'
|
39 |
OPENROUTER_MODEL = "mistralai/mistral-small-3.1-24b-instruct:free"
|
40 |
OUTPUT_VIDEO_FILENAME = "final_video.mp4"
|
41 |
-
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
|
42 |
|
43 |
-
# These globals
|
44 |
TARGET_RESOLUTION = None
|
45 |
CAPTION_COLOR = None
|
46 |
TEMP_FOLDER = None
|
47 |
|
48 |
-
#
|
|
|
49 |
def generate_script(user_input):
|
50 |
-
"""Generate documentary script using OpenRouter API."""
|
51 |
headers = {
|
52 |
'Authorization': f'Bearer {OPENROUTER_API_KEY}',
|
53 |
'HTTP-Referer': 'https://your-domain.com',
|
@@ -73,7 +78,7 @@ Keep it natural, funny, and unpredictable (not robotic, poetic, or rhythmic).
|
|
73 |
|
74 |
No Special Formatting:
|
75 |
|
76 |
-
No bold, italics, or special characters. You are
|
77 |
|
78 |
Generalized Search Terms:
|
79 |
|
@@ -131,8 +136,10 @@ Now here is the Topic/scrip: {user_input}
|
|
131 |
|
132 |
def parse_script(script_text):
|
133 |
"""
|
134 |
-
Parse the generated script into a list of
|
135 |
-
Each clip
|
|
|
|
|
136 |
"""
|
137 |
sections = {}
|
138 |
current_title = None
|
@@ -259,7 +266,7 @@ def search_pexels_images(query, pexels_api_key):
|
|
259 |
return None
|
260 |
|
261 |
def search_google_images(query):
|
262 |
-
"""Search for images on Google Images."""
|
263 |
try:
|
264 |
search_url = f"https://www.google.com/search?q={quote(query)}&tbm=isch"
|
265 |
headers = {"User-Agent": USER_AGENT}
|
@@ -281,10 +288,10 @@ def search_google_images(query):
|
|
281 |
return None
|
282 |
|
283 |
def download_image(image_url, filename):
|
284 |
-
"""Download an image from a URL to a local file
|
285 |
try:
|
286 |
headers = {"User-Agent": USER_AGENT}
|
287 |
-
print(f"Downloading image from: {image_url}
|
288 |
response = requests.get(image_url, headers=headers, stream=True, timeout=15)
|
289 |
response.raise_for_status()
|
290 |
with open(filename, 'wb') as f:
|
@@ -298,10 +305,9 @@ def download_image(image_url, filename):
|
|
298 |
if img.mode != 'RGB':
|
299 |
img = img.convert('RGB')
|
300 |
img.save(filename)
|
301 |
-
print(f"Image validated and processed: {filename}")
|
302 |
return filename
|
303 |
except Exception as e_validate:
|
304 |
-
print(f"
|
305 |
if os.path.exists(filename):
|
306 |
os.remove(filename)
|
307 |
return None
|
@@ -319,7 +325,6 @@ def download_video(video_url, filename):
|
|
319 |
with open(filename, 'wb') as f:
|
320 |
for chunk in response.iter_content(chunk_size=8192):
|
321 |
f.write(chunk)
|
322 |
-
print(f"Video downloaded to: {filename}")
|
323 |
return filename
|
324 |
except Exception as e:
|
325 |
print(f"Video download error: {e}")
|
@@ -330,17 +335,14 @@ def download_video(video_url, filename):
|
|
330 |
def generate_media(prompt, user_image=None, current_index=0, total_segments=1):
|
331 |
"""
|
332 |
Generate a visual asset for the clip.
|
333 |
-
If user_image
|
334 |
-
use Pexels (
|
335 |
"""
|
336 |
safe_prompt = re.sub(r'[^\w\s-]', '', prompt).strip().replace(' ', '_')
|
337 |
if user_image is not None:
|
338 |
-
|
339 |
-
print(f"Using custom media provided for prompt: {prompt}")
|
340 |
return {"path": user_image, "asset_type": "image"}
|
341 |
-
|
342 |
if "news" in prompt.lower():
|
343 |
-
print(f"News query detected: {prompt}. Using Google Images...")
|
344 |
image_file = os.path.join(TEMP_FOLDER, f"{safe_prompt}_news.jpg")
|
345 |
image_url = search_google_images(prompt)
|
346 |
if image_url:
|
@@ -368,11 +370,11 @@ def generate_media(prompt, user_image=None, current_index=0, total_segments=1):
|
|
368 |
downloaded_fallback = download_image(fallback_url, fallback_file)
|
369 |
if downloaded_fallback:
|
370 |
return {"path": downloaded_fallback, "asset_type": "image"}
|
371 |
-
print(f"Failed to generate asset for: {prompt}")
|
372 |
return None
|
373 |
|
374 |
def generate_silent_audio(duration, sample_rate=24000):
|
375 |
-
"""Generate a silent WAV file
|
376 |
num_samples = int(duration * sample_rate)
|
377 |
silence = np.zeros(num_samples, dtype=np.float32)
|
378 |
silent_path = os.path.join(TEMP_FOLDER, f"silent_{int(time.time())}.wav")
|
@@ -380,7 +382,7 @@ def generate_silent_audio(duration, sample_rate=24000):
|
|
380 |
return silent_path
|
381 |
|
382 |
def generate_tts(text, voice):
|
383 |
-
"""Generate TTS audio using Kokoro
|
384 |
safe_text = re.sub(r'[^\w\s-]', '', text[:10]).strip().replace(' ', '_')
|
385 |
file_path = os.path.join(TEMP_FOLDER, f"tts_{safe_text}.wav")
|
386 |
if os.path.exists(file_path):
|
@@ -407,7 +409,7 @@ def generate_tts(text, voice):
|
|
407 |
return generate_silent_audio(duration=max(3, len(text.split()) * 0.5))
|
408 |
|
409 |
def apply_kenburns_effect(clip, target_resolution, effect_type=None):
|
410 |
-
"""Apply a
|
411 |
target_w, target_h = target_resolution
|
412 |
clip_aspect = clip.w / clip.h
|
413 |
target_aspect = target_w / target_h
|
@@ -428,32 +430,17 @@ def apply_kenburns_effect(clip, target_resolution, effect_type=None):
|
|
428 |
if effect_type is None or effect_type == "random":
|
429 |
effect_type = random.choice(available_effects)
|
430 |
if effect_type == "zoom-in":
|
431 |
-
start_zoom = 0.9
|
432 |
-
end_zoom = 1.1
|
433 |
-
start_center = (new_width / 2, new_height / 2)
|
434 |
-
end_center = start_center
|
435 |
elif effect_type == "zoom-out":
|
436 |
-
start_zoom = 1.1
|
437 |
-
end_zoom = 0.9
|
438 |
-
start_center = (new_width / 2, new_height / 2)
|
439 |
-
end_center = start_center
|
440 |
elif effect_type == "pan-left":
|
441 |
-
start_zoom = 1.0
|
442 |
-
end_zoom = 1.0
|
443 |
-
start_center = (max_offset_x + target_w / 2, (max_offset_y // 2) + target_h / 2)
|
444 |
-
end_center = (target_w / 2, (max_offset_y // 2) + target_h / 2)
|
445 |
elif effect_type == "pan-right":
|
446 |
-
start_zoom = 1.0
|
447 |
-
end_zoom = 1.0
|
448 |
-
start_center = (target_w / 2, (max_offset_y // 2) + target_h / 2)
|
449 |
-
end_center = (max_offset_x + target_w / 2, (max_offset_y // 2) + target_h / 2)
|
450 |
elif effect_type == "up-left":
|
451 |
-
start_zoom = 1.0
|
452 |
-
end_zoom = 1.0
|
453 |
-
start_center = (max_offset_x + target_w / 2, max_offset_y + target_h / 2)
|
454 |
-
end_center = (target_w / 2, target_h / 2)
|
455 |
else:
|
456 |
-
raise ValueError(f"Unsupported
|
457 |
def transform_frame(get_frame, t):
|
458 |
frame = get_frame(t)
|
459 |
ratio = t / clip.duration if clip.duration > 0 else 0
|
@@ -463,10 +450,8 @@ def apply_kenburns_effect(clip, target_resolution, effect_type=None):
|
|
463 |
crop_h = int(target_h / current_zoom)
|
464 |
current_center_x = start_center[0] + (end_center[0] - start_center[0]) * ratio
|
465 |
current_center_y = start_center[1] + (end_center[1] - start_center[1]) * ratio
|
466 |
-
min_center_x = crop_w / 2
|
467 |
-
|
468 |
-
min_center_y = crop_h / 2
|
469 |
-
max_center_y = new_height - crop_h / 2
|
470 |
current_center_x = max(min_center_x, min(current_center_x, max_center_x))
|
471 |
current_center_y = max(min_center_y, min(current_center_y, max_center_y))
|
472 |
cropped_frame = cv2.getRectSubPix(frame, (crop_w, crop_h), (current_center_x, current_center_y))
|
@@ -490,17 +475,16 @@ def resize_to_fill(clip, target_resolution):
|
|
490 |
return clip
|
491 |
|
492 |
def find_mp3_files():
|
493 |
-
"""
|
494 |
mp3_files = []
|
495 |
for root, dirs, files in os.walk('.'):
|
496 |
for file in files:
|
497 |
if file.endswith('.mp3'):
|
498 |
-
|
499 |
-
mp3_files.append(mp3_path)
|
500 |
return mp3_files[0] if mp3_files else None
|
501 |
|
502 |
def add_background_music(final_video, bg_music_volume=0.08):
|
503 |
-
"""Add background music to the final video."""
|
504 |
try:
|
505 |
bg_music_path = find_mp3_files()
|
506 |
if bg_music_path and os.path.exists(bg_music_path):
|
@@ -520,7 +504,7 @@ def add_background_music(final_video, bg_music_volume=0.08):
|
|
520 |
return final_video
|
521 |
|
522 |
def create_clip(media_path, asset_type, tts_path, duration=None, effects=None, narration_text=None, segment_index=0):
|
523 |
-
"""Create a video clip from a media asset and
|
524 |
try:
|
525 |
if not os.path.exists(media_path) or not os.path.exists(tts_path):
|
526 |
return None
|
@@ -595,7 +579,7 @@ def create_clip(media_path, asset_type, tts_path, duration=None, effects=None, n
|
|
595 |
return None
|
596 |
|
597 |
def fix_imagemagick_policy():
|
598 |
-
"""Attempt to
|
599 |
try:
|
600 |
policy_paths = [
|
601 |
"/etc/ImageMagick-6/policy.xml",
|
@@ -617,8 +601,8 @@ def fix_imagemagick_policy():
|
|
617 |
def generate_video(user_input, resolution, caption_option):
|
618 |
"""
|
619 |
Original video generation function.
|
620 |
-
This version
|
621 |
-
|
622 |
"""
|
623 |
global TARGET_RESOLUTION, CAPTION_COLOR, TEMP_FOLDER
|
624 |
if resolution == "Full HD (1920x1080)":
|
@@ -629,9 +613,7 @@ def generate_video(user_input, resolution, caption_option):
|
|
629 |
TARGET_RESOLUTION = (1920, 1080)
|
630 |
CAPTION_COLOR = "white" if caption_option == "Yes" else "transparent"
|
631 |
TEMP_FOLDER = tempfile.mkdtemp()
|
632 |
-
|
633 |
-
if not fix_success:
|
634 |
-
print("Unable to fix ImageMagick policies; proceeding with alternative methods.")
|
635 |
print("Generating script from API...")
|
636 |
script = generate_script(user_input)
|
637 |
if not script:
|
@@ -678,15 +660,16 @@ def generate_video(user_input, resolution, caption_option):
|
|
678 |
shutil.rmtree(TEMP_FOLDER)
|
679 |
return OUTPUT_VIDEO_FILENAME
|
680 |
|
681 |
-
#
|
682 |
-
|
|
|
683 |
generated_clip_data = []
|
684 |
|
685 |
def generate_script_clips(topic_input, full_script):
|
686 |
"""
|
687 |
-
Callback when
|
688 |
-
Uses full_script if provided,
|
689 |
-
Returns
|
690 |
"""
|
691 |
input_text = full_script if full_script.strip() != "" else topic_input
|
692 |
script = generate_script(input_text)
|
@@ -701,7 +684,7 @@ def generate_script_clips(topic_input, full_script):
|
|
701 |
clip_info = {
|
702 |
"prompt": media_elem.get("prompt", ""),
|
703 |
"narration": tts_elem.get("text", ""),
|
704 |
-
"custom_media":
|
705 |
}
|
706 |
clip_list.append(clip_info)
|
707 |
global generated_clip_data
|
@@ -710,25 +693,24 @@ def generate_script_clips(topic_input, full_script):
|
|
710 |
|
711 |
def update_clip_editor(clip_json):
|
712 |
"""
|
713 |
-
|
714 |
-
|
|
|
|
|
|
|
|
|
715 |
"""
|
716 |
clip_list = json.loads(clip_json)
|
717 |
editors = []
|
|
|
718 |
for idx, clip in enumerate(clip_list, start=1):
|
719 |
-
|
720 |
-
# Create an accordion for each clip.
|
721 |
-
with gr.Accordion(label=accordion_title, open=(idx<=2)) as accordion:
|
722 |
prompt_box = gr.Textbox(label="Visual Prompt", value=clip["prompt"])
|
723 |
-
|
724 |
-
custom_media_box = gr.File(label="
|
725 |
-
# Pack
|
726 |
-
|
727 |
-
|
728 |
-
"narration": narration_box,
|
729 |
-
"custom_media": custom_media_box
|
730 |
-
})
|
731 |
-
editors.append(accordion)
|
732 |
return editors
|
733 |
|
734 |
def generate_final_video(topic_input, full_script, clip_data_json, resolution, render_speed,
|
@@ -736,36 +718,37 @@ def generate_final_video(topic_input, full_script, clip_data_json, resolution, r
|
|
736 |
subtitle_enabled, font_dropdown, font_size, outline_width,
|
737 |
font_color, outline_color, subtitle_position):
|
738 |
"""
|
739 |
-
Callback
|
740 |
-
|
741 |
-
|
|
|
742 |
"""
|
743 |
-
# In a real integration you would process clip_data_json to update each clip with custom overrides.
|
744 |
print("Final settings:")
|
745 |
print(f"Resolution: {resolution}, Render Speed: {render_speed}, Video Clip %: {video_clip_percent}, Zoom/Pan: {zoom_pan}")
|
746 |
if bg_music_file is not None:
|
747 |
print("Custom background music provided.")
|
748 |
-
# For now, we simply call generate_video using topic_input and full_script.
|
749 |
video_file = generate_video(topic_input, resolution, "Yes")
|
750 |
-
|
|
|
751 |
|
752 |
-
#
|
753 |
with gr.Blocks(title="π Orbit Video Engine") as demo:
|
754 |
with gr.Row():
|
755 |
# Column 1: Content Input & Script Generation
|
756 |
with gr.Column(scale=1):
|
757 |
gr.Markdown("## 1. Content Input")
|
758 |
topic_input = gr.Textbox(label="Topic Input", placeholder="Enter your video topic here...")
|
759 |
-
full_script = gr.Textbox(label="Or Paste Full Script", placeholder="Paste full script
|
760 |
generate_script_btn = gr.Button("π Generate Script & Load Clips")
|
761 |
generated_script_disp = gr.Textbox(label="Generated Script", interactive=False, visible=False)
|
|
|
762 |
clip_data_storage = gr.Textbox(visible=False)
|
763 |
-
# Column 2: Clip Editor
|
764 |
with gr.Column(scale=1):
|
765 |
gr.Markdown("## 2. Edit Clips")
|
766 |
-
gr.Markdown("Modify each clip's
|
767 |
clip_editor_container = gr.Column(visible=False)
|
768 |
-
# Column 3: Settings & Output
|
769 |
with gr.Column(scale=1):
|
770 |
gr.Markdown("## 3. Video Settings")
|
771 |
resolution = gr.Radio(choices=["Short (1080x1920)", "Full HD (1920x1080)"],
|
@@ -788,9 +771,12 @@ with gr.Blocks(title="π Orbit Video Engine") as demo:
|
|
788 |
subtitle_position = gr.Radio(choices=["center", "bottom", "top"], label="Subtitle Position", value="center")
|
789 |
gr.Markdown("## 4. Output")
|
790 |
generate_video_btn = gr.Button("π¬ Generate Video")
|
|
|
791 |
video_preview = gr.Video(label="Generated Video")
|
792 |
download_video_file = gr.File(label="Download Video", interactive=False)
|
793 |
-
|
|
|
|
|
794 |
generate_script_btn.click(
|
795 |
fn=generate_script_clips,
|
796 |
inputs=[topic_input, full_script],
|
@@ -808,12 +794,14 @@ with gr.Blocks(title="π Orbit Video Engine") as demo:
|
|
808 |
inputs=[clip_editor_container],
|
809 |
outputs=[clip_editor_container]
|
810 |
)
|
|
|
|
|
811 |
generate_video_btn.click(
|
812 |
fn=generate_final_video,
|
813 |
inputs=[topic_input, full_script, clip_data_storage, resolution, render_speed,
|
814 |
video_clip_percent, zoom_pan, bg_music_file, bg_music_volume,
|
815 |
subtitle_enabled, font_dropdown, font_size, outline_width, font_color, outline_color, subtitle_position],
|
816 |
-
outputs=[video_preview]
|
817 |
)
|
818 |
-
|
819 |
demo.launch(share=True)
|
|
|
1 |
"""
|
2 |
+
Full Code: Orbit Video Engine with Dynamic Clip Editor and Video Output Download
|
3 |
+
|
4 |
+
This script implements a Gradio Blocks UI that:
|
5 |
+
1. Accepts a video topic or a full script.
|
6 |
+
2. Generates a documentary-style script using an AI API.
|
7 |
+
3. Parses the script into clip data.
|
8 |
+
4. Dynamically creates an editor for each clip, where users can:
|
9 |
+
- Edit the visual prompt.
|
10 |
+
- Edit the TTS (narration) text.
|
11 |
+
- Upload their own image/video to override the generated media.
|
12 |
+
5. Provides video settings (resolution, render speed, clip preferences, background music, subtitle settings).
|
13 |
+
6. Renders the video and displays it so you can preview it and download the final MP4 file.
|
14 |
+
|
15 |
+
Make sure you have all required dependencies installed.
|
16 |
"""
|
17 |
|
18 |
+
# ------------------- IMPORTS -------------------
|
19 |
from kokoro import KPipeline
|
|
|
20 |
import soundfile as sf
|
21 |
import torch
|
22 |
import os, time, random, math, json, tempfile, shutil, re, requests
|
23 |
+
from moviepy.editor import (VideoFileClip, AudioFileClip, ImageClip, concatenate_videoclips,
|
24 |
+
CompositeVideoClip, TextClip, CompositeAudioClip)
|
25 |
import moviepy.video.fx.all as vfx
|
26 |
import moviepy.config as mpy_config
|
27 |
from pydub import AudioSegment
|
28 |
+
from PIL import Image
|
|
|
29 |
import numpy as np
|
30 |
from bs4 import BeautifulSoup
|
31 |
from urllib.parse import quote
|
|
|
34 |
import cv2
|
35 |
import gradio as gr
|
36 |
|
37 |
+
# ------------------- GLOBAL CONFIGURATION -------------------
|
38 |
+
pipeline = KPipeline(lang_code='a') # American English voice (uses 'af_heart')
|
39 |
mpy_config.change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
|
40 |
|
41 |
PEXELS_API_KEY = 'BhJqbcdm9Vi90KqzXKAhnEHGsuFNv4irXuOjWtT761U49lRzo03qBGna'
|
42 |
OPENROUTER_API_KEY = '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'
|
43 |
OPENROUTER_MODEL = "mistralai/mistral-small-3.1-24b-instruct:free"
|
44 |
OUTPUT_VIDEO_FILENAME = "final_video.mp4"
|
45 |
+
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36"
|
46 |
|
47 |
+
# These globals will be set for each run
|
48 |
TARGET_RESOLUTION = None
|
49 |
CAPTION_COLOR = None
|
50 |
TEMP_FOLDER = None
|
51 |
|
52 |
+
# ------------------- HELPER FUNCTIONS -------------------
|
53 |
+
|
54 |
def generate_script(user_input):
|
55 |
+
"""Generate a documentary script using the OpenRouter API."""
|
56 |
headers = {
|
57 |
'Authorization': f'Bearer {OPENROUTER_API_KEY}',
|
58 |
'HTTP-Referer': 'https://your-domain.com',
|
|
|
78 |
|
79 |
No Special Formatting:
|
80 |
|
81 |
+
No bold, italics, or special characters. You are an assistant AI your task is to create script. You aren't a chatbot. So, don't write extra text
|
82 |
|
83 |
Generalized Search Terms:
|
84 |
|
|
|
136 |
|
137 |
def parse_script(script_text):
|
138 |
"""
|
139 |
+
Parse the generated script into a list of elements.
|
140 |
+
Each clip (segment) is represented as a pair of elements:
|
141 |
+
- A media element with a 'prompt'
|
142 |
+
- A TTS element with narration text and duration
|
143 |
"""
|
144 |
sections = {}
|
145 |
current_title = None
|
|
|
266 |
return None
|
267 |
|
268 |
def search_google_images(query):
|
269 |
+
"""Search for images on Google Images (useful for news-like queries)."""
|
270 |
try:
|
271 |
search_url = f"https://www.google.com/search?q={quote(query)}&tbm=isch"
|
272 |
headers = {"User-Agent": USER_AGENT}
|
|
|
288 |
return None
|
289 |
|
290 |
def download_image(image_url, filename):
|
291 |
+
"""Download an image from a URL to a local file and verify it."""
|
292 |
try:
|
293 |
headers = {"User-Agent": USER_AGENT}
|
294 |
+
print(f"Downloading image from: {image_url}")
|
295 |
response = requests.get(image_url, headers=headers, stream=True, timeout=15)
|
296 |
response.raise_for_status()
|
297 |
with open(filename, 'wb') as f:
|
|
|
305 |
if img.mode != 'RGB':
|
306 |
img = img.convert('RGB')
|
307 |
img.save(filename)
|
|
|
308 |
return filename
|
309 |
except Exception as e_validate:
|
310 |
+
print(f"Invalid image file: {e_validate}")
|
311 |
if os.path.exists(filename):
|
312 |
os.remove(filename)
|
313 |
return None
|
|
|
325 |
with open(filename, 'wb') as f:
|
326 |
for chunk in response.iter_content(chunk_size=8192):
|
327 |
f.write(chunk)
|
|
|
328 |
return filename
|
329 |
except Exception as e:
|
330 |
print(f"Video download error: {e}")
|
|
|
335 |
def generate_media(prompt, user_image=None, current_index=0, total_segments=1):
|
336 |
"""
|
337 |
Generate a visual asset for the clip.
|
338 |
+
If a user_image (custom media) is provided, it is used directly.
|
339 |
+
Otherwise, use Pexels (or Google Images for news queries) with fallbacks.
|
340 |
"""
|
341 |
safe_prompt = re.sub(r'[^\w\s-]', '', prompt).strip().replace(' ', '_')
|
342 |
if user_image is not None:
|
343 |
+
print(f"Using custom media for prompt: {prompt}")
|
|
|
344 |
return {"path": user_image, "asset_type": "image"}
|
|
|
345 |
if "news" in prompt.lower():
|
|
|
346 |
image_file = os.path.join(TEMP_FOLDER, f"{safe_prompt}_news.jpg")
|
347 |
image_url = search_google_images(prompt)
|
348 |
if image_url:
|
|
|
370 |
downloaded_fallback = download_image(fallback_url, fallback_file)
|
371 |
if downloaded_fallback:
|
372 |
return {"path": downloaded_fallback, "asset_type": "image"}
|
373 |
+
print(f"Failed to generate asset for prompt: {prompt}")
|
374 |
return None
|
375 |
|
376 |
def generate_silent_audio(duration, sample_rate=24000):
|
377 |
+
"""Generate a silent WAV audio file of given duration."""
|
378 |
num_samples = int(duration * sample_rate)
|
379 |
silence = np.zeros(num_samples, dtype=np.float32)
|
380 |
silent_path = os.path.join(TEMP_FOLDER, f"silent_{int(time.time())}.wav")
|
|
|
382 |
return silent_path
|
383 |
|
384 |
def generate_tts(text, voice):
|
385 |
+
"""Generate TTS audio using Kokoro with fallback to gTTS."""
|
386 |
safe_text = re.sub(r'[^\w\s-]', '', text[:10]).strip().replace(' ', '_')
|
387 |
file_path = os.path.join(TEMP_FOLDER, f"tts_{safe_text}.wav")
|
388 |
if os.path.exists(file_path):
|
|
|
409 |
return generate_silent_audio(duration=max(3, len(text.split()) * 0.5))
|
410 |
|
411 |
def apply_kenburns_effect(clip, target_resolution, effect_type=None):
|
412 |
+
"""Apply a Ken Burns effect to the clip (used for images)."""
|
413 |
target_w, target_h = target_resolution
|
414 |
clip_aspect = clip.w / clip.h
|
415 |
target_aspect = target_w / target_h
|
|
|
430 |
if effect_type is None or effect_type == "random":
|
431 |
effect_type = random.choice(available_effects)
|
432 |
if effect_type == "zoom-in":
|
433 |
+
start_zoom = 0.9; end_zoom = 1.1; start_center = (new_width/2, new_height/2); end_center = start_center
|
|
|
|
|
|
|
434 |
elif effect_type == "zoom-out":
|
435 |
+
start_zoom = 1.1; end_zoom = 0.9; start_center = (new_width/2, new_height/2); end_center = start_center
|
|
|
|
|
|
|
436 |
elif effect_type == "pan-left":
|
437 |
+
start_zoom = 1.0; end_zoom = 1.0; start_center = (max_offset_x+target_w/2, (max_offset_y//2)+target_h/2); end_center = (target_w/2, (max_offset_y//2)+target_h/2)
|
|
|
|
|
|
|
438 |
elif effect_type == "pan-right":
|
439 |
+
start_zoom = 1.0; end_zoom = 1.0; start_center = (target_w/2, (max_offset_y//2)+target_h/2); end_center = (max_offset_x+target_w/2, (max_offset_y//2)+target_h/2)
|
|
|
|
|
|
|
440 |
elif effect_type == "up-left":
|
441 |
+
start_zoom = 1.0; end_zoom = 1.0; start_center = (max_offset_x+target_w/2, max_offset_y+target_h/2); end_center = (target_w/2, target_h/2)
|
|
|
|
|
|
|
442 |
else:
|
443 |
+
raise ValueError(f"Unsupported effect: {effect_type}")
|
444 |
def transform_frame(get_frame, t):
|
445 |
frame = get_frame(t)
|
446 |
ratio = t / clip.duration if clip.duration > 0 else 0
|
|
|
450 |
crop_h = int(target_h / current_zoom)
|
451 |
current_center_x = start_center[0] + (end_center[0] - start_center[0]) * ratio
|
452 |
current_center_y = start_center[1] + (end_center[1] - start_center[1]) * ratio
|
453 |
+
min_center_x = crop_w / 2; max_center_x = new_width - crop_w / 2
|
454 |
+
min_center_y = crop_h / 2; max_center_y = new_height - crop_h / 2
|
|
|
|
|
455 |
current_center_x = max(min_center_x, min(current_center_x, max_center_x))
|
456 |
current_center_y = max(min_center_y, min(current_center_y, max_center_y))
|
457 |
cropped_frame = cv2.getRectSubPix(frame, (crop_w, crop_h), (current_center_x, current_center_y))
|
|
|
475 |
return clip
|
476 |
|
477 |
def find_mp3_files():
|
478 |
+
"""Search for an MP3 file in the current directory (for background music)."""
|
479 |
mp3_files = []
|
480 |
for root, dirs, files in os.walk('.'):
|
481 |
for file in files:
|
482 |
if file.endswith('.mp3'):
|
483 |
+
mp3_files.append(os.path.join(root, file))
|
|
|
484 |
return mp3_files[0] if mp3_files else None
|
485 |
|
486 |
def add_background_music(final_video, bg_music_volume=0.08):
|
487 |
+
"""Add background music to the final video if an MP3 file is found."""
|
488 |
try:
|
489 |
bg_music_path = find_mp3_files()
|
490 |
if bg_music_path and os.path.exists(bg_music_path):
|
|
|
504 |
return final_video
|
505 |
|
506 |
def create_clip(media_path, asset_type, tts_path, duration=None, effects=None, narration_text=None, segment_index=0):
|
507 |
+
"""Create a video clip from a media asset and TTS audio, and add subtitles if enabled."""
|
508 |
try:
|
509 |
if not os.path.exists(media_path) or not os.path.exists(tts_path):
|
510 |
return None
|
|
|
579 |
return None
|
580 |
|
581 |
def fix_imagemagick_policy():
|
582 |
+
"""Attempt to modify ImageMagick security policies if needed."""
|
583 |
try:
|
584 |
policy_paths = [
|
585 |
"/etc/ImageMagick-6/policy.xml",
|
|
|
601 |
def generate_video(user_input, resolution, caption_option):
|
602 |
"""
|
603 |
Original video generation function.
|
604 |
+
This version uses the generated script to create video clips.
|
605 |
+
(Integration with custom clip edits could be extended further.)
|
606 |
"""
|
607 |
global TARGET_RESOLUTION, CAPTION_COLOR, TEMP_FOLDER
|
608 |
if resolution == "Full HD (1920x1080)":
|
|
|
613 |
TARGET_RESOLUTION = (1920, 1080)
|
614 |
CAPTION_COLOR = "white" if caption_option == "Yes" else "transparent"
|
615 |
TEMP_FOLDER = tempfile.mkdtemp()
|
616 |
+
fix_imagemagick_policy()
|
|
|
|
|
617 |
print("Generating script from API...")
|
618 |
script = generate_script(user_input)
|
619 |
if not script:
|
|
|
660 |
shutil.rmtree(TEMP_FOLDER)
|
661 |
return OUTPUT_VIDEO_FILENAME
|
662 |
|
663 |
+
# ------------------- NEW CALLBACKS FOR THE ADVANCED UI -------------------
|
664 |
+
|
665 |
+
# Global variable to store clip data from the script generation.
|
666 |
generated_clip_data = []
|
667 |
|
668 |
def generate_script_clips(topic_input, full_script):
|
669 |
"""
|
670 |
+
Callback when "π Generate Script & Load Clips" is clicked.
|
671 |
+
Uses the full_script if provided; otherwise, generates based on the topic.
|
672 |
+
Returns the raw generated script and a JSON string containing the clip data.
|
673 |
"""
|
674 |
input_text = full_script if full_script.strip() != "" else topic_input
|
675 |
script = generate_script(input_text)
|
|
|
684 |
clip_info = {
|
685 |
"prompt": media_elem.get("prompt", ""),
|
686 |
"narration": tts_elem.get("text", ""),
|
687 |
+
"custom_media": "" # initially empty; user may upload a file later
|
688 |
}
|
689 |
clip_list.append(clip_info)
|
690 |
global generated_clip_data
|
|
|
693 |
|
694 |
def update_clip_editor(clip_json):
|
695 |
"""
|
696 |
+
Dynamically build a UI editor for each clip.
|
697 |
+
Each editor contains:
|
698 |
+
- A textbox to edit the visual prompt.
|
699 |
+
- A textbox to edit the TTS text.
|
700 |
+
- A File uploader to allow custom media upload.
|
701 |
+
Returns a list of dynamically generated Accordions.
|
702 |
"""
|
703 |
clip_list = json.loads(clip_json)
|
704 |
editors = []
|
705 |
+
# Create a container for all clip editors.
|
706 |
for idx, clip in enumerate(clip_list, start=1):
|
707 |
+
with gr.Accordion(label=f"Clip {idx}: {clip['prompt']}", open=(idx<=2)) as acc:
|
|
|
|
|
708 |
prompt_box = gr.Textbox(label="Visual Prompt", value=clip["prompt"])
|
709 |
+
tts_box = gr.Textbox(label="TTS Text", value=clip["narration"], lines=3)
|
710 |
+
custom_media_box = gr.File(label="Upload custom image/video", file_types=[".jpg", ".png", ".mp4"])
|
711 |
+
# Pack values in a dictionary and return them later (here we simply display the components).
|
712 |
+
# In a full integration you could use additional state to track these values.
|
713 |
+
editors.append(acc)
|
|
|
|
|
|
|
|
|
714 |
return editors
|
715 |
|
716 |
def generate_final_video(topic_input, full_script, clip_data_json, resolution, render_speed,
|
|
|
718 |
subtitle_enabled, font_dropdown, font_size, outline_width,
|
719 |
font_color, outline_color, subtitle_position):
|
720 |
"""
|
721 |
+
Callback when "π¬ Generate Video" is clicked.
|
722 |
+
In a full implementation you would update each clip from the dynamic editors.
|
723 |
+
Here, for demonstration purposes, we simply call the original generate_video function.
|
724 |
+
The final video file is returned and used both for preview and download.
|
725 |
"""
|
|
|
726 |
print("Final settings:")
|
727 |
print(f"Resolution: {resolution}, Render Speed: {render_speed}, Video Clip %: {video_clip_percent}, Zoom/Pan: {zoom_pan}")
|
728 |
if bg_music_file is not None:
|
729 |
print("Custom background music provided.")
|
|
|
730 |
video_file = generate_video(topic_input, resolution, "Yes")
|
731 |
+
# Return the file path for both the video preview and the download component.
|
732 |
+
return video_file, video_file
|
733 |
|
734 |
+
# ------------------- GRADIO BLOCKS UI -------------------
|
735 |
with gr.Blocks(title="π Orbit Video Engine") as demo:
|
736 |
with gr.Row():
|
737 |
# Column 1: Content Input & Script Generation
|
738 |
with gr.Column(scale=1):
|
739 |
gr.Markdown("## 1. Content Input")
|
740 |
topic_input = gr.Textbox(label="Topic Input", placeholder="Enter your video topic here...")
|
741 |
+
full_script = gr.Textbox(label="Or Paste Full Script", placeholder="Paste full script (using [Title] etc.)", lines=5)
|
742 |
generate_script_btn = gr.Button("π Generate Script & Load Clips")
|
743 |
generated_script_disp = gr.Textbox(label="Generated Script", interactive=False, visible=False)
|
744 |
+
# Hidden storage for clip JSON data
|
745 |
clip_data_storage = gr.Textbox(visible=False)
|
746 |
+
# Column 2: Dynamic Clip Editor
|
747 |
with gr.Column(scale=1):
|
748 |
gr.Markdown("## 2. Edit Clips")
|
749 |
+
gr.Markdown("Modify each clip's Visual Prompt, TTS Text or upload your own media.")
|
750 |
clip_editor_container = gr.Column(visible=False)
|
751 |
+
# Column 3: Video Settings & Output
|
752 |
with gr.Column(scale=1):
|
753 |
gr.Markdown("## 3. Video Settings")
|
754 |
resolution = gr.Radio(choices=["Short (1080x1920)", "Full HD (1920x1080)"],
|
|
|
771 |
subtitle_position = gr.Radio(choices=["center", "bottom", "top"], label="Subtitle Position", value="center")
|
772 |
gr.Markdown("## 4. Output")
|
773 |
generate_video_btn = gr.Button("π¬ Generate Video")
|
774 |
+
# Two outputs: one for video preview, one for file download
|
775 |
video_preview = gr.Video(label="Generated Video")
|
776 |
download_video_file = gr.File(label="Download Video", interactive=False)
|
777 |
+
|
778 |
+
# ------------------- INTERACTIONS -------------------
|
779 |
+
# When "Generate Script & Load Clips" is clicked:
|
780 |
generate_script_btn.click(
|
781 |
fn=generate_script_clips,
|
782 |
inputs=[topic_input, full_script],
|
|
|
794 |
inputs=[clip_editor_container],
|
795 |
outputs=[clip_editor_container]
|
796 |
)
|
797 |
+
|
798 |
+
# When "Generate Video" is clicked, call the video generation callback.
|
799 |
generate_video_btn.click(
|
800 |
fn=generate_final_video,
|
801 |
inputs=[topic_input, full_script, clip_data_storage, resolution, render_speed,
|
802 |
video_clip_percent, zoom_pan, bg_music_file, bg_music_volume,
|
803 |
subtitle_enabled, font_dropdown, font_size, outline_width, font_color, outline_color, subtitle_position],
|
804 |
+
outputs=[video_preview, download_video_file]
|
805 |
)
|
806 |
+
|
807 |
demo.launch(share=True)
|