Update app.py
Browse files
app.py
CHANGED
@@ -29,31 +29,50 @@ import yt_dlp
|
|
29 |
|
30 |
import yt_dlp
|
31 |
|
|
|
|
|
|
|
|
|
32 |
def download_video(url):
|
33 |
"""
|
34 |
-
Downloads a video from a URL using yt-dlp.
|
|
|
35 |
Args:
|
36 |
-
url: The URL of the video to download.
|
|
|
37 |
Returns:
|
38 |
-
The path to the downloaded file, or None if an error occurred.
|
39 |
"""
|
40 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
try:
|
43 |
ydl_opts = {
|
44 |
-
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
45 |
'merge_output_format': 'mp4', # Ensure final output is mp4
|
46 |
'outtmpl': '%(title)s.%(ext)s', # Use title for filename
|
47 |
'noprogress': True, # Remove progress bar output
|
48 |
-
'quiet': False, # Show
|
49 |
-
|
50 |
-
#
|
51 |
-
|
52 |
-
#
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
}
|
56 |
-
|
57 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
58 |
info_dict = ydl.extract_info(url, download=True)
|
59 |
filename = ydl.prepare_filename(info_dict) # Get the actual filename
|
@@ -70,6 +89,8 @@ def download_video(url):
|
|
70 |
|
71 |
|
72 |
|
|
|
|
|
73 |
def validate_youtube(url):
|
74 |
"""
|
75 |
Validates a YouTube URL, checks if the video exists, and returns whether its length exceeds 10 minutes.
|
|
|
29 |
|
30 |
import yt_dlp
|
31 |
|
32 |
+
import yt_dlp
|
33 |
+
import time
|
34 |
+
import random
|
35 |
+
|
36 |
def download_video(url):
|
37 |
"""
|
38 |
+
Downloads a video from a URL using yt-dlp while emulating human behavior.
|
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 |
+
# Introduce a randomized delay to mimic human behavior
|
49 |
+
delay = random.uniform(2, 5) # Random delay between 2 to 5 seconds
|
50 |
+
print(f"Waiting for {delay:.2f} seconds before starting the download...")
|
51 |
+
time.sleep(delay)
|
52 |
|
53 |
try:
|
54 |
ydl_opts = {
|
55 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
56 |
'merge_output_format': 'mp4', # Ensure final output is mp4
|
57 |
'outtmpl': '%(title)s.%(ext)s', # Use title for filename
|
58 |
'noprogress': True, # Remove progress bar output
|
59 |
+
'quiet': False, # Show logs
|
60 |
+
'nocheckcertificate': True, # Bypass SSL verification issues
|
61 |
+
'sleep_interval': random.uniform(1, 3), # Random sleep between requests
|
62 |
+
|
63 |
+
# **Emulating a real browser**
|
64 |
+
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
65 |
+
'headers': {
|
66 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
67 |
+
'Accept-Encoding': 'gzip, deflate, br',
|
68 |
+
'Referer': 'https://www.youtube.com/',
|
69 |
+
'Connection': 'keep-alive'
|
70 |
+
},
|
71 |
+
|
72 |
+
# **Using browser cookies for authentication**
|
73 |
+
'cookies-from-browser': 'chrome', # Change to 'firefox' if needed
|
74 |
}
|
75 |
+
|
76 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
77 |
info_dict = ydl.extract_info(url, download=True)
|
78 |
filename = ydl.prepare_filename(info_dict) # Get the actual filename
|
|
|
89 |
|
90 |
|
91 |
|
92 |
+
|
93 |
+
|
94 |
def validate_youtube(url):
|
95 |
"""
|
96 |
Validates a YouTube URL, checks if the video exists, and returns whether its length exceeds 10 minutes.
|