File size: 3,249 Bytes
2cca49c
 
 
 
 
 
 
 
25332e0
 
 
2cca49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753ecf1
 
 
 
 
 
2cca49c
753ecf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cca49c
 
753ecf1
2cca49c
753ecf1
2cca49c
 
 
 
 
 
753ecf1
 
2cca49c
753ecf1
2cca49c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import yt_dlp

class YouTubeExtractor:
    def __init__(self, ydl_opts=None):
        self.ydl_opts = ydl_opts or {
            'quiet': True,
            'no_warnings': True,
            'no_color': True,
            'youtube_include_dash_manifest': False,
            'youtube_include_hls_manifest': False,
            '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'
        }

    def extract_info(self, youtube_url):
        try:
            with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
                info = ydl.extract_info(youtube_url, download=False)
                
                metadata = {
                    "제목": info.get('title', 'N/A'),
                    "채널": info.get('channel', 'N/A'),
                    "업로드 날짜": info.get('upload_date', 'N/A'),
                    "조회수": info.get('view_count', 'N/A'),
                    "길이 (초)": info.get('duration', 'N/A'),
                }
                
                formats = info.get('formats', [])
                
                # 비디오 전용 (가장 높은 해상도)
                best_video_only = max(
                    (f for f in formats if f.get('vcodec') != 'none' and f.get('acodec') == 'none'),
                    key=lambda x: x.get('height', 0),
                    default=None
                )
                
                # 오디오 전용 (가장 높은 비트레이트)
                best_audio_only = max(
                    (f for f in formats if f.get('acodec') != 'none' and f.get('vcodec') == 'none'),
                    key=lambda x: x.get('abr', 0),
                    default=None
                )
                
                # 비디오+오디오 결합 (가장 높은 해상도)
                best_combined = max(
                    (f for f in formats if f.get('vcodec') != 'none' and f.get('acodec') != 'none'),
                    key=lambda x: x.get('height', 0),
                    default=None
                )
                
                return metadata, best_video_only, best_audio_only, best_combined

        except Exception as e:
            return None, None, None, None

    def format_output(self, metadata, best_video_only, best_audio_only, best_combined):
        if not metadata:
            return "오류 발생: 메타데이터를 추출할 수 없습니다.", "다운로드 링크를 찾을 수 없습니다."

        metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
        
        video_only_link = f'<a href="{best_video_only["url"]}" target="_blank">비디오만 다운로드</a>' if best_video_only else ""
        audio_only_link = f'<a href="{best_audio_only["url"]}" target="_blank">오디오만 다운로드</a>' if best_audio_only else ""
        combined_link = f'<a href="{best_combined["url"]}" target="_blank">비디오+오디오 다운로드</a>' if best_combined else ""
        
        download_links = f"{video_only_link}<br>{audio_only_link}<br>{combined_link}" if any([video_only_link, audio_only_link, combined_link]) else "다운로드 링크를 찾을 수 없습니다."
        
        return metadata_str, download_links