|
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() |