Spaces:
Running
Running
| import threading | |
| from queue import Queue | |
| import uuid | |
| import time | |
| from .job_queue import jobs, update, notify_webhook | |
| from .transcription import transcribe_video | |
| from .render import render_video | |
| from .highlights import detect_highlights | |
| batch_queue = Queue() | |
| def create_batch_job(video_path, webhook=None): | |
| job_id = str(uuid.uuid4()) | |
| jobs[job_id] = { | |
| "id": job_id, | |
| "status": "queued", | |
| "progress": 0, | |
| "clips": [], | |
| "video": video_path, | |
| "webhook": webhook, | |
| } | |
| batch_queue.put(job_id) | |
| return job_id | |
| def worker(): | |
| while True: | |
| job_id = batch_queue.get() | |
| job = jobs[job_id] | |
| try: | |
| update(job_id, status="processing", progress=5) | |
| # 1. TRANSCRIBE | |
| words = transcribe_video(job["video"]) | |
| update(job_id, progress=30) | |
| # 2. DETECT HIGHLIGHTS | |
| highlights = detect_highlights(words) | |
| update(job_id, progress=50) | |
| outputs = [] | |
| # 3. RENDER MULTIPLE CLIPS | |
| for i, segment in enumerate(highlights): | |
| start = segment[0]["start"] | |
| end = segment[-1]["end"] | |
| clip_path = render_video(job["video"], words) | |
| outputs.append(clip_path) | |
| update(job_id, progress=50 + int((i+1)/len(highlights)*40)) | |
| # 4. FINALIZE | |
| update(job_id, | |
| status="completed", | |
| progress=100, | |
| clips=outputs) | |
| notify_webhook(job_id) | |
| except Exception as e: | |
| update(job_id, | |
| status="failed", | |
| error=str(e)) | |
| notify_webhook(job_id) | |
| batch_queue.task_done() | |
| def start_batch_worker(): | |
| t = threading.Thread(target=worker, daemon=True) | |
| t.start() |