File size: 3,229 Bytes
1cd4ebf
 
 
464c564
1cd4ebf
b41072e
1cd4ebf
 
464c564
 
1cd4ebf
 
d182886
 
 
 
 
b41072e
1cd4ebf
 
 
 
464c564
1cd4ebf
 
 
 
 
 
464c564
1cd4ebf
 
 
 
 
 
 
 
 
 
 
 
 
464c564
d182886
 
464c564
 
 
 
 
 
 
 
 
 
 
 
1cd4ebf
 
464c564
1cd4ebf
 
 
 
 
fbfe5bf
 
 
 
1cd4ebf
464c564
 
 
 
d182886
464c564
1cd4ebf
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import yt_dlp
import os
import zipfile

def download_video(url, quality, format_choice, download_playlist, cookies_file):
    try:
        ydl_opts = {
            'outtmpl': 'downloads/%(title)s.%(ext)s',
            'noplaylist': not download_playlist,
        }
        
        if cookies_file is not None:
            cookie_path = "temp_cookies.txt"
            with open(cookie_path, "wb") as f:
                f.write(cookies_file)
            ydl_opts['cookiefile'] = cookie_path
        
        if quality == "Best":
            ydl_opts['format'] = 'bestvideo+bestaudio/best'
        elif quality == "Medium":
            ydl_opts['format'] = 'bestvideo[height<=720]+bestaudio/best[height<=720]'
        else:
            ydl_opts['format'] = 'bestvideo[height<=480]+bestaudio/best[height<=480]'
        
        if format_choice == "MP4":
            ydl_opts['merge_output_format'] = 'mp4'
        elif format_choice == "MKV":
            ydl_opts['merge_output_format'] = 'mkv'
        else:
            ydl_opts['format'] = 'bestaudio'
            ydl_opts['postprocessors'] = [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }]
        
        if not os.path.exists('downloads'):
            os.makedirs('downloads')
            
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=True)
            filename = ydl.prepare_filename(info)
        
        if cookies_file is not None and os.path.exists(cookie_path):
            os.remove(cookie_path)
        
        # Nếu là playlist, nén toàn bộ thư mục downloads thành ZIP
        if download_playlist and os.path.isdir('downloads'):
            zip_path = "downloads/playlist.zip"
            with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
                for root, _, files in os.walk('downloads'):
                    for file in files:
                        if file != "playlist.zip":  # Tránh nén chính file ZIP
                            zipf.write(os.path.join(root, file), file)
            return "Playlist đã tải thành công dưới dạng ZIP", zip_path
        
        return f"Video đã tải thành công: {os.path.basename(filename)}", filename
    
    except Exception as e:
        return f"Lỗi: {str(e)}", None

interface = gr.Interface(
    fn=download_video,
    inputs=[
        gr.Textbox(label="Dán URL video vào đây", placeholder="https://..."),
        gr.Dropdown(choices=["Best", "Medium", "Low"], label="Chất lượng video", value="Best"),
        gr.Dropdown(choices=["MP4", "MKV", "Audio Only (MP3)"], label="Định dạng đầu ra", value="MP4"),
        gr.Checkbox(label="Tải toàn bộ playlist (nếu có)", value=False),
        gr.File(label="Upload file cookie (nếu cần)", type="binary")
    ],
    outputs=[
        gr.Textbox(label="Kết quả"),
        gr.File(label="Tải file về máy")
    ],
    title="Trình tải video trên Hugging Face Spaces",
    description="Dán URL video và upload file cookie nếu cần. Tải file hoặc playlist (ZIP) về máy."
)

interface.launch()