-
Notifications
You must be signed in to change notification settings - Fork 2
/
video.py
48 lines (42 loc) · 1.68 KB
/
video.py
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
import youtube_dl as ytdl
import discord
YTDL_OPTS = {
"default_search": "ytsearch",
"format": "bestaudio/best",
"quiet": True,
"extract_flat": "in_playlist"
}
class Video:
"""Class containing information about a particular video."""
def __init__(self, url_or_search, requested_by):
"""Plays audio from (or searches for) a URL."""
with ytdl.YoutubeDL(YTDL_OPTS) as ydl:
video = self._get_info(url_or_search)
video_format = video["formats"][0]
self.stream_url = video_format["url"]
self.video_url = video["webpage_url"]
self.title = video["title"]
self.uploader = video["uploader"] if "uploader" in video else ""
self.thumbnail = video[
"thumbnail"] if "thumbnail" in video else None
self.requested_by = requested_by
def _get_info(self, video_url):
with ytdl.YoutubeDL(YTDL_OPTS) as ydl:
info = ydl.extract_info(video_url, download=False)
video = None
if "_type" in info and info["_type"] == "playlist":
return self._get_info(
info["entries"][0]["url"]) # get info for first video
else:
video = info
return video
def get_embed(self):
"""Makes an embed out of this Video's information."""
embed = discord.Embed(
title=self.title, description=self.uploader, url=self.video_url)
embed.set_footer(
text=f"Requested by {self.requested_by.name}",
icon_url=self.requested_by.avatar_url)
if self.thumbnail:
embed.set_thumbnail(url=self.thumbnail)
return embed