Create youtube_extractor.py
Browse files- youtube_extractor.py +48 -0
youtube_extractor.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yt_dlp
|
2 |
+
|
3 |
+
class YouTubeExtractor:
|
4 |
+
def __init__(self, ydl_opts=None):
|
5 |
+
self.ydl_opts = ydl_opts or {
|
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 |
+
|
14 |
+
def extract_info(self, youtube_url):
|
15 |
+
try:
|
16 |
+
with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
|
17 |
+
info = ydl.extract_info(youtube_url, download=False)
|
18 |
+
|
19 |
+
metadata = {
|
20 |
+
"제목": info.get('title', 'N/A'),
|
21 |
+
"채널": info.get('channel', 'N/A'),
|
22 |
+
"업로드 날짜": info.get('upload_date', 'N/A'),
|
23 |
+
"조회수": info.get('view_count', 'N/A'),
|
24 |
+
"길이 (초)": info.get('duration', 'N/A'),
|
25 |
+
}
|
26 |
+
|
27 |
+
formats = info.get('formats', [])
|
28 |
+
|
29 |
+
best_video_only = next((f for f in formats if f['ext'] == 'mp4' and f.get('vcodec') != 'none' and f.get('acodec') == 'none'), None)
|
30 |
+
combined = next((f for f in formats if f['ext'] == 'mp4' and f.get('vcodec') != 'none' and f.get('acodec') != 'none'), None)
|
31 |
+
|
32 |
+
return metadata, best_video_only, combined
|
33 |
+
|
34 |
+
except Exception as e:
|
35 |
+
return None, None, None
|
36 |
+
|
37 |
+
def format_output(self, metadata, best_video_only, combined):
|
38 |
+
if not metadata:
|
39 |
+
return "오류 발생: 메타데이터를 추출할 수 없습니다.", "다운로드 링크를 찾을 수 없습니다."
|
40 |
+
|
41 |
+
metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
|
42 |
+
|
43 |
+
video_only_link = f'<a href="{best_video_only["url"]}" target="_blank">비디오만 다운로드</a>' if best_video_only else ""
|
44 |
+
combined_link = f'<a href="{combined["url"]}" target="_blank">비디오+오디오 다운로드</a>' if combined else ""
|
45 |
+
|
46 |
+
download_links = f"{video_only_link}<br>{combined_link}" if video_only_link or combined_link else "다운로드 링크를 찾을 수 없습니다."
|
47 |
+
|
48 |
+
return metadata_str, download_links
|