fxyoutube/yt_info.py (view raw)
1from yt_dlp import YoutubeDL
2import fxyoutube.constants as c
3ydl = YoutubeDL()
4
5def handle_format(format):
6
7 if format["resolution"] == "audio only":
8 return None # audio-only
9 try:
10 if format["audio_channels"] is None:
11 return None # video-only
12 except KeyError:
13 return None # video-only
14
15 if format["url"].endswith(".m3u8"):
16 return None # HLS stream
17
18 try:
19 if format["filesize"] > c.MAX_SIZE_BYTES:
20 return None # too large
21 except TypeError:
22 if format["filesize_approx"] > c.MAX_SIZE_BYTES:
23 return None
24
25 return format
26
27def get_info_ytdl(yt_id: str):
28 info = ydl.extract_info(c.BASE_URL + yt_id, download=False)
29 formats = map(handle_format, info["formats"])
30 formats = list(filter(lambda x: x is not None, formats))
31 try:
32 max_format = max(formats, key=lambda x:x["quality"])
33 except ValueError:
34 return None
35
36 return {
37 "id": info["id"],
38 "title": info["title"],
39 "description": info["description"],
40 "uploader": info["uploader"],
41 "uploader_id": info["uploader_id"],
42 "video_ext": max_format["video_ext"],
43 "height": max_format["height"],
44 "width": max_format["width"],
45 "url": max_format["url"],
46 }