Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,52 +29,25 @@ import yt_dlp
|
|
| 29 |
import time
|
| 30 |
import random
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
"""
|
| 36 |
-
print("Simulating human-like download...")
|
| 37 |
-
|
| 38 |
-
# Randomized human-like delay
|
| 39 |
-
delay = random.uniform(1, 4)
|
| 40 |
-
print(f"Artificial delay: {delay:.2f}s")
|
| 41 |
-
time.sleep(delay)
|
| 42 |
-
|
| 43 |
ydl_opts = {
|
| 44 |
-
'format': 'bestvideo
|
| 45 |
'outtmpl': '%(title)s.%(ext)s',
|
| 46 |
-
'
|
| 47 |
'quiet': False,
|
| 48 |
-
|
| 49 |
-
# Modern browser headers
|
| 50 |
-
'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',
|
| 51 |
-
'http_headers': {
|
| 52 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 53 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
| 54 |
-
'Sec-Fetch-Mode': 'navigate',
|
| 55 |
-
},
|
| 56 |
-
|
| 57 |
-
# Cookie authentication if provided
|
| 58 |
-
'cookiefile': cookies_file if cookies_file else None,
|
| 59 |
-
|
| 60 |
-
# Retry and fragment settings for robustness
|
| 61 |
-
'retries': 3,
|
| 62 |
-
'fragment_retries': 5,
|
| 63 |
}
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
info = ydl.extract_info(url, download=True)
|
| 68 |
-
filename = ydl.prepare_filename(info)
|
| 69 |
-
return filename
|
| 70 |
-
except Exception as e:
|
| 71 |
-
print(f"Error: {str(e)}")
|
| 72 |
-
return f"Download failed: {str(e)}"
|
| 73 |
|
| 74 |
-
# Example
|
| 75 |
url = "https://www.youtube.com/watch?v=uLVRZE8OAI4"
|
| 76 |
download_video(url)
|
| 77 |
-
|
| 78 |
|
| 79 |
|
| 80 |
|
|
|
|
| 29 |
import time
|
| 30 |
import random
|
| 31 |
|
| 32 |
+
import yt_dlp
|
| 33 |
+
|
| 34 |
+
def download_video(url):
|
| 35 |
"""
|
| 36 |
+
Download a video from YouTube using yt-dlp.
|
| 37 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
ydl_opts = {
|
| 39 |
+
'format': 'bestvideo+bestaudio/best',
|
| 40 |
'outtmpl': '%(title)s.%(ext)s',
|
| 41 |
+
'merge_output_format': 'mp4',
|
| 42 |
'quiet': False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 46 |
+
ydl.download([url])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Example Usage
|
| 49 |
url = "https://www.youtube.com/watch?v=uLVRZE8OAI4"
|
| 50 |
download_video(url)
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
|