Ajusta projeto para consumir uma fila
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import unicodedata
|
|
||||||
|
|
||||||
from moviepy.video.io.VideoFileClip import VideoFileClip
|
from moviepy.video.io.VideoFileClip import VideoFileClip
|
||||||
from moviepy.video.VideoClip import ColorClip
|
from moviepy.video.VideoClip import ColorClip
|
||||||
@@ -126,12 +124,15 @@ def process_full_video(filename: str, times: list = None) -> list:
|
|||||||
if end is None:
|
if end is None:
|
||||||
with VideoFileClip(video_path) as clip:
|
with VideoFileClip(video_path) as clip:
|
||||||
end = clip.duration
|
end = clip.duration
|
||||||
|
|
||||||
print(f"Cortando trecho {idx}: {start}s a {end}s")
|
print(f"Cortando trecho {idx}: {start}s a {end}s")
|
||||||
|
|
||||||
temp_path = f"temp/{os.path.splitext(filename)[0]}_{idx}.mp4"
|
temp_path = f"temp/{os.path.splitext(filename)[0]}_{idx}.mp4"
|
||||||
|
|
||||||
cut_video_new_clip(video_path, start, end, temp_path)
|
cut_video_new_clip(video_path, start, end, temp_path)
|
||||||
|
|
||||||
out = process_segment(temp_path, top_text, bottom_text, filename, idx)
|
out = process_segment(temp_path, top_text, bottom_text, filename, idx)
|
||||||
|
|
||||||
processed.append(out)
|
processed.append(out)
|
||||||
|
|
||||||
return processed
|
return processed
|
||||||
|
|||||||
106
main.py
106
main.py
@@ -1,21 +1,30 @@
|
|||||||
import os
|
import os
|
||||||
|
import pika
|
||||||
|
import json
|
||||||
import requests
|
import requests
|
||||||
import threading
|
|
||||||
|
|
||||||
os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg"
|
|
||||||
|
|
||||||
from flask import Flask, request, jsonify
|
|
||||||
|
|
||||||
from components.video import process_full_video
|
from components.video import process_full_video
|
||||||
|
|
||||||
app = Flask(__name__)
|
RABBITMQ_HOST = os.environ.get('RABBITMQ_HOST', 'rabbitmq')
|
||||||
|
RABBITMQ_PORT = int(os.environ.get('RABBITMQ_PORT', 5672))
|
||||||
|
RABBITMQ_USER = os.environ.get('RABBITMQ_USER', 'admin')
|
||||||
|
RABBITMQ_PASS = os.environ.get('RABBITMQ_PASS')
|
||||||
|
RABBITMQ_QUEUE = os.environ.get('RABBITMQ_QUEUE', 'to-render')
|
||||||
|
|
||||||
def process_and_call_webhook(url, video_id, times, webhook_url, filename):
|
if not RABBITMQ_PASS:
|
||||||
|
raise RuntimeError("RABBITMQ_PASS não definido no ambiente")
|
||||||
|
|
||||||
|
def callback(ch, method, properties, body):
|
||||||
try:
|
try:
|
||||||
os.makedirs("videos", exist_ok=True)
|
data = json.loads(body)
|
||||||
os.makedirs("temp", exist_ok=True)
|
filename = data.get("filename")
|
||||||
|
times = data.get("times", [])
|
||||||
|
webhook_url = data.get("webhookUrl")
|
||||||
|
url = data.get("url")
|
||||||
|
video_id = data.get("videoId")
|
||||||
|
|
||||||
|
print(f"Processando vídeo: {filename}")
|
||||||
|
|
||||||
print(f"Working on video {filename}")
|
|
||||||
processed_files = process_full_video(filename, times)
|
processed_files = process_full_video(filename, times)
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
@@ -26,63 +35,44 @@ def process_and_call_webhook(url, video_id, times, webhook_url, filename):
|
|||||||
"videoId": video_id,
|
"videoId": video_id,
|
||||||
"error": False,
|
"error": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
|
||||||
resp = requests.post(webhook_url, json=payload, timeout=30)
|
|
||||||
|
|
||||||
print(f"Webhook status: {resp.status_code}, content: {resp.text}")
|
|
||||||
|
|
||||||
except Exception as webhook_error:
|
|
||||||
print(f"Erro ao chamar webhook: {webhook_error}")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
payload = {
|
payload = {
|
||||||
"videosProcessedQuantity": 0,
|
"videosProcessedQuantity": 0,
|
||||||
"filename": filename,
|
"filename": filename if 'filename' in locals() else None,
|
||||||
"processedFiles": processed_files,
|
"processedFiles": [],
|
||||||
"url": url,
|
"url": url if 'url' in locals() else None,
|
||||||
"videoId": video_id,
|
"videoId": video_id if 'video_id' in locals() else None,
|
||||||
"error": str(e),
|
"error": str(e),
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
|
||||||
resp = requests.post(webhook_url, json=payload, timeout=30)
|
|
||||||
|
|
||||||
print(f"Webhook send error status: {resp.status_code}")
|
|
||||||
|
|
||||||
print(str(e))
|
|
||||||
except Exception as webhook_error:
|
|
||||||
print(f"Erro ao chamar webhook: {webhook_error}")
|
|
||||||
|
|
||||||
print(f"Erro no processamento: {e}")
|
print(f"Erro no processamento: {e}")
|
||||||
|
|
||||||
@app.route('/process', methods=['POST'])
|
try:
|
||||||
def process_video():
|
if webhook_url:
|
||||||
|
resp = requests.post(webhook_url, json=payload, timeout=30)
|
||||||
|
print(f"Webhook status: {resp.status_code}")
|
||||||
|
except Exception as wh_err:
|
||||||
|
print(f"Erro ao chamar webhook: {wh_err}")
|
||||||
|
|
||||||
data = request.get_json()
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||||
|
|
||||||
if not data or not ("url" in data or "videoId" in data):
|
def main():
|
||||||
return jsonify({"error": "Informe 'url' ou 'videoId'"}), 400
|
credentials = pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASS)
|
||||||
|
parameters = pika.ConnectionParameters(
|
||||||
|
host=RABBITMQ_HOST,
|
||||||
|
port=RABBITMQ_PORT,
|
||||||
|
credentials=credentials,
|
||||||
|
heartbeat=600,
|
||||||
|
blocked_connection_timeout=300
|
||||||
|
)
|
||||||
|
connection = pika.BlockingConnection(parameters)
|
||||||
|
channel = connection.channel()
|
||||||
|
channel.queue_declare(queue=RABBITMQ_QUEUE, durable=True)
|
||||||
|
channel.basic_qos(prefetch_count=1)
|
||||||
|
channel.basic_consume(queue=RABBITMQ_QUEUE, on_message_callback=callback)
|
||||||
|
|
||||||
url = data.get("url")
|
print(' [*] Esperando mensagens. Para sair: CTRL+C')
|
||||||
video_id = data.get("videoId")
|
|
||||||
times = data.get("times", [])
|
|
||||||
webhook_url = data.get("webhookUrl")
|
|
||||||
|
|
||||||
if not webhook_url:
|
channel.start_consuming()
|
||||||
return jsonify({"error": "Informe 'webhookUrl'"}), 400
|
|
||||||
|
|
||||||
filename = data.get("filename")
|
if __name__ == "__main__":
|
||||||
if not filename:
|
main()
|
||||||
return jsonify({"error": "Informe 'filename'"}), 400
|
|
||||||
|
|
||||||
threading.Thread(
|
|
||||||
target=process_and_call_webhook,
|
|
||||||
args=(url, video_id, times, webhook_url, filename),
|
|
||||||
daemon=True
|
|
||||||
).start()
|
|
||||||
|
|
||||||
return jsonify({"message": f"{video_id if video_id else url}"}), 200
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
flask
|
|
||||||
moviepy==2.2.0
|
moviepy==2.2.0
|
||||||
pillow==9.5.0
|
pillow==9.5.0
|
||||||
yt_dlp
|
|
||||||
requests
|
requests
|
||||||
|
pika
|
||||||
Reference in New Issue
Block a user