Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1321,7 +1321,14 @@ class VideoGeneratorUI:
|
|
| 1321 |
|
| 1322 |
class VideoGenerator:
|
| 1323 |
def __init__(self):
|
| 1324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1325 |
self.setup_resources()
|
| 1326 |
|
| 1327 |
def setup_resources(self):
|
|
@@ -1424,10 +1431,18 @@ class VideoGenerator:
|
|
| 1424 |
)
|
| 1425 |
|
| 1426 |
def generate_video(self, script: str, images: List[str], duration: int, output_path: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1427 |
try:
|
| 1428 |
-
# Create temporary directory for processing
|
| 1429 |
-
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 1430 |
-
|
| 1431 |
# Process images
|
| 1432 |
processed_images = []
|
| 1433 |
for img_url in images:
|
|
@@ -1454,14 +1469,12 @@ class VideoGenerator:
|
|
| 1454 |
frames = []
|
| 1455 |
frame_count = 0
|
| 1456 |
|
| 1457 |
-
# Generate
|
| 1458 |
for idx, img in enumerate(processed_images):
|
| 1459 |
-
# Get text section for this image
|
| 1460 |
start_idx = idx * words_per_image
|
| 1461 |
end_idx = start_idx + words_per_image if idx < len(processed_images) - 1 else len(words)
|
| 1462 |
section_text = ' '.join(words[start_idx:end_idx])
|
| 1463 |
|
| 1464 |
-
# Generate frames for this section
|
| 1465 |
for frame in range(frames_per_image):
|
| 1466 |
if frame_count < total_frames:
|
| 1467 |
frame_img = self.create_video_frame(
|
|
@@ -1473,10 +1486,10 @@ class VideoGenerator:
|
|
| 1473 |
frames.append(frame_img)
|
| 1474 |
frame_count += 1
|
| 1475 |
|
| 1476 |
-
# Add
|
| 1477 |
if idx < len(processed_images) - 1:
|
| 1478 |
next_img = processed_images[idx + 1]
|
| 1479 |
-
for t in range(15):
|
| 1480 |
if frame_count < total_frames:
|
| 1481 |
alpha = t / 15
|
| 1482 |
transition_frame = Image.blend(
|
|
@@ -1494,15 +1507,15 @@ class VideoGenerator:
|
|
| 1494 |
frame_count += 1
|
| 1495 |
|
| 1496 |
# Generate audio
|
| 1497 |
-
audio_path = self.temp_dir
|
| 1498 |
tts = gTTS(text=script, lang='en')
|
| 1499 |
-
tts.save(
|
| 1500 |
|
| 1501 |
# Create video
|
| 1502 |
clip = ImageSequenceClip(frames, fps=fps)
|
| 1503 |
-
audio_clip = AudioFileClip(
|
| 1504 |
|
| 1505 |
-
# Adjust
|
| 1506 |
if audio_clip.duration < clip.duration:
|
| 1507 |
clip = clip.subclip(0, audio_clip.duration)
|
| 1508 |
else:
|
|
|
|
| 1321 |
|
| 1322 |
class VideoGenerator:
|
| 1323 |
def __init__(self):
|
| 1324 |
+
# Create base directories
|
| 1325 |
+
self.base_dir = os.path.abspath("generated_videos")
|
| 1326 |
+
self.temp_dir = os.path.abspath(tempfile.mkdtemp())
|
| 1327 |
+
|
| 1328 |
+
# Ensure directories exist
|
| 1329 |
+
os.makedirs(self.base_dir, exist_ok=True)
|
| 1330 |
+
os.makedirs(self.temp_dir, exist_ok=True)
|
| 1331 |
+
|
| 1332 |
self.setup_resources()
|
| 1333 |
|
| 1334 |
def setup_resources(self):
|
|
|
|
| 1431 |
)
|
| 1432 |
|
| 1433 |
def generate_video(self, script: str, images: List[str], duration: int, output_path: str) -> str:
|
| 1434 |
+
if not script or not images:
|
| 1435 |
+
raise ValueError("Script and images are required")
|
| 1436 |
+
|
| 1437 |
+
if not output_path:
|
| 1438 |
+
# Generate default output path if none provided
|
| 1439 |
+
timestamp = int(time.time())
|
| 1440 |
+
output_path = os.path.join(self.base_dir, f"video_{timestamp}.mp4")
|
| 1441 |
+
|
| 1442 |
+
# Ensure output directory exists
|
| 1443 |
+
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
|
| 1444 |
+
|
| 1445 |
try:
|
|
|
|
|
|
|
|
|
|
| 1446 |
# Process images
|
| 1447 |
processed_images = []
|
| 1448 |
for img_url in images:
|
|
|
|
| 1469 |
frames = []
|
| 1470 |
frame_count = 0
|
| 1471 |
|
| 1472 |
+
# Generate frames
|
| 1473 |
for idx, img in enumerate(processed_images):
|
|
|
|
| 1474 |
start_idx = idx * words_per_image
|
| 1475 |
end_idx = start_idx + words_per_image if idx < len(processed_images) - 1 else len(words)
|
| 1476 |
section_text = ' '.join(words[start_idx:end_idx])
|
| 1477 |
|
|
|
|
| 1478 |
for frame in range(frames_per_image):
|
| 1479 |
if frame_count < total_frames:
|
| 1480 |
frame_img = self.create_video_frame(
|
|
|
|
| 1486 |
frames.append(frame_img)
|
| 1487 |
frame_count += 1
|
| 1488 |
|
| 1489 |
+
# Add transitions
|
| 1490 |
if idx < len(processed_images) - 1:
|
| 1491 |
next_img = processed_images[idx + 1]
|
| 1492 |
+
for t in range(15):
|
| 1493 |
if frame_count < total_frames:
|
| 1494 |
alpha = t / 15
|
| 1495 |
transition_frame = Image.blend(
|
|
|
|
| 1507 |
frame_count += 1
|
| 1508 |
|
| 1509 |
# Generate audio
|
| 1510 |
+
audio_path = os.path.join(self.temp_dir, "audio.mp3")
|
| 1511 |
tts = gTTS(text=script, lang='en')
|
| 1512 |
+
tts.save(audio_path)
|
| 1513 |
|
| 1514 |
# Create video
|
| 1515 |
clip = ImageSequenceClip(frames, fps=fps)
|
| 1516 |
+
audio_clip = AudioFileClip(audio_path)
|
| 1517 |
|
| 1518 |
+
# Adjust durations
|
| 1519 |
if audio_clip.duration < clip.duration:
|
| 1520 |
clip = clip.subclip(0, audio_clip.duration)
|
| 1521 |
else:
|