|
import gradio as gr |
|
import yt_dlp |
|
|
|
def extract_info(youtube_url): |
|
ydl_opts = { |
|
'quiet': True, |
|
'no_warnings': True, |
|
'no_color': True, |
|
} |
|
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'), |
|
} |
|
|
|
formats = info.get('formats', []) |
|
|
|
|
|
best_video = max((f for f in formats if f['vcodec'] != 'none' and f['acodec'] == 'none'), |
|
key=lambda x: x.get('height', 0), default=None) |
|
|
|
|
|
best_audio = max((f for f in formats if f['acodec'] != 'none' and f['vcodec'] == 'none'), |
|
key=lambda x: x.get('abr', 0), default=None) |
|
|
|
|
|
best_combined = max((f for f in formats if f['vcodec'] != 'none' and f['acodec'] != 'none'), |
|
key=lambda x: x.get('height', 0), default=None) |
|
|
|
metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()]) |
|
|
|
if best_video and best_audio: |
|
return (metadata_str, |
|
best_video['url'], best_audio['url'], best_combined['url'] if best_combined else None, |
|
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True if best_combined else False)) |
|
else: |
|
return ("적절한 형식을 찾을 수 없습니다.", None, None, None, |
|
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)) |
|
except Exception as e: |
|
return (f"오류 발생: {str(e)}", None, None, None, |
|
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)) |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## YouTube 메타데이터 및 다운로드 URL 추출기") |
|
gr.Markdown("주의: 이 도구를 사용하여 저작권이 있는 콘텐츠를 무단으로 다운로드하는 것은 불법입니다.") |
|
|
|
youtube_url_input = gr.Textbox(label="YouTube URL 입력") |
|
extract_button = gr.Button("정보 추출") |
|
output = gr.Textbox(label="추출된 정보", lines=10) |
|
video_button = gr.Button("비디오 다운로드 (오디오 없음)", visible=False) |
|
audio_button = gr.Button("오디오 다운로드", visible=False) |
|
combined_button = gr.Button("비디오+오디오 다운로드", visible=False) |
|
|
|
def on_download_click(url): |
|
return f'<script>window.location.href = "{url}";</script>' |
|
|
|
extract_button.click( |
|
fn=extract_info, |
|
inputs=youtube_url_input, |
|
outputs=[output, video_button, audio_button, combined_button, video_button, audio_button, combined_button] |
|
) |
|
video_button.click(fn=on_download_click, inputs=video_button, outputs=gr.HTML()) |
|
audio_button.click(fn=on_download_click, inputs=audio_button, outputs=gr.HTML()) |
|
combined_button.click(fn=on_download_click, inputs=combined_button, outputs=gr.HTML()) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |