JUNGU commited on
Commit
c8891ad
·
verified ·
1 Parent(s): d596036

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -1,5 +1,13 @@
1
  import gradio as gr
2
  import yt_dlp
 
 
 
 
 
 
 
 
3
 
4
  def extract_info(youtube_url):
5
  ydl_opts = {
@@ -21,24 +29,25 @@ def extract_info(youtube_url):
21
 
22
  formats = info.get('formats', [])
23
 
24
- # 최고 품질의 비디오 (오디오 없음)
25
  best_video = max((f for f in formats if f['vcodec'] != 'none' and f['acodec'] == 'none'),
26
  key=lambda x: x.get('height', 0), default=None)
27
 
28
- # 최고 품질의 오디오
29
  best_audio = max((f for f in formats if f['acodec'] != 'none' and f['vcodec'] == 'none'),
30
  key=lambda x: x.get('abr', 0), default=None)
31
 
32
- # 비디오+오디오 통합 스트림 (일반적으로 더 낮은 품질)
33
  best_combined = max((f for f in formats if f['vcodec'] != 'none' and f['acodec'] != 'none'),
34
  key=lambda x: x.get('height', 0), default=None)
35
 
36
  metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
37
 
38
  if best_video and best_audio:
 
 
 
 
39
  return (metadata_str,
40
- best_video['url'], best_audio['url'], best_combined['url'] if best_combined else None,
41
- gr.update(visible=True), gr.update(visible=True), gr.update(visible=True if best_combined else False))
42
  else:
43
  return ("적절한 형식을 찾을 수 없습니다.", None, None, None,
44
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
@@ -47,7 +56,7 @@ def extract_info(youtube_url):
47
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
48
 
49
  with gr.Blocks() as demo:
50
- gr.Markdown("## YouTube 메타데이터 및 다운로드 URL 추출기")
51
  gr.Markdown("주의: 이 도구를 사용하여 저작권이 있는 콘텐츠를 무단으로 다운로드하는 것은 불법입니다.")
52
 
53
  youtube_url_input = gr.Textbox(label="YouTube URL 입력")
@@ -57,17 +66,30 @@ with gr.Blocks() as demo:
57
  audio_button = gr.Button("오디오 다운로드", visible=False)
58
  combined_button = gr.Button("비디오+오디오 다운로드", visible=False)
59
 
60
- def on_download_click(url):
61
- return f'<script>window.location.href = "{url}";</script>'
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  extract_button.click(
64
  fn=extract_info,
65
  inputs=youtube_url_input,
66
  outputs=[output, video_button, audio_button, combined_button, video_button, audio_button, combined_button]
67
  )
68
- video_button.click(fn=on_download_click, inputs=video_button, outputs=gr.HTML())
69
- audio_button.click(fn=on_download_click, inputs=audio_button, outputs=gr.HTML())
70
- combined_button.click(fn=on_download_click, inputs=combined_button, outputs=gr.HTML())
71
 
72
  if __name__ == "__main__":
73
  demo.launch()
 
1
  import gradio as gr
2
  import yt_dlp
3
+ import base64
4
+ import requests
5
+
6
+ def download_and_encode(url):
7
+ response = requests.get(url)
8
+ if response.status_code == 200:
9
+ return base64.b64encode(response.content).decode('utf-8')
10
+ return None
11
 
12
  def extract_info(youtube_url):
13
  ydl_opts = {
 
29
 
30
  formats = info.get('formats', [])
31
 
 
32
  best_video = max((f for f in formats if f['vcodec'] != 'none' and f['acodec'] == 'none'),
33
  key=lambda x: x.get('height', 0), default=None)
34
 
 
35
  best_audio = max((f for f in formats if f['acodec'] != 'none' and f['vcodec'] == 'none'),
36
  key=lambda x: x.get('abr', 0), default=None)
37
 
 
38
  best_combined = max((f for f in formats if f['vcodec'] != 'none' and f['acodec'] != 'none'),
39
  key=lambda x: x.get('height', 0), default=None)
40
 
41
  metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
42
 
43
  if best_video and best_audio:
44
+ video_b64 = download_and_encode(best_video['url'])
45
+ audio_b64 = download_and_encode(best_audio['url'])
46
+ combined_b64 = download_and_encode(best_combined['url']) if best_combined else None
47
+
48
  return (metadata_str,
49
+ video_b64, audio_b64, combined_b64,
50
+ gr.update(visible=True), gr.update(visible=True), gr.update(visible=True if combined_b64 else False))
51
  else:
52
  return ("적절한 형식을 찾을 수 없습니다.", None, None, None,
53
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
 
56
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
57
 
58
  with gr.Blocks() as demo:
59
+ gr.Markdown("## YouTube 메타데이터 및 다운로드")
60
  gr.Markdown("주의: 이 도구를 사용하여 저작권이 있는 콘텐츠를 무단으로 다운로드하는 것은 불법입니다.")
61
 
62
  youtube_url_input = gr.Textbox(label="YouTube URL 입력")
 
66
  audio_button = gr.Button("오디오 다운로드", visible=False)
67
  combined_button = gr.Button("비디오+오디오 다운로드", visible=False)
68
 
69
+ def on_download_click(b64_data, file_type):
70
+ return f'''
71
+ <script>
72
+ var binary = atob('{b64_data}');
73
+ var array = new Uint8Array(binary.length);
74
+ for (var i = 0; i < binary.length; i++) {{
75
+ array[i] = binary.charCodeAt(i);
76
+ }}
77
+ var blob = new Blob([array], {{type: 'video/mp4'}});
78
+ var link = document.createElement('a');
79
+ link.href = window.URL.createObjectURL(blob);
80
+ link.download = "download.{file_type}";
81
+ link.click();
82
+ </script>
83
+ '''
84
 
85
  extract_button.click(
86
  fn=extract_info,
87
  inputs=youtube_url_input,
88
  outputs=[output, video_button, audio_button, combined_button, video_button, audio_button, combined_button]
89
  )
90
+ video_button.click(fn=lambda x: on_download_click(x, 'mp4'), inputs=video_button, outputs=gr.HTML())
91
+ audio_button.click(fn=lambda x: on_download_click(x, 'm4a'), inputs=audio_button, outputs=gr.HTML())
92
+ combined_button.click(fn=lambda x: on_download_click(x, 'mp4'), inputs=combined_button, outputs=gr.HTML())
93
 
94
  if __name__ == "__main__":
95
  demo.launch()