|
from flask import Flask, request, jsonify, send_file |
|
from moviepy.editor import ColorClip,ImageClip, concatenate_videoclips |
|
import traceback |
|
import uuid |
|
import glob |
|
import os |
|
import asyncio |
|
from image_fetcher import main |
|
from video import create_text_image |
|
|
|
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("**","") |
|
if prompts == '': |
|
return jsonify({"error": "prompts be must"}), 400 |
|
image_folder = "/tmp/images" |
|
|
|
|
|
raw_lines = prompts.splitlines(keepends=False) |
|
lines = [] |
|
|
|
i = 0 |
|
while i < len(raw_lines): |
|
line = raw_lines[i].strip() |
|
|
|
|
|
if line.endswith('?') or line.endswith(';'): |
|
heading = line |
|
i += 1 |
|
|
|
|
|
paragraph_lines = [] |
|
while i < len(raw_lines) and not (raw_lines[i].strip().endswith('?') or raw_lines[i].strip().endswith(':')): |
|
if raw_lines[i].strip(): |
|
paragraph_lines.append(raw_lines[i].strip()) |
|
i += 1 |
|
|
|
|
|
chunk_size = 4 |
|
for idx, j in enumerate(range(0, len(paragraph_lines), chunk_size)): |
|
chunk = paragraph_lines[j:j+chunk_size] |
|
if idx == 0: |
|
|
|
block = heading + '\n' + '\n'.join(chunk) |
|
else: |
|
|
|
block = '\n'.join(chunk) |
|
lines.append(block) |
|
else: |
|
|
|
block = '\n'.join(raw_lines[i:i+5]) |
|
lines.append(block) |
|
i += 5 |
|
print(lines) |
|
image_olst=[] |
|
for id in range(len(lines)): |
|
create_text_image(lines[id],id,image_olst) |
|
image_files = sorted(glob.glob(os.path.join(image_folder, "*.png"))) |
|
if not image_files: |
|
raise ValueError("No images found in folder!") |
|
|
|
clips = [ImageClip(m).set_duration(5) for m in image_files] |
|
video = concatenate_videoclips(clips, method="compose") |
|
video_path = f"/tmp/video_{uuid.uuid4().hex}.mp4" |
|
video.write_videofile(video_path, fps=24) |
|
for img in image_files: |
|
os.remove(img) |
|
|
|
return send_file(video_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) |
|
|
|
|
|
|
|
|