Update app.py
Browse files
app.py
CHANGED
|
@@ -231,14 +231,128 @@ def generate_video():
|
|
| 231 |
raw_body=raw_data.get("jsondata" , '')
|
| 232 |
#print(f"Raw body length: {len(raw_body)}")
|
| 233 |
#print(f"First 200 chars: {raw_body[:200]}")
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
# Return video file as blob
|
| 240 |
output_path="https://videos.pexels.com/video-files/3173312/3173312-uhd_2560_1440_30fps.mp4"
|
| 241 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
except subprocess.TimeoutExpired:
|
| 244 |
print("Video rendering timeout")
|
|
|
|
| 231 |
raw_body=raw_data.get("jsondata" , '')
|
| 232 |
#print(f"Raw body length: {len(raw_body)}")
|
| 233 |
#print(f"First 200 chars: {raw_body[:200]}")
|
| 234 |
+
lst = raw_body.split("&&&&")
|
| 235 |
+
cleaned = re.sub(r'(\d)\s*\.\s*(\d)', r'\1.\2', lst[0])
|
| 236 |
+
nlist = ast.literal_eval(cleaned)
|
| 237 |
+
datalst=[]
|
| 238 |
+
for line in range(len(nlist)):
|
| 239 |
+
datalst.append({
|
| 240 |
+
"type": nlist[line][0].strip(),
|
| 241 |
+
"content": nlist[line][1].strip(),
|
| 242 |
+
"animation": nlist[line][2].strip().replace(" ",""),
|
| 243 |
+
"duration": nlist[line][3]
|
| 244 |
+
})
|
| 245 |
+
|
| 246 |
+
data={
|
| 247 |
+
"video_settings": {
|
| 248 |
+
"background_color": "#0f0f23",
|
| 249 |
+
"text_color": "WHITE",
|
| 250 |
+
"highlight_color": "YELLOW",
|
| 251 |
+
"font": "CMU Serif",
|
| 252 |
+
"text_size": 36,
|
| 253 |
+
"equation_size": 42,
|
| 254 |
+
"title_size": 48
|
| 255 |
+
},
|
| 256 |
+
"slides":datalst}'''
|
| 257 |
+
'''# Now proceed with video generation using 'data'
|
| 258 |
+
print(json.dumps(data, indent=2)) # For debugging
|
| 259 |
+
# ✅ Final validation
|
| 260 |
+
if "slides" not in data or not data["slides"]:
|
| 261 |
+
return jsonify({"error": "No slides provided in request"}), 400
|
| 262 |
+
|
| 263 |
+
print(f"✅ Parsed {len(data['slides'])} slides successfully.")
|
| 264 |
+
|
| 265 |
+
# Validate input
|
| 266 |
+
if "slides" not in data or not data["slides"]:
|
| 267 |
+
return jsonify({"error": "No slides provided in request"}), 400
|
| 268 |
+
|
| 269 |
+
print(f"Received request with {len(data['slides'])} slides")
|
| 270 |
+
|
| 271 |
+
# Create unique temporary directory
|
| 272 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 273 |
+
temp_work_dir = os.path.join(TEMP_DIR, f"manim_{timestamp}")
|
| 274 |
+
os.makedirs(temp_work_dir, exist_ok=True)
|
| 275 |
+
|
| 276 |
+
# Generate Manim script
|
| 277 |
+
script_path = os.path.join(temp_work_dir, "scene.py")
|
| 278 |
+
create_manim_script(data, script_path)
|
| 279 |
+
print(f"Created Manim script at {script_path}")
|
| 280 |
+
|
| 281 |
+
# Render video using subprocess
|
| 282 |
+
quality = 'l' # l=low, m=medium, h=high
|
| 283 |
+
render_command = [
|
| 284 |
+
"manim",
|
| 285 |
+
f"-q{quality}",
|
| 286 |
+
"--disable_caching",
|
| 287 |
+
"--media_dir", temp_work_dir,
|
| 288 |
+
script_path,
|
| 289 |
+
"GeneratedMathScene"
|
| 290 |
+
]
|
| 291 |
+
|
| 292 |
+
print(f"Running command: {' '.join(render_command)}")
|
| 293 |
+
|
| 294 |
+
result = subprocess.run(
|
| 295 |
+
render_command,
|
| 296 |
+
capture_output=True,
|
| 297 |
+
text=True,
|
| 298 |
+
cwd=temp_work_dir,
|
| 299 |
+
timeout=120
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
if result.returncode != 0:
|
| 303 |
+
error_msg = result.stderr or result.stdout
|
| 304 |
+
print(f"Manim rendering failed: {error_msg}")
|
| 305 |
+
return jsonify({
|
| 306 |
+
"error": "Manim rendering failed",
|
| 307 |
+
"details": error_msg
|
| 308 |
+
}), 500
|
| 309 |
+
|
| 310 |
+
print("Manim rendering completed successfully")
|
| 311 |
+
|
| 312 |
+
# Find generated video
|
| 313 |
+
quality_map = {'l': '480p15', 'm': '720p30', 'h': '1080p60'}
|
| 314 |
+
video_quality = quality_map.get(quality, '480p15')
|
| 315 |
+
|
| 316 |
+
video_path = os.path.join(
|
| 317 |
+
temp_work_dir,
|
| 318 |
+
"videos",
|
| 319 |
+
"scene",
|
| 320 |
+
video_quality,
|
| 321 |
+
"GeneratedMathScene.mp4"
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
if not os.path.exists(video_path):
|
| 325 |
+
print(f"Video not found at expected path: {video_path}")
|
| 326 |
+
return jsonify({
|
| 327 |
+
"error": "Video file not found after rendering",
|
| 328 |
+
"expected_path": video_path
|
| 329 |
+
}), 500
|
| 330 |
+
|
| 331 |
+
print(f"Video found at: {video_path}")
|
| 332 |
+
|
| 333 |
+
# Copy to media directory
|
| 334 |
+
output_filename = f"math_video_{timestamp}.mp4"
|
| 335 |
+
output_path = os.path.join(MEDIA_DIR, output_filename)
|
| 336 |
+
shutil.copy(video_path, output_path)
|
| 337 |
+
print(f"Video copied to: {output_path}")
|
| 338 |
+
|
| 339 |
+
# Clean up temp directory
|
| 340 |
+
try:
|
| 341 |
+
shutil.rmtree(temp_work_dir)
|
| 342 |
+
print("Cleaned up temp directory")
|
| 343 |
+
except Exception as e:
|
| 344 |
+
print(f"Failed to clean temp dir: {e}")
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
# Return video file as blob
|
| 349 |
output_path="https://videos.pexels.com/video-files/3173312/3173312-uhd_2560_1440_30fps.mp4"
|
| 350 |
+
return send_file(
|
| 351 |
+
output_path,
|
| 352 |
+
mimetype='video/mp4',
|
| 353 |
+
as_attachment=False,
|
| 354 |
+
download_name="math.mp4"
|
| 355 |
+
)
|
| 356 |
|
| 357 |
except subprocess.TimeoutExpired:
|
| 358 |
print("Video rendering timeout")
|