From 0299aea39b4f9d450fae4d00119f7fab67a16297 Mon Sep 17 00:00:00 2001 From: mini Date: Thu, 19 Feb 2026 00:06:08 +0800 Subject: [PATCH] fix: pornhub download format spec and add UA headers --- backend/app/services/downloader.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/app/services/downloader.py b/backend/app/services/downloader.py index 17fc31d..7cc5ecc 100644 --- a/backend/app/services/downloader.py +++ b/backend/app/services/downloader.py @@ -313,6 +313,12 @@ def _download_youtube_video(url: str, format_id: str = "best", progress_callback } +_PH_HEADERS = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36", + "Referer": "https://www.pornhub.com/", +} + + def _parse_pornhub_video(url: str) -> dict: """Parse Pornhub video info using yt-dlp.""" ydl_opts = { @@ -320,6 +326,7 @@ def _parse_pornhub_video(url: str) -> dict: "no_warnings": True, "extract_flat": False, "skip_download": True, + "http_headers": _PH_HEADERS, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=False) @@ -372,9 +379,11 @@ def _download_pornhub_video(url: str, format_id: str = "best", progress_callback output_template = os.path.join(PH_VIDEOS_PATH, f"%(id)s_{task_id}.%(ext)s") if format_id == "best": - format_spec = "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" + # Prefer mp4 with audio; fall back to best available + format_spec = "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio/best[ext=mp4]/best" else: - format_spec = f"{format_id}+bestaudio/best" + # The format may already contain audio (merged); try with audio fallback gracefully + format_spec = f"{format_id}+bestaudio/{format_id}/best" def hook(d): if d["status"] == "downloading" and progress_callback: @@ -391,6 +400,7 @@ def _download_pornhub_video(url: str, format_id: str = "best", progress_callback "merge_output_format": "mp4", "quiet": True, "no_warnings": True, + "http_headers": _PH_HEADERS, "progress_hooks": [hook], }