diff --git a/.env.cloud.example b/.env.cloud.example index b5f42e2..a065a57 100644 --- a/.env.cloud.example +++ b/.env.cloud.example @@ -36,3 +36,7 @@ OPENSHORTS_LOGO_URL=https://openshorts.app/logo.png # hosted server). Recommended: DataImpulse (~$1/GB PAYG). Downloads are capped # to 720p when this is set, to control bandwidth cost. Empty = direct download. PROXY_URL= + +# PO-token provider for 720p/1080p YouTube (bgutil sidecar in docker-compose.cloud.yml). +# Defaults to the sidecar; only override if you run bgutil elsewhere. +BGUTIL_BASE_URL=http://bgutil:4416 diff --git a/Dockerfile b/Dockerfile index 31895e2..56f2486 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,13 +35,18 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ nodejs \ && rm -rf /var/lib/apt/lists/* +# Deno JS runtime — yt-dlp needs it to solve YouTube's nsig challenge for 720p+ +# (used together with the bgutil PO-token sidecar in cloud mode). +COPY --from=denoland/deno:bin /deno /usr/local/bin/deno + # Copy virtual env from builder COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" ENV PYTHONUNBUFFERED=1 -# Always upgrade yt-dlp to latest (YouTube bot-detection changes frequently) -RUN pip install --upgrade --no-cache-dir yt-dlp +# Latest yt-dlp (nightly, YouTube changes constantly) + the bgutil PO-token +# provider plugin. Together with Deno + the bgutil sidecar this unlocks 720p/1080p. +RUN pip install --upgrade --pre --no-cache-dir "yt-dlp[default]" bgutil-ytdlp-pot-provider # Copy application code COPY . . diff --git a/cloud/metering.py b/cloud/metering.py index 965336a..43e4465 100644 --- a/cloud/metering.py +++ b/cloud/metering.py @@ -72,14 +72,19 @@ def probe_url_minutes(url: str) -> float: bandwidth. Raises ValueError if the duration is unknown (e.g. live streams). """ import yt_dlp - opts = { - "skip_download": True, "quiet": True, "no_warnings": True, - "extractor_args": { + bgutil = os.environ.get("BGUTIL_BASE_URL", "").strip() + if bgutil: + extractor_args = {"youtubepot-bgutilhttp": {"base_url": [bgutil]}} + else: + extractor_args = { "youtube": { "player_client": ["tv_embed", "android", "mweb", "web"], "player_skip": ["webpage", "configs"], } - }, + } + opts = { + "skip_download": True, "quiet": True, "no_warnings": True, + "extractor_args": extractor_args, } proxy = os.environ.get("PROXY_URL", "").strip() if proxy: diff --git a/docker-compose.cloud.yml b/docker-compose.cloud.yml index 662c547..1659913 100644 --- a/docker-compose.cloud.yml +++ b/docker-compose.cloud.yml @@ -21,10 +21,19 @@ services: retries: 10 restart: unless-stopped + # PO-token provider — lets yt-dlp fetch YouTube's GVS PO tokens so 720p/1080p + # formats are available (with Deno + a recent yt-dlp in the backend image). + bgutil: + image: brainicism/bgutil-ytdlp-pot-provider + container_name: openshorts-bgutil + restart: unless-stopped + backend: depends_on: db: condition: service_healthy + bgutil: + condition: service_started environment: - BILLING_ENABLED=true - DATABASE_URL=postgresql+asyncpg://openshorts:${POSTGRES_PASSWORD:-openshorts}@db:5432/openshorts @@ -47,6 +56,8 @@ services: # Residential proxy for YouTube downloads (avoids datacenter-IP bans). # e.g. http://user:pass@gate.dataimpulse.com:823 — leave empty to go direct. - PROXY_URL=${PROXY_URL:-} + # PO-token provider (bgutil sidecar) → unlocks 720p/1080p downloads. + - BGUTIL_BASE_URL=${BGUTIL_BASE_URL:-http://bgutil:4416} volumes: pgdata: diff --git a/main.py b/main.py index f8fa0f9..b0a1824 100644 --- a/main.py +++ b/main.py @@ -481,9 +481,24 @@ def download_youtube_video(url, output_dir="."): if _proxy: print("🌐 Using residential proxy for download.") - # Common yt-dlp options to work around YouTube bot detection. - # extractor_args tries multiple player clients in order; tv_embed / android - # avoid the OAuth/PO-token checks that block server IPs. + # YouTube client strategy: + # - Hosted (BGUTIL_BASE_URL set): use yt-dlp's default clients + the bgutil + # PO-token provider → unlocks 720p/1080p (needs the bgutil sidecar + a Deno + # JS runtime + a recent yt-dlp, all baked into the cloud image). + # - Self-host (no bgutil): fall back to the conservative tv_embed/android + # clients that work without a PO-token provider (caps around 360p, but no + # extra infra required). + _bgutil = os.environ.get("BGUTIL_BASE_URL", "").strip() + if _bgutil: + _extractor_args = {'youtubepot-bgutilhttp': {'base_url': [_bgutil]}} + else: + _extractor_args = { + 'youtube': { + 'player_client': ['tv_embed', 'android', 'mweb', 'web'], + 'player_skip': ['webpage', 'configs'], + } + } + _COMMON_YDL_OPTS = { 'quiet': False, 'verbose': True, @@ -495,12 +510,7 @@ def download_youtube_video(url, output_dir="."): 'fragment_retries': 10, 'nocheckcertificate': True, 'cachedir': False, - 'extractor_args': { - 'youtube': { - 'player_client': ['tv_embed', 'android', 'mweb', 'web'], - 'player_skip': ['webpage', 'configs'], - } - }, + 'extractor_args': _extractor_args, 'http_headers': { 'User-Agent': ( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '