-
-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add synchronization to start of audio recordings (#1984)
Signed-off-by: Om <92863779+Om1609@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: VincentRPS <vincentbusiness55@gmail.com> Co-authored-by: plun1331 <49261529+plun1331@users.noreply.github.com> Co-authored-by: Lala Sabathil <lala@pycord.dev> Co-authored-by: JustaSqu1d <overenchanted.gaming@gmail.com>
- Loading branch information
1 parent
4675c6c
commit 7a6a42c
Showing
4 changed files
with
157 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import io | ||
|
||
import pydub # pip install pydub==0.25.1 | ||
|
||
import discord | ||
from discord.sinks import MP3Sink | ||
|
||
bot = discord.Bot() | ||
connections: dict[int, discord.VoiceClient] = {} | ||
|
||
|
||
@bot.event | ||
async def on_ready(): | ||
print(f"Logged in as {bot.user}") | ||
|
||
|
||
async def finished_callback(sink: MP3Sink, channel: discord.TextChannel): | ||
mention_strs = [] | ||
audio_segs: list[pydub.AudioSegment] = [] | ||
files: list[discord.File] = [] | ||
|
||
longest = pydub.AudioSegment.empty() | ||
|
||
for user_id, audio in sink.audio_data.items(): | ||
mention_strs.append(f"<@{user_id}>") | ||
|
||
seg = pydub.AudioSegment.from_file(audio.file, format="mp3") | ||
|
||
# Determine the longest audio segment | ||
if len(seg) > len(longest): | ||
audio_segs.append(longest) | ||
longest = seg | ||
else: | ||
audio_segs.append(seg) | ||
|
||
audio.file.seek(0) | ||
files.append(discord.File(audio.file, filename=f"{user_id}.mp3")) | ||
|
||
for seg in audio_segs: | ||
longest = longest.overlay(seg) | ||
|
||
with io.BytesIO() as f: | ||
longest.export(f, format="mp3") | ||
await channel.send( | ||
f"Finished! Recorded audio for {', '.join(mention_strs)}.", | ||
files=files + [discord.File(f, filename="recording.mp3")], | ||
) | ||
|
||
|
||
@bot.command() | ||
async def join(ctx: discord.ApplicationContext): | ||
"""Join the voice channel!""" | ||
voice = ctx.author.voice | ||
|
||
if not voice: | ||
return await ctx.respond("You're not in a vc right now") | ||
|
||
vc = await voice.channel.connect() | ||
connections.update({ctx.guild.id: vc}) | ||
|
||
await ctx.respond("Joined!") | ||
|
||
|
||
@bot.command() | ||
async def start(ctx: discord.ApplicationContext): | ||
"""Record the voice channel!""" | ||
voice = ctx.author.voice | ||
|
||
if not voice: | ||
return await ctx.respond("You're not in a vc right now") | ||
|
||
vc = connections.get(ctx.guild.id) | ||
|
||
if not vc: | ||
return await ctx.respond( | ||
"I'm not in a vc right now. Use `/join` to make me join!" | ||
) | ||
|
||
vc.start_recording( | ||
MP3Sink(), | ||
finished_callback, | ||
ctx.channel, | ||
sync_start=True, | ||
) | ||
|
||
await ctx.respond("The recording has started!") | ||
|
||
|
||
@bot.command() | ||
async def stop(ctx: discord.ApplicationContext): | ||
"""Stop the recording""" | ||
vc = connections.get(ctx.guild.id) | ||
|
||
if not vc: | ||
return await ctx.respond("There's no recording going on right now") | ||
|
||
vc.stop_recording() | ||
|
||
await ctx.respond("The recording has stopped!") | ||
|
||
|
||
@bot.command() | ||
async def leave(ctx: discord.ApplicationContext): | ||
"""Leave the voice channel!""" | ||
vc = connections.get(ctx.guild.id) | ||
|
||
if not vc: | ||
return await ctx.respond("I'm not in a vc right now") | ||
|
||
await vc.disconnect() | ||
|
||
await ctx.respond("Left!") | ||
|
||
|
||
bot.run("TOKEN") |