Add cookies support to yt_download.py for YouTube bot detection bypass
This commit is contained in:
parent
8e41bf21f6
commit
47509b4f06
@ -6,13 +6,14 @@ Primer:
|
||||
python3 yt_download.py "https://youtu.be/dQw4w9WgXcQ" /data/uploads/video.mp4
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
|
||||
def download(url, output, max_height=1080, format_str=None):
|
||||
def download(url, output, max_height=1080, format_str=None, cookies_file=None):
|
||||
"""
|
||||
Download YT video. Privzeto: best mp4 ≤1080p z audiotrackom.
|
||||
"""
|
||||
@ -30,8 +31,23 @@ def download(url, output, max_height=1080, format_str=None):
|
||||
"--write-info-json",
|
||||
"--restrict-filenames",
|
||||
"-o", str(output),
|
||||
url,
|
||||
]
|
||||
|
||||
# Auto-detect cookies file če ni eksplicitno podan
|
||||
if cookies_file is None:
|
||||
for candidate in [
|
||||
"/data/cookies/youtube.txt",
|
||||
os.environ.get("YT_COOKIES_FILE", ""),
|
||||
]:
|
||||
if candidate and Path(candidate).exists():
|
||||
cookies_file = candidate
|
||||
break
|
||||
|
||||
if cookies_file and Path(cookies_file).exists():
|
||||
cmd += ["--cookies", str(cookies_file)]
|
||||
print(f"🍪 Using cookies: {cookies_file}", file=sys.stderr)
|
||||
|
||||
cmd.append(url)
|
||||
print(f"⬇ Downloading {url}...", file=sys.stderr)
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
@ -41,9 +57,17 @@ def download(url, output, max_height=1080, format_str=None):
|
||||
return output
|
||||
|
||||
|
||||
def get_info(url):
|
||||
def get_info(url, cookies_file=None):
|
||||
"""Vrni metadata brez prenosa."""
|
||||
cmd = ["yt-dlp", "--dump-json", "--no-playlist", url]
|
||||
cmd = ["yt-dlp", "--dump-json", "--no-playlist"]
|
||||
if cookies_file is None:
|
||||
for candidate in ["/data/cookies/youtube.txt", os.environ.get("YT_COOKIES_FILE", "")]:
|
||||
if candidate and Path(candidate).exists():
|
||||
cookies_file = candidate
|
||||
break
|
||||
if cookies_file and Path(cookies_file).exists():
|
||||
cmd += ["--cookies", str(cookies_file)]
|
||||
cmd.append(url)
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
return None
|
||||
@ -55,12 +79,14 @@ def main():
|
||||
ap.add_argument("url")
|
||||
ap.add_argument("output")
|
||||
ap.add_argument("--max-height", type=int, default=1080)
|
||||
ap.add_argument("--cookies", default=None,
|
||||
help="Pot do cookies.txt (Netscape format)")
|
||||
ap.add_argument("--info-only", action="store_true",
|
||||
help="Samo metadata, brez prenosa")
|
||||
args = ap.parse_args()
|
||||
|
||||
if args.info_only:
|
||||
info = get_info(args.url)
|
||||
info = get_info(args.url, cookies_file=args.cookies)
|
||||
if info:
|
||||
print(json.dumps({
|
||||
"title": info.get("title"),
|
||||
@ -73,7 +99,7 @@ def main():
|
||||
sys.exit(1)
|
||||
return
|
||||
|
||||
download(args.url, args.output, max_height=args.max_height)
|
||||
download(args.url, args.output, max_height=args.max_height, cookies_file=args.cookies)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Loading…
Reference in New Issue
Block a user