Update app.py
Browse files
app.py
CHANGED
@@ -33,62 +33,52 @@ import yt_dlp
|
|
33 |
import time
|
34 |
import random
|
35 |
|
36 |
-
def download_video(url):
|
37 |
"""
|
38 |
-
Downloads a video from
|
39 |
-
|
40 |
-
Args:
|
41 |
-
url (str): The URL of the video to download.
|
42 |
-
|
43 |
-
Returns:
|
44 |
-
str: The path to the downloaded file, or None if an error occurred.
|
45 |
"""
|
46 |
print("Simulating human-like download...")
|
47 |
|
48 |
-
#
|
49 |
-
delay = random.uniform(
|
50 |
-
print(f"
|
51 |
time.sleep(delay)
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
'
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
75 |
|
|
|
76 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
77 |
-
|
78 |
-
filename = ydl.prepare_filename(
|
79 |
-
print("Downloaded:", filename)
|
80 |
return filename
|
81 |
-
except yt_dlp.utils.DownloadError as e:
|
82 |
-
print(f"Download error: {e}")
|
83 |
except Exception as e:
|
84 |
-
print(f"
|
|
|
85 |
|
86 |
-
return None
|
87 |
-
|
88 |
-
# Example usage
|
89 |
url = "https://www.youtube.com/watch?v=uLVRZE8OAI4"
|
90 |
-
download_video(url)
|
91 |
-
|
92 |
|
93 |
|
94 |
|
|
|
33 |
import time
|
34 |
import random
|
35 |
|
36 |
+
def download_video(url, cookies_file=None):
|
37 |
"""
|
38 |
+
Downloads a video from YouTube with bot evasion techniques
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
"""
|
40 |
print("Simulating human-like download...")
|
41 |
|
42 |
+
# Randomized human-like delay
|
43 |
+
delay = random.uniform(1, 4)
|
44 |
+
print(f"Artificial delay: {delay:.2f}s")
|
45 |
time.sleep(delay)
|
46 |
|
47 |
+
ydl_opts = {
|
48 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
49 |
+
'outtmpl': '%(title)s.%(ext)s',
|
50 |
+
'noprogress': True,
|
51 |
+
'quiet': False,
|
52 |
+
|
53 |
+
# Modern browser headers
|
54 |
+
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
55 |
+
'headers': {
|
56 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
57 |
+
'Accept-Language': 'en-US,en;q=0.5',
|
58 |
+
'Sec-Fetch-Mode': 'navigate',
|
59 |
+
},
|
60 |
+
|
61 |
+
# Cookie authentication
|
62 |
+
'cookiefile': cookies_file.name if cookies_file else None,
|
63 |
+
|
64 |
+
# Advanced bot evasion
|
65 |
+
'throttledratelimit': random.randint(500, 1000),
|
66 |
+
'retries': 3,
|
67 |
+
'fragment_retries': 5,
|
68 |
+
'skip_download': False,
|
69 |
+
}
|
70 |
|
71 |
+
try:
|
72 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
73 |
+
info = ydl.extract_info(url, download=True)
|
74 |
+
filename = ydl.prepare_filename(info)
|
|
|
75 |
return filename
|
|
|
|
|
76 |
except Exception as e:
|
77 |
+
print(f"Error: {str(e)}")
|
78 |
+
raise gr.Error(f"Download failed: {str(e)}")
|
79 |
|
|
|
|
|
|
|
80 |
url = "https://www.youtube.com/watch?v=uLVRZE8OAI4"
|
81 |
+
download_video(url)
|
|
|
82 |
|
83 |
|
84 |
|