fix: pornhub download format spec and add UA headers

This commit is contained in:
mini
2026-02-19 00:06:08 +08:00
parent 0856b001a9
commit 0299aea39b

View File

@@ -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],
}