|
|
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__) |
|
|
|
|
|
@app.route("/") |
|
|
def home(): |
|
|
return "Flask Video Generator is Running" |
|
|
|
|
|
@app.route("/generate", methods=["POST"]) |
|
|
def generate_video(): |
|
|
try: |
|
|
data = request.get_json() |
|
|
id = data["id"] |
|
|
lines = data["lines"] |
|
|
|
|
|
image_folder = "/tmp/images" |
|
|
image_olst = [] |
|
|
|
|
|
|
|
|
create_text_image(lines[id], id, image_olst) |
|
|
|
|
|
|
|
|
video_path = video_func(id, lines) |
|
|
|
|
|
|
|
|
if not os.path.exists(video_path): |
|
|
raise Exception("Video not found: " + video_path) |
|
|
|
|
|
print("Generated video at:", video_path) |
|
|
|
|
|
|
|
|
image_files = sorted(glob.glob(os.path.join(image_folder, "*.png"))) |
|
|
for img in image_files: |
|
|
os.remove(img) |
|
|
|
|
|
|
|
|
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) |