Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import yt_dlp
|
3 |
+
import os
|
4 |
+
|
5 |
+
def download_video(url, quality, format_choice, download_playlist):
|
6 |
+
try:
|
7 |
+
# Tùy chọn cấu hình cho yt-dlp
|
8 |
+
ydl_opts = {
|
9 |
+
'outtmpl': 'downloads/%(title)s.%(ext)s', # Đường dẫn lưu file
|
10 |
+
'noplaylist': not download_playlist, # Tải playlist nếu được chọn
|
11 |
+
}
|
12 |
+
|
13 |
+
# Điều chỉnh chất lượng video
|
14 |
+
if quality == "Best":
|
15 |
+
ydl_opts['format'] = 'bestvideo+bestaudio/best'
|
16 |
+
elif quality == "Medium":
|
17 |
+
ydl_opts['format'] = 'bestvideo[height<=720]+bestaudio/best[height<=720]'
|
18 |
+
else: # Low
|
19 |
+
ydl_opts['format'] = 'bestvideo[height<=480]+bestaudio/best[height<=480]'
|
20 |
+
|
21 |
+
# Điều chỉnh định dạng đầu ra
|
22 |
+
if format_choice == "MP4":
|
23 |
+
ydl_opts['merge_output_format'] = 'mp4'
|
24 |
+
elif format_choice == "MKV":
|
25 |
+
ydl_opts['merge_output_format'] = 'mkv'
|
26 |
+
else: # Audio Only (MP3)
|
27 |
+
ydl_opts['format'] = 'bestaudio'
|
28 |
+
ydl_opts['postprocessors'] = [{
|
29 |
+
'key': 'FFmpegExtractAudio',
|
30 |
+
'preferredcodec': 'mp3',
|
31 |
+
'preferredquality': '192',
|
32 |
+
}]
|
33 |
+
|
34 |
+
# Tạo thư mục downloads nếu chưa tồn tại
|
35 |
+
if not os.path.exists('downloads'):
|
36 |
+
os.makedirs('downloads')
|
37 |
+
|
38 |
+
# Tải video
|
39 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
40 |
+
info = ydl.extract_info(url, download=True)
|
41 |
+
filename = ydl.prepare_filename(info)
|
42 |
+
|
43 |
+
return f"Video đã tải thành công: {filename}"
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
return f"Lỗi: {str(e)}"
|
47 |
+
|
48 |
+
# Tạo giao diện Gradio
|
49 |
+
interface = gr.Interface(
|
50 |
+
fn=download_video,
|
51 |
+
inputs=[
|
52 |
+
gr.Textbox(label="Dán URL video vào đây", placeholder="https://..."),
|
53 |
+
gr.Dropdown(
|
54 |
+
choices=["Best", "Medium", "Low"],
|
55 |
+
label="Chất lượng video",
|
56 |
+
value="Best",
|
57 |
+
info="Best: Cao nhất có thể, Medium: 720p, Low: 480p"
|
58 |
+
),
|
59 |
+
gr.Dropdown(
|
60 |
+
choices=["MP4", "MKV", "Audio Only (MP3)"],
|
61 |
+
label="Định dạng đầu ra",
|
62 |
+
value="MP4",
|
63 |
+
info="Chọn định dạng file xuất ra"
|
64 |
+
),
|
65 |
+
gr.Checkbox(
|
66 |
+
label="Tải toàn bộ playlist (nếu có)",
|
67 |
+
value=False,
|
68 |
+
info="Check để tải toàn bộ playlist thay vì chỉ một video"
|
69 |
+
)
|
70 |
+
],
|
71 |
+
outputs=gr.Textbox(label="Kết quả"),
|
72 |
+
title="Trình tải video nâng cao",
|
73 |
+
description="Dán URL video từ bất kỳ trang nào hỗ trợ (YouTube, Facebook, v.v.) và tùy chỉnh cách tải."
|
74 |
+
)
|
75 |
+
|
76 |
+
# Khởi chạy ứng dụng
|
77 |
+
interface.launch()
|