Spaces:
Running
🚀 Deploy from GitHub @ 4eb79b5e42a5d81d582a63b79ba4afd35d266c0b
Browse filesTriggered by: Smasduq
Message: Speed up video processing pipeline
Encoding:
- Remux H.264/AAC sources that fit the tier cap with -c copy instead of
re-encoding them.
- Never upscale: filter the rendition ladder against the source height and
drop the previously unused _target_height.
- Free tier now emits a single rendition (min(source, 720)); the 1080/720/480
ladder stays for Pro.
- Set -threads per encoder so multi-rendition jobs stop oversubscribing
small Spaces, and probe the source with one ffprobe call.
Pipeline overlap:
- Upload HLS segments to S3 while ffmpeg is still encoding (12 concurrent
PUTs), removing the post-encode transfer tail.
- Stream downloads straight to disk instead of buffering the whole file, with
progress reported to Redis.
- Pull the cover frame from an encoded segment instead of re-reading and
re-probing the source.
- Stage progress (download 2-14%, encode 15-90%) so the UI moves immediately.
Reliability:
- Rust /process returns 503 when the queue push fails instead of a 200 with
{'status': 'error'} that the backend treated as success.
- Completion payload reports produced 'variants'; the backend only wires
per-resolution URLs that exist.
- Retries wipe the output dir so stale segments cannot be uploaded.
- Storage init failure writes an error status instead of returning silently.
- Beat task pings the video service every 4 minutes to avoid cold starts.
- RUST_SERVICE_URL timeout raised to 60s for cold starts.
Adds MAX_CONCURRENT_JOBS and UPLOAD_CONCURRENCY knobs.
- app/tasks/video_tasks.py +47 -2
- app/worker.py +6 -0
|
@@ -100,9 +100,27 @@ def _finalize_transcode_success(
|
|
| 100 |
logger.info(f"Finalizing transcode for video_id={video_id} task_id={task_id}")
|
| 101 |
video_url = storage.get_url(f"videos/{task_id}/master.m3u8")
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if video_type == "flash":
|
| 104 |
url_480p = url_720p = url_1080p = url_2k = url_4k = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
|
|
|
| 106 |
url_480p = storage.get_url(f"videos/{task_id}/480p.m3u8")
|
| 107 |
url_720p = storage.get_url(f"videos/{task_id}/720p.m3u8")
|
| 108 |
url_1080p = storage.get_url(f"videos/{task_id}/1080p.m3u8")
|
|
@@ -119,7 +137,6 @@ def _finalize_transcode_success(
|
|
| 119 |
return {"status": "error", "message": "video missing"}
|
| 120 |
|
| 121 |
# Cover metadata: prefer Rust completion payload, then upload_jobs row
|
| 122 |
-
status_data = status_data or {}
|
| 123 |
rust_cover_url = status_data.get("cover_url") or status_data.get("coverUrl")
|
| 124 |
rust_cover_source = status_data.get("cover_source") or status_data.get("coverSource")
|
| 125 |
|
|
@@ -406,10 +423,20 @@ def process_video_task(
|
|
| 406 |
rust_response = requests.post(
|
| 407 |
f"{config.RUST_SERVICE_URL}/process",
|
| 408 |
json=rust_payload,
|
| 409 |
-
|
|
|
|
| 410 |
)
|
| 411 |
logger.info(f"Rust responded {rust_response.status_code}")
|
| 412 |
rust_response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
except requests.exceptions.RequestException as re:
|
| 414 |
err = f"Rust service unreachable or returned error: {re}"
|
| 415 |
logger.error(err)
|
|
@@ -622,3 +649,21 @@ def update_discovery_score_task(video_id: int = None, post_id: int = None):
|
|
| 622 |
db.commit()
|
| 623 |
finally:
|
| 624 |
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
logger.info(f"Finalizing transcode for video_id={video_id} task_id={task_id}")
|
| 101 |
video_url = storage.get_url(f"videos/{task_id}/master.m3u8")
|
| 102 |
|
| 103 |
+
# Rust reports which rendition playlists it actually produced. Advertising a
|
| 104 |
+
# variant that was skipped (e.g. a passthrough job that emits only one) would
|
| 105 |
+
# give the player a 404 when the user picks that quality.
|
| 106 |
+
status_data = status_data or {}
|
| 107 |
+
raw_variants = status_data.get("variants")
|
| 108 |
+
produced = (
|
| 109 |
+
{str(v).strip().lower() for v in raw_variants if isinstance(v, str)}
|
| 110 |
+
if isinstance(raw_variants, list)
|
| 111 |
+
else set()
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
if video_type == "flash":
|
| 115 |
url_480p = url_720p = url_1080p = url_2k = url_4k = None
|
| 116 |
+
elif produced:
|
| 117 |
+
url_480p = storage.get_url(f"videos/{task_id}/480p.m3u8") if "480p" in produced else None
|
| 118 |
+
url_720p = storage.get_url(f"videos/{task_id}/720p.m3u8") if "720p" in produced else None
|
| 119 |
+
url_1080p = storage.get_url(f"videos/{task_id}/1080p.m3u8") if "1080p" in produced else None
|
| 120 |
+
url_2k = storage.get_url(f"videos/{task_id}/2k.m3u8") if "2k" in produced else None
|
| 121 |
+
url_4k = storage.get_url(f"videos/{task_id}/4k.m3u8") if "4k" in produced else None
|
| 122 |
else:
|
| 123 |
+
# Older Rust builds don't send `variants`; keep the legacy behaviour.
|
| 124 |
url_480p = storage.get_url(f"videos/{task_id}/480p.m3u8")
|
| 125 |
url_720p = storage.get_url(f"videos/{task_id}/720p.m3u8")
|
| 126 |
url_1080p = storage.get_url(f"videos/{task_id}/1080p.m3u8")
|
|
|
|
| 137 |
return {"status": "error", "message": "video missing"}
|
| 138 |
|
| 139 |
# Cover metadata: prefer Rust completion payload, then upload_jobs row
|
|
|
|
| 140 |
rust_cover_url = status_data.get("cover_url") or status_data.get("coverUrl")
|
| 141 |
rust_cover_source = status_data.get("cover_source") or status_data.get("coverSource")
|
| 142 |
|
|
|
|
| 423 |
rust_response = requests.post(
|
| 424 |
f"{config.RUST_SERVICE_URL}/process",
|
| 425 |
json=rust_payload,
|
| 426 |
+
# Cold-starting Space can take ~30s to answer; give it headroom.
|
| 427 |
+
timeout=60.0,
|
| 428 |
)
|
| 429 |
logger.info(f"Rust responded {rust_response.status_code}")
|
| 430 |
rust_response.raise_for_status()
|
| 431 |
+
# Older Rust builds answered 200 even when the queue push failed.
|
| 432 |
+
try:
|
| 433 |
+
rust_body = rust_response.json()
|
| 434 |
+
except ValueError:
|
| 435 |
+
rust_body = None
|
| 436 |
+
if isinstance(rust_body, dict) and str(rust_body.get("status", "")).lower() == "error":
|
| 437 |
+
raise requests.exceptions.RequestException(
|
| 438 |
+
f"Rust rejected task: {rust_body.get('message', 'unknown error')}"
|
| 439 |
+
)
|
| 440 |
except requests.exceptions.RequestException as re:
|
| 441 |
err = f"Rust service unreachable or returned error: {re}"
|
| 442 |
logger.error(err)
|
|
|
|
| 649 |
db.commit()
|
| 650 |
finally:
|
| 651 |
db.close()
|
| 652 |
+
|
| 653 |
+
|
| 654 |
+
@shared_task(name="video_tasks.warm_video_service")
|
| 655 |
+
def warm_video_service():
|
| 656 |
+
"""Ping the Rust service so a sleeping Space stays awake.
|
| 657 |
+
|
| 658 |
+
A cold Hugging Face Space takes 30-60s to answer, which the user experiences
|
| 659 |
+
as "the upload did nothing". Beat runs this every few minutes.
|
| 660 |
+
"""
|
| 661 |
+
url = (config.RUST_SERVICE_URL or "").rstrip("/")
|
| 662 |
+
if not url:
|
| 663 |
+
return {"status": "skipped", "reason": "RUST_SERVICE_URL not set"}
|
| 664 |
+
try:
|
| 665 |
+
resp = requests.get(f"{url}/health", timeout=15.0)
|
| 666 |
+
return {"status": "ok", "code": resp.status_code}
|
| 667 |
+
except Exception as e:
|
| 668 |
+
logger.debug(f"Video service warm-up failed: {e}")
|
| 669 |
+
return {"status": "error", "error": str(e)}
|
|
@@ -85,6 +85,12 @@ celery_app.conf.update(
|
|
| 85 |
"task": "tasks.events.flush_video_events",
|
| 86 |
"schedule": 60.0,
|
| 87 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
},
|
| 89 |
)
|
| 90 |
|
|
|
|
| 85 |
"task": "tasks.events.flush_video_events",
|
| 86 |
"schedule": 60.0,
|
| 87 |
},
|
| 88 |
+
# Keep the Rust video service warm — HF Spaces sleep when idle and a
|
| 89 |
+
# cold start costs 30-60s on the first upload.
|
| 90 |
+
"warm-video-service": {
|
| 91 |
+
"task": "video_tasks.warm_video_service",
|
| 92 |
+
"schedule": 240.0,
|
| 93 |
+
},
|
| 94 |
},
|
| 95 |
)
|
| 96 |
|