From 3b9843118d27afb3d0627c4802247691aabe7328 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 17 Apr 2019 18:53:53 +0200 Subject: [PATCH] Bot is finally working! --- royalgames.py | 1 + royalnet/audio/royalaudiofile.py | 26 +++++++++++++++----------- royalnet/bots/discord.py | 3 ++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/royalgames.py b/royalgames.py index b971390b..7bc782f2 100644 --- a/royalgames.py +++ b/royalgames.py @@ -12,6 +12,7 @@ loop = asyncio.get_event_loop() log = logging.root log.addHandler(logging.StreamHandler()) +logging.getLogger("royalnet.audio.royalaudiofile").setLevel(logging.DEBUG) logging.getLogger("royalnet.bots.discord").setLevel(logging.DEBUG) commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, diff --git a/royalnet/audio/royalaudiofile.py b/royalnet/audio/royalaudiofile.py index 032d62dd..a0359a3b 100644 --- a/royalnet/audio/royalaudiofile.py +++ b/royalnet/audio/royalaudiofile.py @@ -6,7 +6,6 @@ import typing import logging as _logging from .youtubedl import YtdlFile, YtdlInfo - log = _logging.getLogger(__name__) @@ -20,16 +19,15 @@ class RoyalAudioFile(YtdlFile): # Overwrite the new ytdl_args self.ytdl_args = {**self.ytdl_args, **ytdl_args} log.info(f"Now downloading {info.webpage_url}") - super().__init__(info, outtmpl="./opusfiles/%(title)s-%(id)s.%(ext)s", **self.ytdl_args) + super().__init__(info, outtmpl="./downloads/%(title)s-%(id)s.ytdl", **self.ytdl_args) # Find the audio_filename with a regex (should be video.opus) - log.info(f"Starting conversion of {self.video_filename}") - self.audio_filename = re.sub(rf"\.{self.info.ext}$", ".opus", self.video_filename) + log.info(f"Preparing {self.video_filename}...") # Convert the video to opus # Actually not needed, but we do this anyways for compression reasons - converter = ffmpeg.input(self.video_filename) \ - .output(self.audio_filename) - converter.run(quiet=True) - log.info(f"Converted to {self.audio_filename}") + ffmpeg.input(self.video_filename) \ + .output(self.audio_filename, format="s16le", acodec="pcm_s16le", ac=2, ar="48000") \ + .overwrite_output() \ + .run(quiet=not __debug__) # Delete the video file log.info(f"Deleting {self.video_filename}") self.delete_video_file() @@ -39,9 +37,15 @@ class RoyalAudioFile(YtdlFile): info_list = YtdlInfo.create_from_url(url) return [RoyalAudioFile(info) for info in info_list] + @property + def audio_filename(self): + return f"./downloads/{self.info.title}-{self.info.id}.pcm" + + def as_audio_source(self): + # TODO: find a way to close this + file = open(self.audio_filename, "rb") + return discord.PCMAudio(file) + def delete_audio_file(self): # TODO: _might_ be unsafe, test this os.remove(self.audio_filename) - - def as_audio_source(self): - return discord.FFmpegPCMAudio(f"./{self.audio_filename}") diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index 485b1225..461f915e 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -213,6 +213,7 @@ class DiscordBot: async def network_handler(self, message: Message) -> Message: """Handle a Royalnet request.""" + log.debug(f"Received a {message.__class__.__name__}") if isinstance(message, SummonMessage): return await self.nh_summon(message) elif isinstance(message, PlayMessage): @@ -246,7 +247,7 @@ class DiscordBot: def advance(error=None): loop.create_task(self.advance_music_data(guild)) - log.info(f"Starting to play {next_file.audio_filename}") + log.info(f"Starting playback of {next_file.info.title}") voice_client.play(next_file.as_audio_source(), after=advance) async def nh_play(self, message: PlayMessage):