Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from flask import Flask, request, jsonify, send_file
|
|
| 2 |
from moviepy.editor import ColorClip,ImageClip, concatenate_videoclips,VideoFileClip
|
| 3 |
import traceback
|
| 4 |
import uuid
|
|
|
|
| 5 |
import glob
|
| 6 |
import os
|
| 7 |
import requests
|
|
@@ -15,10 +16,32 @@ app = Flask(__name__)
|
|
| 15 |
SAVE_DIR = "/app/data/video/"
|
| 16 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@app.route("/")
|
| 19 |
def home():
|
| 20 |
return "Flask Video Generator is Running"
|
| 21 |
|
|
|
|
|
|
|
| 22 |
@app.route("/generate", methods=["POST"])
|
| 23 |
def generate_video():
|
| 24 |
try:
|
|
@@ -117,6 +140,16 @@ def generate_video():
|
|
| 117 |
else:
|
| 118 |
raise Exception(f"Error from Space B: {response.text}")
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
video_path = video_com(lines)
|
| 121 |
#for img in image_files:
|
| 122 |
#os.remove(img)
|
|
|
|
| 2 |
from moviepy.editor import ColorClip,ImageClip, concatenate_videoclips,VideoFileClip
|
| 3 |
import traceback
|
| 4 |
import uuid
|
| 5 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 6 |
import glob
|
| 7 |
import os
|
| 8 |
import requests
|
|
|
|
| 16 |
SAVE_DIR = "/app/data/video/"
|
| 17 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 18 |
|
| 19 |
+
def send_request_to_space(id,lines,url):
|
| 20 |
+
payload = {
|
| 21 |
+
"id": id,
|
| 22 |
+
"lines": lines # or [line] depending on your expected payload
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = requests.post(url, json=payload)
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
video_path = f"/app/data/video/clip{id}.mp4"
|
| 29 |
+
with open(video_path, "wb") as f:
|
| 30 |
+
f.write(response.content)
|
| 31 |
+
print(video_path)
|
| 32 |
+
else:
|
| 33 |
+
raise Exception(f"[{url}] HTTP {response.status_code}: {response.text}")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error for ID {id}: {e}")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
|
| 39 |
@app.route("/")
|
| 40 |
def home():
|
| 41 |
return "Flask Video Generator is Running"
|
| 42 |
|
| 43 |
+
|
| 44 |
+
|
| 45 |
@app.route("/generate", methods=["POST"])
|
| 46 |
def generate_video():
|
| 47 |
try:
|
|
|
|
| 140 |
else:
|
| 141 |
raise Exception(f"Error from Space B: {response.text}")
|
| 142 |
|
| 143 |
+
with ThreadPoolExecutor(max_workers=10) as executor:
|
| 144 |
+
for id, line in enumerate(lines):
|
| 145 |
+
url = space_b_urls[id % len(space_b_urls)] # reuse if more lines than URLs
|
| 146 |
+
futures.append(executor.submit(send_request_to_space, id, lines, url))
|
| 147 |
+
|
| 148 |
+
for future in as_completed(futures):
|
| 149 |
+
result = future.result()
|
| 150 |
+
if result:
|
| 151 |
+
video_paths.append(result)
|
| 152 |
+
|
| 153 |
video_path = video_com(lines)
|
| 154 |
#for img in image_files:
|
| 155 |
#os.remove(img)
|