1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

New stuff

This commit is contained in:
Steffo 2019-07-31 01:49:53 +02:00
parent 638e113edb
commit bb0507d79e
3 changed files with 72 additions and 5 deletions

View file

@ -0,0 +1,41 @@
import discord
class FileAudioSource(discord.AudioSource):
"""A :py:class:`discord.AudioSource` that uses a :py:class:`io.BufferedIOBase` as an input instead of memory.
The stream should be in the usual PCM encoding.
Warning:
This AudioSource will consume (and close) the passed stream"""
def __init__(self, file):
self.file = file
def __repr__(self):
if self.file.seekable():
return f"<{self.__class__.__name__} @{self.file.tell()}>"
else:
return f"<{self.__class__.__name__}>"
def is_opus(self):
"""This audio file isn't Opus-encoded, but PCM-encoded.
Returns:
``False``."""
return False
def read(self):
"""Reads 20ms worth of audio.
If the audio is complete, then returning an empty :py:class:`bytes`-like object to signal this is the way to do so."""
data: bytes = self.file.read(discord.opus.Encoder.FRAME_SIZE)
# If the stream is closed, it should stop playing immediatly
if self.file.closed:
return b""
# If there is no more data to be streamed
if len(data) != discord.opus.Encoder.FRAME_SIZE:
# Close the file
self.file.close()
return b""
return data

View file

@ -1,2 +1,28 @@
import typing
import discord import discord
import re
import ffmpeg
from .ytdlfile import YtdlFile from .ytdlfile import YtdlFile
from .fileaudiosource import FileAudioSource
class YtdlDiscord:
def __init__(self, ytdl_file: YtdlFile):
self.ytdl_file: YtdlFile = ytdl_file
self.pcm_filename: typing.Optional[str] = None
def convert_to_pcm(self) -> None:
if not self.ytdl_file.is_downloaded():
raise FileNotFoundError("File hasn't been downloaded yet")
destination_filename = re.sub(r"\.[^.]+$", ".pcm", self.ytdl_file.filename)
(
ffmpeg.input(self.ytdl_file.filename)
.output(destination_filename, format="s16le", ac=2, ar="48000")
.overwrite_output()
.run(quiet=True)
)
self.pcm_filename = destination_filename
def to_audiosource(self) -> discord.AudioSource:
stream = open(self.pcm_filename, "rb")
return FileAudioSource(stream)

View file

@ -18,11 +18,11 @@ class YtdlFile:
def __init__(self, def __init__(self,
url: str, url: str,
info: YtdlInfo = None, info: typing.Optional[YtdlInfo] = None,
filename: str = None): filename: typing.Optional[str] = None):
self.url: str = url self.url: str = url
self.info: YtdlInfo = info self.info: typing.Optional[YtdlInfo] = info
self.filename: str = filename self.filename: typing.Optional[str] = filename
def has_info(self) -> bool: def has_info(self) -> bool:
return self.info is not None return self.info is not None