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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -45
app.py CHANGED
@@ -1,64 +1,73 @@
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()
 
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
+ }
10
+ try:
11
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
12
+ info = ydl.extract_info(youtube_url, download=False)
13
+
14
+ metadata = {
15
+ "제목": info.get('title', 'N/A'),
16
+ "채널": info.get('channel', 'N/A'),
17
+ "업로드 날짜": info.get('upload_date', 'N/A'),
18
+ "조회수": info.get('view_count', 'N/A'),
19
+ "길이 (초)": info.get('duration', 'N/A'),
20
+ }
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))
45
+ except Exception as e:
46
+ return (f"오류 발생: {str(e)}", None, None, None,
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 입력")
54
+ extract_button = gr.Button("정보 추출")
55
+ output = gr.Textbox(label="추출된 정보", lines=10)
56
+ video_button = gr.Button("비디오 다운로드 (오디오 없음)", visible=False)
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()