Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
-
import tempfile
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
|
| 7 |
def get_formats(url):
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
ydl_opts = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 11 |
info = ydl.extract_info(url, download=False)
|
| 12 |
|
|
@@ -14,20 +22,29 @@ def get_formats(url):
|
|
| 14 |
for f in info.get("formats", []):
|
| 15 |
format_id = f.get("format_id")
|
| 16 |
ext = f.get("ext", "unknown")
|
| 17 |
-
resolution =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if format_id:
|
| 19 |
formats.append((f"{format_id} - {ext} - {resolution}", format_id))
|
| 20 |
|
| 21 |
return gr.update(choices=formats, value=formats[0][1] if formats else None)
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
def download_video(url, format_id):
|
| 27 |
-
if not url
|
| 28 |
-
raise gr.Error("Vui lòng nhập URL
|
|
|
|
|
|
|
| 29 |
|
| 30 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
| 31 |
ydl_opts = {
|
| 32 |
"format": format_id,
|
| 33 |
"outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"),
|
|
@@ -35,27 +52,33 @@ def download_video(url, format_id):
|
|
| 35 |
"noplaylist": True,
|
| 36 |
}
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
gr.Markdown("## Audio/Video Downloader with Preview")
|
| 47 |
-
gr.Markdown(
|
| 48 |
-
"Chỉ tải nội dung khi bạn có quyền hợp pháp để tải và sử dụng nội dung đó."
|
| 49 |
-
)
|
| 50 |
|
| 51 |
url_input = gr.Textbox(label="Video URL")
|
| 52 |
format_dropdown = gr.Dropdown(label="Select download format", choices=[])
|
|
|
|
| 53 |
download_btn = gr.Button("Download")
|
| 54 |
|
| 55 |
output_file = gr.File(label="Download File")
|
| 56 |
output_video = gr.Video(label="Preview Media")
|
| 57 |
|
| 58 |
-
|
| 59 |
fn=get_formats,
|
| 60 |
inputs=url_input,
|
| 61 |
outputs=format_dropdown
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
import gradio as gr
|
| 4 |
import yt_dlp
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def get_formats(url):
|
| 8 |
+
if not url or not url.strip():
|
| 9 |
+
return gr.update(choices=[], value=None)
|
| 10 |
+
|
| 11 |
try:
|
| 12 |
+
ydl_opts = {
|
| 13 |
+
"quiet": True,
|
| 14 |
+
"noplaylist": True,
|
| 15 |
+
"skip_download": True,
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 19 |
info = ydl.extract_info(url, download=False)
|
| 20 |
|
|
|
|
| 22 |
for f in info.get("formats", []):
|
| 23 |
format_id = f.get("format_id")
|
| 24 |
ext = f.get("ext", "unknown")
|
| 25 |
+
resolution = (
|
| 26 |
+
f.get("resolution")
|
| 27 |
+
or f.get("format_note")
|
| 28 |
+
or ("audio" if f.get("vcodec") == "none" else "unknown")
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
if format_id:
|
| 32 |
formats.append((f"{format_id} - {ext} - {resolution}", format_id))
|
| 33 |
|
| 34 |
return gr.update(choices=formats, value=formats[0][1] if formats else None)
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
raise gr.Error(f"Không lấy được format: {str(e)}")
|
| 38 |
|
| 39 |
|
| 40 |
def download_video(url, format_id):
|
| 41 |
+
if not url:
|
| 42 |
+
raise gr.Error("Vui lòng nhập URL.")
|
| 43 |
+
if not format_id:
|
| 44 |
+
raise gr.Error("Vui lòng chọn format.")
|
| 45 |
|
| 46 |
temp_dir = tempfile.mkdtemp()
|
| 47 |
+
|
| 48 |
ydl_opts = {
|
| 49 |
"format": format_id,
|
| 50 |
"outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"),
|
|
|
|
| 52 |
"noplaylist": True,
|
| 53 |
}
|
| 54 |
|
| 55 |
+
try:
|
| 56 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 57 |
+
info = ydl.extract_info(url, download=True)
|
| 58 |
+
filename = ydl.prepare_filename(info)
|
| 59 |
+
|
| 60 |
+
if not os.path.exists(filename):
|
| 61 |
+
raise gr.Error("Không tìm thấy file sau khi tải.")
|
| 62 |
|
| 63 |
+
return filename, filename
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
raise gr.Error(f"Tải thất bại: {str(e)}")
|
| 67 |
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
gr.Markdown("## Audio/Video Downloader with Preview")
|
| 71 |
+
gr.Markdown("Chỉ dùng với nội dung bạn có quyền tải và sử dụng.")
|
|
|
|
|
|
|
| 72 |
|
| 73 |
url_input = gr.Textbox(label="Video URL")
|
| 74 |
format_dropdown = gr.Dropdown(label="Select download format", choices=[])
|
| 75 |
+
load_btn = gr.Button("Load Formats")
|
| 76 |
download_btn = gr.Button("Download")
|
| 77 |
|
| 78 |
output_file = gr.File(label="Download File")
|
| 79 |
output_video = gr.Video(label="Preview Media")
|
| 80 |
|
| 81 |
+
load_btn.click(
|
| 82 |
fn=get_formats,
|
| 83 |
inputs=url_input,
|
| 84 |
outputs=format_dropdown
|