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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -55
app.py CHANGED
@@ -1,19 +1,14 @@
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 = {
14
  'quiet': True,
15
  'no_warnings': True,
16
  'no_color': True,
 
 
 
17
  }
18
  try:
19
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
@@ -27,69 +22,41 @@ def extract_info(youtube_url):
27
  "길이 (초)": info.get('duration', 'N/A'),
28
  }
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))
54
  except Exception as e:
55
- return (f"오류 발생: {str(e)}", None, None, None,
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 입력")
63
  extract_button = gr.Button("정보 추출")
64
  output = gr.Textbox(label="추출된 정보", lines=10)
65
- video_button = gr.Button("비디오 다운로드 (오디오 없음)", visible=False)
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()
 
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:
 
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
+ download_url = best_mp4['url']
31
+ metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
32
+ return metadata_str, download_url, gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  else:
34
+ return "적절한 형식을 찾을 수 없습니다.", None, gr.update(visible=False)
 
35
  except Exception as e:
36
+ return f"오류 발생: {str(e)}", None, gr.update(visible=False)
37
+
38
+ def on_download_click(url):
39
+ return f'<script>window.location.href = "{url}";</script>'
40
 
41
  with gr.Blocks() as demo:
42
+ gr.Markdown("## YouTube 메타데이터 및 다운로드 링크 추출기")
43
  gr.Markdown("주의: 이 도구를 사용하여 저작권이 있는 콘텐츠를 무단으로 다운로드하는 것은 불법입니다.")
44
 
45
  youtube_url_input = gr.Textbox(label="YouTube URL 입력")
46
  extract_button = gr.Button("정보 추출")
47
  output = gr.Textbox(label="추출된 정보", lines=10)
48
+ download_button = gr.Button("다운로드", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  extract_button.click(
51
  fn=extract_info,
52
  inputs=youtube_url_input,
53
+ outputs=[output, download_button, download_button]
54
+ )
55
+ download_button.click(
56
+ fn=on_download_click,
57
+ inputs=download_button,
58
+ outputs=gr.HTML()
59
  )
 
 
 
60
 
61
  if __name__ == "__main__":
62
  demo.launch()