Spaces:
Running
Running
Create downloader.py
Browse files- ingestion/downloader.py +74 -0
ingestion/downloader.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import uuid
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
OUTPUT_DIR = "jobs"
|
| 6 |
+
|
| 7 |
+
COOKIE_FILE = "engine/ingestion/cookies/youtube.txt"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def run_cmd(cmd):
|
| 11 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 12 |
+
|
| 13 |
+
if result.returncode == 0:
|
| 14 |
+
return True, result.stdout
|
| 15 |
+
|
| 16 |
+
return False, result.stderr
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def yt_dlp_download(url):
|
| 20 |
+
|
| 21 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 22 |
+
output = f"{OUTPUT_DIR}/{uuid.uuid4()}.mp4"
|
| 23 |
+
|
| 24 |
+
base_cmd = [
|
| 25 |
+
"yt-dlp",
|
| 26 |
+
"-f", "bestvideo+bestaudio/best",
|
| 27 |
+
"--merge-output-format", "mp4",
|
| 28 |
+
"--no-playlist",
|
| 29 |
+
"--retries", "3",
|
| 30 |
+
"--extractor-retries", "3",
|
| 31 |
+
"-o", output,
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
strategies = [
|
| 35 |
+
|
| 36 |
+
# Strategy 1 — normal
|
| 37 |
+
base_cmd + [url],
|
| 38 |
+
|
| 39 |
+
# Strategy 2 — mobile user agent
|
| 40 |
+
base_cmd + [
|
| 41 |
+
"--user-agent",
|
| 42 |
+
"Mozilla/5.0 (Linux; Android 10)",
|
| 43 |
+
url,
|
| 44 |
+
],
|
| 45 |
+
|
| 46 |
+
# Strategy 3 — cookies auth
|
| 47 |
+
base_cmd + [
|
| 48 |
+
"--cookies",
|
| 49 |
+
COOKIE_FILE,
|
| 50 |
+
url,
|
| 51 |
+
],
|
| 52 |
+
|
| 53 |
+
# Strategy 4 — cookies + mobile
|
| 54 |
+
base_cmd + [
|
| 55 |
+
"--cookies",
|
| 56 |
+
COOKIE_FILE,
|
| 57 |
+
"--user-agent",
|
| 58 |
+
"Mozilla/5.0 (Linux; Android 10)",
|
| 59 |
+
url,
|
| 60 |
+
],
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
for attempt, cmd in enumerate(strategies, 1):
|
| 64 |
+
|
| 65 |
+
print(f"[V8.2] Attempt {attempt}")
|
| 66 |
+
|
| 67 |
+
ok, log = run_cmd(cmd)
|
| 68 |
+
|
| 69 |
+
if ok and os.path.exists(output):
|
| 70 |
+
return output
|
| 71 |
+
|
| 72 |
+
print(log)
|
| 73 |
+
|
| 74 |
+
raise Exception("All download strategies failed")
|