-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_video_from_existing_audio.py
More file actions
176 lines (148 loc) · 5.69 KB
/
Copy pathgenerate_video_from_existing_audio.py
File metadata and controls
176 lines (148 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
Generate video using pre-generated audio files
FAST - just combine images + audio
"""
import subprocess
from pathlib import Path
from PIL import Image
# --- Configuration ---
BASE_DIR = Path("/Users/xiangzheng/Desktop/nova/paper_presenter")
STAGE1_DIR = BASE_DIR / "outputs/stage1_v2"
TTS_OUTPUT_DIR = BASE_DIR / "tts_output"
OUTPUT_DIR = BASE_DIR / "outputs"
TEMP_DIR = OUTPUT_DIR / "tmp"
TEMP_DIR.mkdir(parents=True, exist_ok=True)
TARGET_SIZE = (1920, 1080)
def prepare_image_with_padding(image_path, output_path):
"""Prepare image with white padding - preserve aspect ratio"""
try:
img = Image.open(image_path).convert('RGB')
img_ratio = img.width / img.height
target_ratio = TARGET_SIZE[0] / TARGET_SIZE[1]
if img_ratio > target_ratio:
new_width = TARGET_SIZE[0]
new_height = int(TARGET_SIZE[0] / img_ratio)
else:
new_height = TARGET_SIZE[1]
new_width = int(TARGET_SIZE[1] * img_ratio)
img = img.resize((new_width, new_height), Image.LANCZOS)
background = Image.new('RGB', TARGET_SIZE, (255, 255, 255))
x = (TARGET_SIZE[0] - new_width) // 2
y = (TARGET_SIZE[1] - new_height) // 2
background.paste(img, (x, y))
background.save(output_path, 'JPEG', quality=95)
print(f" ✓ Image: {new_width}x{new_height} centered")
return output_path
except Exception as e:
print(f" ✗ Error: {e}")
return None
def create_video_segment(image_path, audio_path, output_path):
"""Create video using ffmpeg"""
try:
print(f" 📹 Creating video segment...")
cmd = [
'ffmpeg', '-y', '-loop', '1', '-hide_banner', '-loglevel', 'error',
'-i', str(image_path),
'-i', str(audio_path),
'-c:v', 'libx264', '-tune', 'stillimage',
'-c:a', 'aac', '-b:a', '192k',
'-pix_fmt', 'yuv420p', '-shortest',
'-preset', 'ultrafast',
str(output_path)
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
if result.returncode == 0:
print(f" ✓ Video created")
return True
else:
print(f" ✗ ffmpeg error: {result.stderr}")
return False
except Exception as e:
print(f" ✗ Error: {e}")
return False
def concatenate_videos(video_files, output_path):
"""Concatenate videos using ffmpeg"""
try:
print(f"\n{'='*80}")
print(f"CONCATENATING {len(video_files)} VIDEO SEGMENTS")
print(f"{'='*80}\n")
concat_file = TEMP_DIR / "concat_list.txt"
with open(concat_file, 'w') as f:
for video_file in video_files:
f.write(f"file '{video_file.absolute()}'\n")
cmd = [
'ffmpeg', '-y', '-f', 'concat', '-safe', '0',
'-hide_banner', '-loglevel', 'error',
'-i', str(concat_file),
'-c', 'copy',
str(output_path)
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
if result.returncode == 0:
size_mb = output_path.stat().st_size / (1024*1024)
print(f"✓ Final video created!")
print(f" Location: {output_path}")
print(f" Size: {size_mb:.1f} MB")
return True
else:
print(f"✗ Concatenation failed: {result.stderr}")
return False
except Exception as e:
print(f"✗ Error: {e}")
return False
def main():
print("="*80)
print("GENERATE VIDEO FROM EXISTING AUDIO FILES")
print("="*80)
print(f"\nUsing pre-generated audio from: tts_output/\n")
# Mapping of slides
slides = [
('01', 'background'),
('02', 'conclusion'),
('03', 'methodology'),
('04', 'methodology'),
('05', 'methodology'),
('06', 'experiments'),
('07', 'experiments'),
('08', 'experiments'),
('09', 'experiments'),
('10', 'summary'),
]
video_segments = []
for idx_str, name in slides:
print(f"{'='*80}")
print(f"SLIDE {idx_str} - {name.upper()}")
print(f"{'='*80}")
# Paths
image_path = STAGE1_DIR / f"{idx_str}_{name}.jpg"
audio_path = TTS_OUTPUT_DIR / f"{idx_str}_{name}.wav"
# Check if audio exists
if not audio_path.exists():
print(f" ⚠ Audio not found: {audio_path}")
continue
# Prepare image with white padding
prepared_image_path = TEMP_DIR / f"image_{idx_str}.jpg"
if not prepare_image_with_padding(image_path, prepared_image_path):
print(f" ⚠ Skipping slide {idx_str}")
continue
# Create video segment
video_path = TEMP_DIR / f"segment_{idx_str}.mp4"
if create_video_segment(prepared_image_path, audio_path, video_path):
video_segments.append(video_path)
print(f"✓ Slide {idx_str} COMPLETE\n")
else:
print(f"⚠ Slide {idx_str} FAILED\n")
if not video_segments:
print("\n✗ No video segments created")
return
print(f"\n✓ Created {len(video_segments)}/10 video segments\n")
# Concatenate all segments
final_output = OUTPUT_DIR / "final_presentation_video_cloned.mp4"
if concatenate_videos(video_segments, final_output):
print(f"\n{'='*80}")
print(f"✓✓✓ SUCCESS! VIDEO WITH CLONED VOICE ✓✓✓")
print(f"{'='*80}\n")
else:
print(f"\n✗ Failed to create final video")
if __name__ == "__main__":
main()