File size: 2,518 Bytes
bc39353
5961a7a
13158d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc39353
4ddbe7a
13158d2
 
 
 
 
 
 
 
99baa8d
9a13e4d
13158d2
9a13e4d
 
 
13158d2
 
 
 
 
 
9a13e4d
13158d2
 
 
 
 
 
 
 
 
 
bc39353
2e18f1a
99baa8d
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
import gradio as gr
import yt_dlp
import os
import tempfile
import base64

def download_and_merge(youtube_url):
    with tempfile.TemporaryDirectory() as temp_dir:
        ydl_opts = {
            'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
            'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'),
            'merge_output_format': 'mp4',
        }
        try:
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(youtube_url, download=True)
                filename = ydl.prepare_filename(info)
                final_filename = os.path.splitext(filename)[0] + '.mp4'
                
                if os.path.exists(final_filename):
                    with open(final_filename, 'rb') as f:
                        video_data = f.read()
                    b64 = base64.b64encode(video_data).decode()
                    return info['title'], b64
                else:
                    return info['title'], None
        except Exception as e:
            return str(e), None

def extract_info(youtube_url):
    title, video_data = download_and_merge(youtube_url)
    if video_data:
        download_link = f'data:video/mp4;base64,{video_data}'
        return (f"제목: {title}\n\n다운로드가 준비되었습니다. '다운로드' 버튼을 클릭하세요.", 
                download_link, 
                gr.update(visible=True))
    else:
        return f"오류 발생: {title}", None, gr.update(visible=False)

with gr.Blocks() as demo:
    gr.Markdown("## YouTube 다운로더")
    gr.Markdown("주의: 이 도구를 사용하여 저작권이 있는 콘텐츠를 무단으로 다운로드하는 것은 불법입니다.")
    
    youtube_url_input = gr.Textbox(label="YouTube URL 입력")
    extract_button = gr.Button("다운로드 준비")
    output = gr.Textbox(label="상태", lines=5)
    download_button = gr.Button("다운로드", visible=False)
    
    def on_download_click(url):
        return f'<script>var link = document.createElement("a"); link.href = "{url}"; link.download = "video.mp4"; document.body.appendChild(link); link.click(); document.body.removeChild(link);</script>'
    
    extract_button.click(
        fn=extract_info, 
        inputs=youtube_url_input, 
        outputs=[output, download_button, download_button]
    )
    download_button.click(
        fn=on_download_click,
        inputs=download_button,
        outputs=gr.HTML()
    )

if __name__ == "__main__":
    demo.launch()