ruslanmv commited on
Commit
229d463
·
verified ·
1 Parent(s): 41caac7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -44
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 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
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"Unexpected error: {e}")
 
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