JUNGU commited on
Commit
13158d2
·
verified ·
1 Parent(s): 9a13e4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -36
app.py CHANGED
@@ -1,49 +1,64 @@
1
  import gradio as gr
2
  import yt_dlp
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  def extract_info(youtube_url):
5
- ydl_opts = {
6
- 'quiet': True,
7
- 'no_warnings': True,
8
- 'no_color': True,
9
- 'youtube_include_dash_manifest': False,
10
- 'youtube_include_hls_manifest': False,
11
- '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',
12
- }
13
- try:
14
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
15
- info = ydl.extract_info(youtube_url, download=False)
16
-
17
- metadata = {
18
- "제목": info.get('title', 'N/A'),
19
- "채널": info.get('channel', 'N/A'),
20
- "업로드 날짜": info.get('upload_date', 'N/A'),
21
- "조회수": info.get('view_count', 'N/A'),
22
- "길이 (초)": info.get('duration', 'N/A'),
23
- }
24
-
25
- # 사용 가능한 모든 포맷 중 가장 높은 품질의 mp4 찾기
26
- formats = info.get('formats', [])
27
- best_mp4 = max((f for f in formats if f['ext'] == 'mp4'), key=lambda x: x.get('quality', 0), default=None)
28
-
29
- if best_mp4:
30
- metadata["다운로드 URL"] = best_mp4['url']
31
- else:
32
- metadata["다운로드 URL"] = "적절한 형식을 찾을 수 없습니다."
33
-
34
- return "\n".join([f"{k}: {v}" for k, v in metadata.items()])
35
- except Exception as e:
36
- return f"오류 발생: {str(e)}"
37
 
38
  with gr.Blocks() as demo:
39
- gr.Markdown("## YouTube 메타데이터 및 다운로드 링크 추출기")
40
  gr.Markdown("주의: 이 도구를 사용하여 저작권이 있는 콘텐츠를 무단으로 다운로드하는 것은 불법입니다.")
41
 
42
  youtube_url_input = gr.Textbox(label="YouTube URL 입력")
43
- extract_button = gr.Button("정보 추출")
44
- output = gr.Textbox(label="추출된 정보", lines=10)
 
 
 
 
45
 
46
- extract_button.click(fn=extract_info, inputs=youtube_url_input, outputs=output)
 
 
 
 
 
 
 
 
 
47
 
48
  if __name__ == "__main__":
49
  demo.launch()
 
1
  import gradio as gr
2
  import yt_dlp
3
+ import os
4
+ import tempfile
5
+ import base64
6
+
7
+ def download_and_merge(youtube_url):
8
+ with tempfile.TemporaryDirectory() as temp_dir:
9
+ ydl_opts = {
10
+ 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
11
+ 'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'),
12
+ 'merge_output_format': 'mp4',
13
+ }
14
+ try:
15
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
16
+ info = ydl.extract_info(youtube_url, download=True)
17
+ filename = ydl.prepare_filename(info)
18
+ final_filename = os.path.splitext(filename)[0] + '.mp4'
19
+
20
+ if os.path.exists(final_filename):
21
+ with open(final_filename, 'rb') as f:
22
+ video_data = f.read()
23
+ b64 = base64.b64encode(video_data).decode()
24
+ return info['title'], b64
25
+ else:
26
+ return info['title'], None
27
+ except Exception as e:
28
+ return str(e), None
29
 
30
  def extract_info(youtube_url):
31
+ title, video_data = download_and_merge(youtube_url)
32
+ if video_data:
33
+ download_link = f'data:video/mp4;base64,{video_data}'
34
+ return (f"제목: {title}\n\n다운로드가 준비되었습니다. '다운로드' 버튼을 클릭하세요.",
35
+ download_link,
36
+ gr.update(visible=True))
37
+ else:
38
+ return f"오류 발생: {title}", None, gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  with gr.Blocks() as demo:
41
+ gr.Markdown("## YouTube 다운로더")
42
  gr.Markdown("주의: 이 도구를 사용하여 저작권이 있는 콘텐츠를 무단으로 다운로드하는 것은 불법입니다.")
43
 
44
  youtube_url_input = gr.Textbox(label="YouTube URL 입력")
45
+ extract_button = gr.Button("다운로드 준비")
46
+ output = gr.Textbox(label="상태", lines=5)
47
+ download_button = gr.Button("다운로드", visible=False)
48
+
49
+ def on_download_click(url):
50
+ 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>'
51
 
52
+ extract_button.click(
53
+ fn=extract_info,
54
+ inputs=youtube_url_input,
55
+ outputs=[output, download_button, download_button]
56
+ )
57
+ download_button.click(
58
+ fn=on_download_click,
59
+ inputs=download_button,
60
+ outputs=gr.HTML()
61
+ )
62
 
63
  if __name__ == "__main__":
64
  demo.launch()