Spaces:
Runtime error
Runtime error
lakshminarayana commited on
Commit ·
25ab9a8
1
Parent(s): 977b57d
added cookies
Browse files
app.py
CHANGED
|
@@ -1,54 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
|
| 7 |
-
# Determine the default download directory based on the user's OS
|
| 8 |
-
def get_default_download_path():
|
| 9 |
-
if platform.system() == 'Windows':
|
| 10 |
-
return str(Path.home() / "Downloads") # Windows default Downloads folder
|
| 11 |
-
elif platform.system() == 'Darwin':
|
| 12 |
-
return str(Path.home() / "Downloads") # macOS default Downloads folder
|
| 13 |
-
else:
|
| 14 |
-
return str(Path.home() / "Downloads") # Linux default Downloads folder
|
| 15 |
-
|
| 16 |
-
# Define the progress hook function that updates the progress bar
|
| 17 |
-
def progress_hook(d, progress):
|
| 18 |
-
if d['status'] == 'downloading':
|
| 19 |
-
p = d['downloaded_bytes'] / d['total_bytes'] * 100
|
| 20 |
-
progress.update(p) # Update Gradio progress bar with percentage
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def download_video(url, progress=gr.Progress()):
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=download_video,
|
| 47 |
-
inputs=
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
title="YouTube Video Downloader",
|
| 50 |
-
description="Enter
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# Launch the
|
| 54 |
-
iface.
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def download_video(url, cookies, progress=gr.Progress()):
|
|
|
|
| 7 |
try:
|
| 8 |
+
# Get the path to the cookies file if provided
|
| 9 |
+
cookies_path = cookies.get('name') if cookies else None
|
| 10 |
+
|
| 11 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 12 |
+
# Configure yt-dlp options
|
| 13 |
+
ydl_opts = {
|
| 14 |
+
'format': 'bestvideo+bestaudio/best',
|
| 15 |
+
'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'),
|
| 16 |
+
'merge_output_format': 'mp4',
|
| 17 |
+
'progress_hooks': [lambda d: progress_hook(d, progress)],
|
| 18 |
+
'cookies': cookies_path,
|
| 19 |
+
'verbose': True,
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# Download video using yt-dlp
|
| 23 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 24 |
+
info_dict = ydl.extract_info(url, download=True)
|
| 25 |
+
|
| 26 |
+
# Read downloaded video file into memory
|
| 27 |
+
video_file = info_dict.get('filename')
|
| 28 |
+
if not video_file:
|
| 29 |
+
return "Download Failed: Could not retrieve video file"
|
| 30 |
|
| 31 |
+
with open(video_file, 'rb') as f:
|
| 32 |
+
video_bytes = f.read()
|
| 33 |
+
|
| 34 |
+
# Return the video bytes for download
|
| 35 |
+
return {'name': os.path.basename(video_file), 'data': video_bytes}
|
| 36 |
+
|
| 37 |
+
except yt_dlp.utils.DownloadError as e:
|
| 38 |
+
return f"Download Failed: {str(e)}"
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"Unexpected Error: {str(e)}"
|
| 41 |
+
|
| 42 |
+
def progress_hook(d, progress):
|
| 43 |
+
if d['status'] == 'downloading':
|
| 44 |
+
p = (d['downloaded_bytes'] / d['total_bytes']) * 100 if d['total_bytes'] else 0
|
| 45 |
+
progress.update(p, desc=f"Downloading ({d['speed'] or '...'} at {d['eta'] or '...'})")
|
| 46 |
|
| 47 |
+
# Gradio interface configuration
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=download_video,
|
| 50 |
+
inputs=[
|
| 51 |
+
gr.Textbox(label="YouTube Video URL"),
|
| 52 |
+
gr.File(label="Upload your cookies.txt file (optional)", file_types=[".txt"], optional=True),
|
| 53 |
+
],
|
| 54 |
+
outputs=gr.File(label="Downloaded Video", type="binary", file_count="single"),
|
| 55 |
title="YouTube Video Downloader",
|
| 56 |
+
description="Enter a YouTube video URL and upload your cookies.txt (if required) to download the video.",
|
| 57 |
+
examples=[
|
| 58 |
+
["https://www.youtube.com/watch?v=dQw4w9WgXcQ", None],
|
| 59 |
+
],
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Launch the Gradio interface
|
| 63 |
+
iface.queue()
|
| 64 |
+
iface.launch(share=True)
|