royalnet.audio

Video and audio downloading related classes, mainly used for Discord voice bots.

class royalnet.audio.PlayMode

The base class for a PlayMode, such as royalnet.audio.Playlist. Inherit from this class if you want to create a custom PlayMode.

__init__()

Create a new PlayMode and initialize the generator inside.

_generate_generator()

Factory function for an async generator that changes the now_playing property either to a discord.audio.RoyalPCMAudio or to None, then yields the value it changed it to.

Yields

The royalnet.audio.RoyalPCMAudio to be played next.

add(item: royalnet.audio.royalpcmaudio.RoyalPCMAudio) → None

Add a new royalnet.audio.RoyalPCMAudio to the PlayMode.

Parameters

item – The item to add to the PlayMode.

delete() → None

Delete all royalnet.audio.RoyalPCMAudio contained inside this PlayMode.

next() → Optional[royalnet.audio.royalpcmaudio.RoyalPCMAudio]

Get the next royalnet.audio.RoyalPCMAudio from the list and advance it.

Returns

The next royalnet.audio.RoyalPCMAudio.

queue_preview() → List[royalnet.audio.royalpcmaudio.RoyalPCMAudio]

Display all the videos in the PlayMode as a list, if possible.

To be used with queue commands, for example.

Raises

NotImplementedError – If a preview can’t be generated.

Returns

A list of videos contained in the queue.

videos_left() → Union[int, float]

Return the number of videos left in the PlayMode.

Returns

Usually a int, but may return also math.inf if the PlayMode is infinite.

class royalnet.audio.Playlist(starting_list: List[royalnet.audio.royalpcmaudio.RoyalPCMAudio] = None)

A video list. royalnet.audio.RoyalPCMAudio played are removed from the list.

__init__(starting_list: List[royalnet.audio.royalpcmaudio.RoyalPCMAudio] = None)

Create a new Playlist.

Parameters

starting_list – A list of items with which the Playlist will be created.

_generate_generator()

Factory function for an async generator that changes the now_playing property either to a discord.audio.RoyalPCMAudio or to None, then yields the value it changed it to.

Yields

The royalnet.audio.RoyalPCMAudio to be played next.

add(item) → None

Add a new royalnet.audio.RoyalPCMAudio to the PlayMode.

Parameters

item – The item to add to the PlayMode.

delete() → None

Delete all royalnet.audio.RoyalPCMAudio contained inside this PlayMode.

queue_preview() → List[royalnet.audio.royalpcmaudio.RoyalPCMAudio]

Display all the videos in the PlayMode as a list, if possible.

To be used with queue commands, for example.

Raises

NotImplementedError – If a preview can’t be generated.

Returns

A list of videos contained in the queue.

videos_left() → Union[int, float]

Return the number of videos left in the PlayMode.

Returns

Usually a int, but may return also math.inf if the PlayMode is infinite.

class royalnet.audio.Pool(starting_pool: List[royalnet.audio.royalpcmaudio.RoyalPCMAudio] = None)

A royalnet.audio.RoyalPCMAudio pool. royalnet.audio.RoyalPCMAudio are selected in random order and are not repeated until every song has been played at least once.

__init__(starting_pool: List[royalnet.audio.royalpcmaudio.RoyalPCMAudio] = None)

Create a new Pool.

Parameters

starting_pool – A list of items the Pool will be created from.

_generate_generator()

Factory function for an async generator that changes the now_playing property either to a discord.audio.RoyalPCMAudio or to None, then yields the value it changed it to.

Yields

The royalnet.audio.RoyalPCMAudio to be played next.

add(item) → None

Add a new royalnet.audio.RoyalPCMAudio to the PlayMode.

Parameters

item – The item to add to the PlayMode.

delete() → None

Delete all royalnet.audio.RoyalPCMAudio contained inside this PlayMode.

queue_preview() → List[royalnet.audio.royalpcmaudio.RoyalPCMAudio]

Display all the videos in the PlayMode as a list, if possible.

To be used with queue commands, for example.

Raises

NotImplementedError – If a preview can’t be generated.

Returns

A list of videos contained in the queue.

videos_left() → Union[int, float]

Return the number of videos left in the PlayMode.

Returns

Usually a int, but may return also math.inf if the PlayMode is infinite.

class royalnet.audio.YtdlFile(info: royalnet.audio.youtubedl.YtdlInfo, outtmpl='%(title)s-%(id)s.%(ext)s', **ytdl_args)

A wrapper around a youtube_dl downloaded file.

_stop_download()

I have no clue of what this does, or why is it here. Possibly remove it?

Raises

InterruptDownload – …uhhh, always?

static create_from_url(url, outtmpl='%(title)s-%(id)s.%(ext)s', **ytdl_args) → List[royalnet.audio.youtubedl.YtdlFile]

Download the videos at the specified url.

Parameters
  • url – The url to download the videos from.

  • outtmpl – The filename that the downloaded videos are going to have. The name can be formatted according to the outtmpl documentation.

  • ytdl_args – Other arguments to be passed to the YoutubeDL object.

Returns

A list of YtdlFiles.

delete_video_file()

Delete the file located at self.video_filename.

Note

No checks are done when deleting, so it may try to delete a non-existing file and raise an exception or do some other weird stuff with weird filenames.

ytdl_args = {'logger': <Logger royalnet.audio.youtubedl (WARNING)>, 'no_warnings': True, 'noplaylist': True, 'quiet': True}
class royalnet.audio.YtdlInfo(info: Dict[str, Any])

A wrapper around youtube_dl extracted info.

__init__(info: Dict[str, Any])

Create a YtdlInfo from the dict returned by the youtube_dl.YoutubeDL.extract_info() function.

Warning

Does not download the info, for that use royalnet.audio.YtdlInfo.create_from_url().

static create_from_url(url, **ytdl_args) → List[royalnet.audio.youtubedl.YtdlInfo]
download(outtmpl='%(title)s-%(id)s.%(ext)s', **ytdl_args) → royalnet.audio.youtubedl.YtdlFile
to_discord_embed() → discord.embeds.Embed
class royalnet.audio.RoyalPCMFile(info: royalnet.audio.youtubedl.YtdlInfo, **ytdl_args)
audio_filename

Returns: The name of the downloaded and PCM-converted audio file.

static create_from_url(url: str, **ytdl_args) → List[royalnet.audio.royalpcmfile.RoyalPCMFile]

Download a file with youtube_dl and create a list of discord.audio.RoyalPCMFile.

Parameters
  • url – The url of the file to download.

  • ytdl_args – Extra arguments to be passed to YoutubeDL while downloading.

Returns

A list of RoyalPCMAudios, each corresponding to a downloaded video.

static create_from_ytsearch(search: str, amount: int = 1, **ytdl_args) → List[royalnet.audio.royalpcmfile.RoyalPCMFile]

Search a string on YouTube and download the first amount number of videos, then download those with youtube_dl and create a list of discord.audio.RoyalPCMFile.

Parameters
  • search – The string to search on YouTube.

  • amount – The number of videos to download.

  • ytdl_args – Extra arguments to be passed to YoutubeDL while downloading.

Returns

A list of RoyalPCMFiles, each corresponding to a downloaded video.

delete_audio_file()

Delete the PCM-converted audio file.

ytdl_args = {'format': 'bestaudio'}
ytdl_filename
Returns

The name of the downloaded video file, as a str.

Warning

It’s going to be deleted as soon as the royalnet.audio.RoyalPCMFile.__init__() function has completed, so it’s probably not going to be very useful…

class royalnet.audio.RoyalPCMAudio(rpf: royalnet.audio.royalpcmfile.RoyalPCMFile)

A discord.AudioSource that keeps data in a file instead of in memory.

__init__(rpf: royalnet.audio.royalpcmfile.RoyalPCMFile)

Create a discord.audio.RoyalPCMAudio from a royalnet.audio.RoyalPCMFile.

static create_from_url(url: str) → List[royalnet.audio.royalpcmaudio.RoyalPCMAudio]

Download a file with youtube_dl and create a list of RoyalPCMAudios.

Parameters

url – The url of the file to download.

Returns

A list of RoyalPCMAudios, each corresponding to a downloaded video.

static create_from_ytsearch(search: str, amount: int = 1) → List[royalnet.audio.royalpcmaudio.RoyalPCMAudio]

Search a string on YouTube and download the first amount number of videos, then download those with youtube_dl and create a list of RoyalPCMAudios.

Parameters
  • search – The string to search on YouTube.

  • amount – The number of videos to download.

Returns

A list of RoyalPCMAudios, each corresponding to a downloaded video.

delete()

Permanently delete the downloaded file.

is_opus()

This audio file isn’t Opus-encoded, but PCM-encoded.

Returns

False.

read()

Reads 20ms worth of audio.

If the audio is complete, then returning an empty bytes-like object to signal this is the way to do so.