From 6228104cf1e6b1ac0c19ff6c01aa503c924d20f3 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 22 Aug 2022 20:50:36 +0000 Subject: [PATCH] migrate to pytube --- .devcontainer/requirements.txt | 2 +- .gitignore | 2 +- src/musicbot.py | 58 ++++++++++++++++------------------ 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/.devcontainer/requirements.txt b/.devcontainer/requirements.txt index 1d5d8f6..86fba5e 100644 --- a/.devcontainer/requirements.txt +++ b/.devcontainer/requirements.txt @@ -1,4 +1,4 @@ discord PyNaCl pytube -yt_dlp \ No newline at end of file +pytube \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3511c61..e338c79 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,4 @@ dmypy.json **/*.json -**/session/* \ No newline at end of file +**/videos/* \ No newline at end of file diff --git a/src/musicbot.py b/src/musicbot.py index 61e7888..3d00b7d 100644 --- a/src/musicbot.py +++ b/src/musicbot.py @@ -1,8 +1,9 @@ from asyncio import sleep import discord from discord.ext import commands -import yt_dlp from config import * +from pytube import YouTube, Playlist +import shutil config_path = "config.json" @@ -71,20 +72,6 @@ async def play(ctx, *arg): await ctx.send("you do not have the role to play music") return - ydl_opts = { - 'format': 'mp4', - 'quiet': True, - 'paths': { - 'home': './session/' - }, - 'outtmpl': { - 'default': '%(autonumber)s.%(ext)s', - }, - 'postprocessors': [{ - 'key': 'FFmpegExtractAudio', - }], - } - try: await ctx.voice_client.disconnect() except: @@ -92,20 +79,31 @@ async def play(ctx, *arg): url = arg[0] - with yt_dlp.YoutubeDL(ydl_opts) as ydl: - info = ydl.extract_info(url, download=False) - duration = info.get('duration') - name = info.get('title') - if duration < 1200: - await ctx.send("downloading music requested: {0}".format(name)) - ydl.download([url]) - audio = "session/00001.m4a" - await ctx.author.voice.channel.connect() - ctx.voice_client.play(discord.FFmpegPCMAudio(audio), after=lambda e: print('Player error: %s' % e) if e else None) - while ctx.voice_client.is_playing(): - await sleep(0.01) - await ctx.voice_client.disconnect() - else: - await ctx.send("music requested was too long ({0} > 1200)".format(duration)) + yt = YouTube(url) + name = yt.title + duration = yt.length + + filepath = 'session/' + fileprefix = '' + filename = name + + if duration < 1200: + await ctx.send("downloading music requested: {0}".format(name)) + + yt.streams.filter(only_audio=True, file_extension='mp4').last().download(output_path=filepath, filename=filename, filename_prefix=fileprefix) + + path = filepath + fileprefix + filename + + await ctx.author.voice.channel.connect() + ctx.voice_client.play(discord.FFmpegPCMAudio(path), after=lambda e: print('Player error: %s' % e) if e else None) + + while ctx.voice_client.is_playing(): + await sleep(0.01) + await ctx.voice_client.disconnect() + + else: + await ctx.send("music requested was too long ({0} > 1200)".format(duration)) + + shutil.rmtree('session/') # temporary cleanup procedure, will add caching later bot.run(token) \ No newline at end of file