Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
def download_video(url, quality, format_choice, download_playlist, cookies_file):
|
| 6 |
try:
|
| 7 |
-
# Tùy chọn cấu hình cho yt-dlp
|
| 8 |
ydl_opts = {
|
| 9 |
-
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
| 10 |
-
'noplaylist': not download_playlist,
|
| 11 |
}
|
| 12 |
|
| 13 |
-
# Sử dụng file cookie nếu được upload
|
| 14 |
if cookies_file is not None:
|
| 15 |
cookie_path = "temp_cookies.txt"
|
| 16 |
with open(cookie_path, "wb") as f:
|
| 17 |
f.write(cookies_file)
|
| 18 |
ydl_opts['cookiefile'] = cookie_path
|
| 19 |
|
| 20 |
-
# Điều chỉnh chất lượng video
|
| 21 |
if quality == "Best":
|
| 22 |
ydl_opts['format'] = 'bestvideo+bestaudio/best'
|
| 23 |
elif quality == "Medium":
|
| 24 |
ydl_opts['format'] = 'bestvideo[height<=720]+bestaudio/best[height<=720]'
|
| 25 |
-
else:
|
| 26 |
ydl_opts['format'] = 'bestvideo[height<=480]+bestaudio/best[height<=480]'
|
| 27 |
|
| 28 |
-
# Điều chỉnh định dạng đầu ra
|
| 29 |
if format_choice == "MP4":
|
| 30 |
ydl_opts['merge_output_format'] = 'mp4'
|
| 31 |
elif format_choice == "MKV":
|
| 32 |
ydl_opts['merge_output_format'] = 'mkv'
|
| 33 |
-
else:
|
| 34 |
ydl_opts['format'] = 'bestaudio'
|
| 35 |
ydl_opts['postprocessors'] = [{
|
| 36 |
'key': 'FFmpegExtractAudio',
|
|
@@ -38,25 +35,31 @@ def download_video(url, quality, format_choice, download_playlist, cookies_file)
|
|
| 38 |
'preferredquality': '192',
|
| 39 |
}]
|
| 40 |
|
| 41 |
-
# Tạo thư mục downloads nếu chưa tồn tại
|
| 42 |
if not os.path.exists('downloads'):
|
| 43 |
os.makedirs('downloads')
|
| 44 |
|
| 45 |
-
# Tải video
|
| 46 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 47 |
info = ydl.extract_info(url, download=True)
|
| 48 |
filename = ydl.prepare_filename(info)
|
| 49 |
-
|
| 50 |
-
# Xóa file cookie tạm thời
|
| 51 |
if cookies_file is not None and os.path.exists(cookie_path):
|
| 52 |
os.remove(cookie_path)
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
-
return f"Lỗi: {str(e)}"
|
| 58 |
|
| 59 |
-
# Tạo giao diện Gradio
|
| 60 |
interface = gr.Interface(
|
| 61 |
fn=download_video,
|
| 62 |
inputs=[
|
|
@@ -66,10 +69,12 @@ interface = gr.Interface(
|
|
| 66 |
gr.Checkbox(label="Tải toàn bộ playlist (nếu có)", value=False),
|
| 67 |
gr.File(label="Upload file cookie (nếu cần)", type="binary")
|
| 68 |
],
|
| 69 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 70 |
title="Trình tải video trên Hugging Face Spaces",
|
| 71 |
-
description="Dán URL video và upload file cookie nếu cần
|
| 72 |
)
|
| 73 |
|
| 74 |
-
# Khởi chạy ứng dụng
|
| 75 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
+
import zipfile
|
| 5 |
|
| 6 |
def download_video(url, quality, format_choice, download_playlist, cookies_file):
|
| 7 |
try:
|
|
|
|
| 8 |
ydl_opts = {
|
| 9 |
+
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
| 10 |
+
'noplaylist': not download_playlist,
|
| 11 |
}
|
| 12 |
|
|
|
|
| 13 |
if cookies_file is not None:
|
| 14 |
cookie_path = "temp_cookies.txt"
|
| 15 |
with open(cookie_path, "wb") as f:
|
| 16 |
f.write(cookies_file)
|
| 17 |
ydl_opts['cookiefile'] = cookie_path
|
| 18 |
|
|
|
|
| 19 |
if quality == "Best":
|
| 20 |
ydl_opts['format'] = 'bestvideo+bestaudio/best'
|
| 21 |
elif quality == "Medium":
|
| 22 |
ydl_opts['format'] = 'bestvideo[height<=720]+bestaudio/best[height<=720]'
|
| 23 |
+
else:
|
| 24 |
ydl_opts['format'] = 'bestvideo[height<=480]+bestaudio/best[height<=480]'
|
| 25 |
|
|
|
|
| 26 |
if format_choice == "MP4":
|
| 27 |
ydl_opts['merge_output_format'] = 'mp4'
|
| 28 |
elif format_choice == "MKV":
|
| 29 |
ydl_opts['merge_output_format'] = 'mkv'
|
| 30 |
+
else:
|
| 31 |
ydl_opts['format'] = 'bestaudio'
|
| 32 |
ydl_opts['postprocessors'] = [{
|
| 33 |
'key': 'FFmpegExtractAudio',
|
|
|
|
| 35 |
'preferredquality': '192',
|
| 36 |
}]
|
| 37 |
|
|
|
|
| 38 |
if not os.path.exists('downloads'):
|
| 39 |
os.makedirs('downloads')
|
| 40 |
|
|
|
|
| 41 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 42 |
info = ydl.extract_info(url, download=True)
|
| 43 |
filename = ydl.prepare_filename(info)
|
| 44 |
+
|
|
|
|
| 45 |
if cookies_file is not None and os.path.exists(cookie_path):
|
| 46 |
os.remove(cookie_path)
|
| 47 |
+
|
| 48 |
+
# Nếu là playlist, nén toàn bộ thư mục downloads thành ZIP
|
| 49 |
+
if download_playlist and os.path.isdir('downloads'):
|
| 50 |
+
zip_path = "downloads/playlist.zip"
|
| 51 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 52 |
+
for root, _, files in os.walk('downloads'):
|
| 53 |
+
for file in files:
|
| 54 |
+
if file != "playlist.zip": # Tránh nén chính file ZIP
|
| 55 |
+
zipf.write(os.path.join(root, file), file)
|
| 56 |
+
return "Playlist đã tải thành công dưới dạng ZIP", zip_path
|
| 57 |
+
|
| 58 |
+
return f"Video đã tải thành công: {os.path.basename(filename)}", filename
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
+
return f"Lỗi: {str(e)}", None
|
| 62 |
|
|
|
|
| 63 |
interface = gr.Interface(
|
| 64 |
fn=download_video,
|
| 65 |
inputs=[
|
|
|
|
| 69 |
gr.Checkbox(label="Tải toàn bộ playlist (nếu có)", value=False),
|
| 70 |
gr.File(label="Upload file cookie (nếu cần)", type="binary")
|
| 71 |
],
|
| 72 |
+
outputs=[
|
| 73 |
+
gr.Textbox(label="Kết quả"),
|
| 74 |
+
gr.File(label="Tải file về máy")
|
| 75 |
+
],
|
| 76 |
title="Trình tải video trên Hugging Face Spaces",
|
| 77 |
+
description="Dán URL video và upload file cookie nếu cần. Tải file hoặc playlist (ZIP) về máy."
|
| 78 |
)
|
| 79 |
|
|
|
|
| 80 |
interface.launch()
|