Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,13 @@ import cv2
|
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
import os
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
# Function to split the script into smaller chunks
|
| 12 |
-
def split_script(script: str, max_words: int =
|
| 13 |
words = script.split()
|
| 14 |
chunks = [" ".join(words[i:i + max_words]) for i in range(0, len(words), max_words)]
|
| 15 |
return chunks
|
|
@@ -19,7 +21,7 @@ def create_video_segment(script_chunk: str, background_color: str, text_color: s
|
|
| 19 |
try:
|
| 20 |
# Step 1: Convert script chunk to audio using gTTS
|
| 21 |
tts = gTTS(script_chunk)
|
| 22 |
-
audio_file = "
|
| 23 |
tts.save(audio_file)
|
| 24 |
|
| 25 |
# Step 2: Create a blank image with text
|
|
@@ -48,7 +50,7 @@ def create_video_segment(script_chunk: str, background_color: str, text_color: s
|
|
| 48 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 49 |
|
| 50 |
# Step 3: Create a video segment with the image and audio
|
| 51 |
-
video_segment_file = f"video_segment_{
|
| 52 |
fps = 24
|
| 53 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 54 |
video_writer = cv2.VideoWriter(video_segment_file, fourcc, fps, (width, height))
|
|
@@ -105,11 +107,12 @@ async def generate_video(script: str, background_color: str = "#000000", text_co
|
|
| 105 |
# Step 1: Split the script into smaller chunks
|
| 106 |
script_chunks = split_script(script)
|
| 107 |
|
| 108 |
-
# Step 2: Generate video segments
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
# Step 3: Combine video segments into a single video
|
| 115 |
final_video_file = combine_video_segments(video_segment_files)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
import os
|
| 8 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
+
import asyncio
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
# Function to split the script into smaller chunks
|
| 14 |
+
def split_script(script: str, max_words: int = 30):
|
| 15 |
words = script.split()
|
| 16 |
chunks = [" ".join(words[i:i + max_words]) for i in range(0, len(words), max_words)]
|
| 17 |
return chunks
|
|
|
|
| 21 |
try:
|
| 22 |
# Step 1: Convert script chunk to audio using gTTS
|
| 23 |
tts = gTTS(script_chunk)
|
| 24 |
+
audio_file = f"output_audio_{os.urandom(4).hex()}.mp3"
|
| 25 |
tts.save(audio_file)
|
| 26 |
|
| 27 |
# Step 2: Create a blank image with text
|
|
|
|
| 50 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 51 |
|
| 52 |
# Step 3: Create a video segment with the image and audio
|
| 53 |
+
video_segment_file = f"video_segment_{os.urandom(4).hex()}.mp4"
|
| 54 |
fps = 24
|
| 55 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 56 |
video_writer = cv2.VideoWriter(video_segment_file, fourcc, fps, (width, height))
|
|
|
|
| 107 |
# Step 1: Split the script into smaller chunks
|
| 108 |
script_chunks = split_script(script)
|
| 109 |
|
| 110 |
+
# Step 2: Generate video segments in parallel
|
| 111 |
+
with ThreadPoolExecutor() as executor:
|
| 112 |
+
video_segment_files = list(executor.map(
|
| 113 |
+
lambda chunk: create_video_segment(chunk, background_color, text_color, font_size),
|
| 114 |
+
script_chunks
|
| 115 |
+
))
|
| 116 |
|
| 117 |
# Step 3: Combine video segments into a single video
|
| 118 |
final_video_file = combine_video_segments(video_segment_files)
|