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.

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.

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.

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()
static create_from_url(url, outtmpl='%(title)s-%(id)s.%(ext)s', **ytdl_args) → List[royalnet.audio.youtubedl.YtdlFile]
delete_video_file()
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
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-compatible 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()

Checks if the audio source is already encoded in Opus.

Defaults to False.

read()

Reads 20ms worth of audio.

Subclasses must implement this.

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

If is_opus() method returns True, then it must return 20ms worth of Opus encoded audio. Otherwise, it must be 20ms worth of 16-bit 48KHz stereo PCM, which is about 3,840 bytes per frame (20ms worth of audio).

Returns

A bytes like object that represents the PCM or Opus data.

Return type

bytes