| from flask import Flask, request, jsonify, send_file |
| from moviepy.editor import VideoFileClip, concatenate_videoclips |
| 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() |
| prompt = data.get("duration", '').strip() |
| prompts = prompt.replace("**", "") |
| print(prompts) |
| if prompts == '': |
| return jsonify({"error": "prompts must not be empty"}), 400 |
|
|
| |
| for d in ["/tmp/images", "/tmp/sound", "/tmp/video"]: |
| os.makedirs(d, exist_ok=True) |
|
|
| raw_lines = prompts.splitlines() |
| lines = [] |
|
|
| i = 0 |
| while i < len(raw_lines): |
| line = raw_lines[i].strip() |
| if line.startswith("#") and (line.endswith('?') or line.endswith(':')): |
| block = line |
| i += 1 |
| paragraph_lines = [] |
| while i < len(raw_lines): |
| next_line = raw_lines[i].strip() |
| if next_line.startswith("#") and (next_line.endswith('?') or next_line.endswith(':')): |
| break |
| paragraph_lines.append(next_line) |
| i += 1 |
| if len(paragraph_lines) >= 5: |
| break |
| if paragraph_lines: |
| block += '\n' + '\n'.join(paragraph_lines) |
| lines.append(block) |
| else: |
| block_lines = [] |
| count = 0 |
| while i < len(raw_lines) and count < 5: |
| next_line = raw_lines[i].strip() |
| if next_line.startswith("#") and (next_line.endswith('?') or next_line.endswith(':')): |
| break |
| block_lines.append(next_line) |
| i += 1 |
| count += 1 |
| if block_lines: |
| lines.append('\n'.join(block_lines)) |
|
|
| |
| image_olst = [] |
| for id in range(len(lines)): |
| create_text_image(lines[id], id, image_olst) |
|
|
| image_files = sorted(glob.glob("/tmp/images/*.png")) |
| if not image_files: |
| raise ValueError("No images found in folder!") |
|
|
| |
| for id in range(len(lines)): |
| video_func(id, lines) |
|
|
| clips = [] |
| for id in range(len(lines)): |
| clip_path = f"/tmp/video/clip{id}.mp4" |
| clip = VideoFileClip(clip_path) |
| clips.append(clip) |
|
|
| final_video = concatenate_videoclips(clips) |
| output_path = "/tmp/final_output.mp4" |
| final_video.write_videofile(output_path, fps=24) |
|
|
| |
| for f in image_files: |
| os.remove(f) |
|
|
| return send_file(output_path, mimetype='video/mp4') |
|
|
| 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) |