Compare commits
3 Commits
94837f0e77
...
65168d38f9
Author | SHA1 | Date | |
---|---|---|---|
65168d38f9 | |||
70ed37737c | |||
3719fc69b5 |
46
youtubedl.py
46
youtubedl.py
@ -12,31 +12,7 @@ from constants import BAR_LENGTH, EMBED_COLOR, YTDL_OPTIONS
|
|||||||
ytdl = yt_dlp.YoutubeDL(YTDL_OPTIONS)
|
ytdl = yt_dlp.YoutubeDL(YTDL_OPTIONS)
|
||||||
|
|
||||||
|
|
||||||
class PCMVolumeTransformer(disnake.AudioSource):
|
class TrackedAudioSource(disnake.AudioSource):
|
||||||
def __init__(self, original: disnake.AudioSource, volume: float = 1.0) -> None:
|
|
||||||
if original.is_opus():
|
|
||||||
raise disnake.ClientException("AudioSource must not be Opus encoded.")
|
|
||||||
|
|
||||||
self.original = original
|
|
||||||
self.volume = volume
|
|
||||||
|
|
||||||
@property
|
|
||||||
def volume(self) -> float:
|
|
||||||
return self._volume
|
|
||||||
|
|
||||||
@volume.setter
|
|
||||||
def volume(self, value: float) -> None:
|
|
||||||
self._volume = max(value, 0.0)
|
|
||||||
|
|
||||||
def cleanup(self) -> None:
|
|
||||||
self.original.cleanup()
|
|
||||||
|
|
||||||
def read(self) -> bytes:
|
|
||||||
ret = self.original.read()
|
|
||||||
return audioop.mul(ret, 2, self._volume)
|
|
||||||
|
|
||||||
|
|
||||||
class CustomAudioSource(disnake.AudioSource):
|
|
||||||
def __init__(self, source):
|
def __init__(self, source):
|
||||||
self._source = source
|
self._source = source
|
||||||
self.read_count = 0
|
self.read_count = 0
|
||||||
@ -56,9 +32,25 @@ class CustomAudioSource(disnake.AudioSource):
|
|||||||
return self.read_count * 0.02
|
return self.read_count * 0.02
|
||||||
|
|
||||||
|
|
||||||
|
class PCMVolumeTransformer(disnake.AudioSource):
|
||||||
|
def __init__(self, original: TrackedAudioSource, volume: float = 1.0) -> None:
|
||||||
|
if original.is_opus():
|
||||||
|
raise disnake.ClientException("AudioSource must not be Opus encoded.")
|
||||||
|
|
||||||
|
self.original = original
|
||||||
|
self.volume = volume
|
||||||
|
|
||||||
|
def cleanup(self) -> None:
|
||||||
|
self.original.cleanup()
|
||||||
|
|
||||||
|
def read(self) -> bytes:
|
||||||
|
ret = self.original.read()
|
||||||
|
return audioop.mul(ret, 2, self.volume)
|
||||||
|
|
||||||
|
|
||||||
class YTDLSource(PCMVolumeTransformer):
|
class YTDLSource(PCMVolumeTransformer):
|
||||||
def __init__(
|
def __init__(
|
||||||
self, source: CustomAudioSource, *, data: dict[str, Any], volume: float = 0.5
|
self, source: TrackedAudioSource, *, data: dict[str, Any], volume: float = 0.5
|
||||||
):
|
):
|
||||||
super().__init__(source, volume)
|
super().__init__(source, volume)
|
||||||
|
|
||||||
@ -93,7 +85,7 @@ class YTDLSource(PCMVolumeTransformer):
|
|||||||
data = data["entries"][0]
|
data = data["entries"][0]
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
CustomAudioSource(
|
TrackedAudioSource(
|
||||||
disnake.FFmpegPCMAudio(
|
disnake.FFmpegPCMAudio(
|
||||||
data["url"] if stream else ytdl.prepare_filename(data),
|
data["url"] if stream else ytdl.prepare_filename(data),
|
||||||
before_options="-vn -reconnect 1",
|
before_options="-vn -reconnect 1",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user