prasanth.thangavel
commited on
Commit
·
9312825
1
Parent(s):
d4eee90
Add app.py first commit
Browse files
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from yt_dlp import YoutubeDL
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
import shutil
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
def download_for_browser(url, mode='audio', quality='high'):
|
9 |
+
if not url:
|
10 |
+
return None, "Please enter a valid URL"
|
11 |
+
|
12 |
+
# Create temporary directory that persists until file is served
|
13 |
+
temp_dir = Path(tempfile.mkdtemp())
|
14 |
+
|
15 |
+
try:
|
16 |
+
# Configure download options
|
17 |
+
opts = {
|
18 |
+
'format': 'bestaudio/best' if mode == 'audio' else 'bestvideo+bestaudio/best',
|
19 |
+
'outtmpl': str(temp_dir / '%(title)s.%(ext)s'),
|
20 |
+
'restrictfilenames': True,
|
21 |
+
'windowsfilenames': True,
|
22 |
+
'quiet': True,
|
23 |
+
'no_warnings': True
|
24 |
+
}
|
25 |
+
|
26 |
+
# Add format-specific options
|
27 |
+
if mode == 'audio':
|
28 |
+
opts.update({
|
29 |
+
'postprocessors': [{
|
30 |
+
'key': 'FFmpegExtractAudio',
|
31 |
+
'preferredcodec': 'mp3',
|
32 |
+
'preferredquality': '320' if quality == 'high' else '192',
|
33 |
+
}],
|
34 |
+
'prefer_ffmpeg': True,
|
35 |
+
'keepvideo': False
|
36 |
+
})
|
37 |
+
else:
|
38 |
+
opts.update({
|
39 |
+
'format': 'bestvideo+bestaudio/best' if quality == 'high' else 'best[height<=720]',
|
40 |
+
'merge_output_format': 'mp4'
|
41 |
+
})
|
42 |
+
|
43 |
+
# Download file
|
44 |
+
with YoutubeDL(opts) as ydl:
|
45 |
+
info = ydl.extract_info(url, download=True)
|
46 |
+
|
47 |
+
# Find downloaded file
|
48 |
+
files = list(temp_dir.glob('*'))
|
49 |
+
if not files:
|
50 |
+
return None, "Download failed - no files found"
|
51 |
+
|
52 |
+
download_file = files[0]
|
53 |
+
if not download_file.exists():
|
54 |
+
return None, f"File not found after download: {download_file.name}"
|
55 |
+
|
56 |
+
# Return file while it exists in temp directory
|
57 |
+
return str(download_file), f"Successfully converted: {download_file.name}"
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
if temp_dir.exists():
|
61 |
+
shutil.rmtree(temp_dir)
|
62 |
+
return None, f"Error during download: {str(e)}"
|
63 |
+
|
64 |
+
def create_browser_ui():
|
65 |
+
with gr.Blocks(title="YouTube Downloader", theme=gr.themes.Soft()) as demo:
|
66 |
+
gr.Markdown("## YouTube Downloader")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
url_input = gr.Textbox(
|
70 |
+
label="YouTube URL",
|
71 |
+
placeholder="https://youtube.com/watch?v=...",
|
72 |
+
scale=2
|
73 |
+
)
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column(scale=1):
|
77 |
+
mode_input = gr.Radio(
|
78 |
+
choices=["audio", "video"],
|
79 |
+
value="audio",
|
80 |
+
label="Format"
|
81 |
+
)
|
82 |
+
quality_input = gr.Radio(
|
83 |
+
choices=["high", "medium"],
|
84 |
+
value="high",
|
85 |
+
label="Quality"
|
86 |
+
)
|
87 |
+
|
88 |
+
download_button = gr.Button("Convert", variant="primary")
|
89 |
+
|
90 |
+
with gr.Column(scale=2):
|
91 |
+
status_text = gr.Textbox(label="Converting Status", interactive=False)
|
92 |
+
output_file = gr.File(label="Download to your device ...")
|
93 |
+
|
94 |
+
download_button.click(
|
95 |
+
fn=download_for_browser,
|
96 |
+
inputs=[url_input, mode_input, quality_input],
|
97 |
+
outputs=[output_file, status_text]
|
98 |
+
)
|
99 |
+
|
100 |
+
return demo
|
101 |
+
|
102 |
+
demo = create_browser_ui()
|
103 |
+
demo.launch(
|
104 |
+
share=False,
|
105 |
+
debug=True,
|
106 |
+
show_error=True
|
107 |
+
)
|