Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify, send_file | |
| from moviepy.editor import ColorClip, ImageClip, concatenate_videoclips, VideoFileClip | |
| import traceback | |
| import uuid | |
| import glob | |
| import os | |
| from image_fetcher import main | |
| from video import create_text_image | |
| from video2 import video_func | |
| app = Flask(__name__) | |
| def home(): | |
| return "Flask Video Generator is Running" | |
| def generate_video(): | |
| try: | |
| data = request.get_json() | |
| id = data["id"] | |
| lines = data["lines"] | |
| image_folder = "/tmp/images" | |
| image_olst = [] | |
| # 1. Create Image | |
| create_text_image(lines[id], id, image_olst) | |
| # 2. Generate Video (Only once!) | |
| video_path = video_func(id, lines) | |
| # 3. Check video exists | |
| if not os.path.exists(video_path): | |
| raise Exception("Video not found: " + video_path) | |
| print("Generated video at:", video_path) | |
| # 4. Cleanup images | |
| image_files = sorted(glob.glob(os.path.join(image_folder, "*.png"))) | |
| for img in image_files: | |
| os.remove(img) | |
| # 5. Return video | |
| return send_file(video_path, mimetype='video/mp4', as_attachment=True) | |
| except Exception as e: | |
| traceback.print_exc() | |
| return jsonify({"error": str(e)}), 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |