File size: 2,504 Bytes
bc39353
5961a7a
bc39353
4ddbe7a
d596036
 
 
 
558b354
 
 
d596036
 
 
 
 
 
 
 
 
 
 
 
 
558b354
d596036
558b354
d596036
558b354
 
 
 
d596036
558b354
d596036
558b354
 
 
 
99baa8d
9a13e4d
558b354
9a13e4d
 
 
d596036
 
558b354
9a13e4d
13158d2
 
 
558b354
 
 
 
 
 
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
import gradio as gr
import yt_dlp

def extract_info(youtube_url):
    ydl_opts = {
        'quiet': True,
        'no_warnings': True,
        'no_color': True,
        'youtube_include_dash_manifest': False,
        'youtube_include_hls_manifest': False,
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    }
    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(youtube_url, download=False)
            
            metadata = {
                "제목": info.get('title', 'N/A'),
                "채널": info.get('channel', 'N/A'),
                "업로드 날짜": info.get('upload_date', 'N/A'),
                "조회수": info.get('view_count', 'N/A'),
                "길이 (초)": info.get('duration', 'N/A'),
            }
            
            # 사용 가능한 모든 포맷 중 가장 높은 품질의 mp4 찾기
            formats = info.get('formats', [])
            best_mp4 = max((f for f in formats if f['ext'] == 'mp4'), key=lambda x: x.get('quality', 0), default=None)
            
            if best_mp4:
                download_url = best_mp4['url']
                metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
                return metadata_str, download_url, gr.update(visible=True)
            else:
                return "적절한 형식을 찾을 수 없습니다.", None, gr.update(visible=False)
    except Exception as e:
        return f"오류 발생: {str(e)}", None, gr.update(visible=False)

def on_download_click(url):
    return f'<script>window.location.href = "{url}";</script>'

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=10)
    download_button = gr.Button("다운로드", visible=False)
    
    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()