migrate to pytube

This commit is contained in:
Arthur Lu 2022-08-22 20:50:36 +00:00
parent bde5bfe530
commit 6228104cf1
3 changed files with 30 additions and 32 deletions

View File

@ -1,4 +1,4 @@
discord discord
PyNaCl PyNaCl
pytube pytube
yt_dlp pytube

2
.gitignore vendored
View File

@ -130,4 +130,4 @@ dmypy.json
**/*.json **/*.json
**/session/* **/videos/*

View File

@ -1,8 +1,9 @@
from asyncio import sleep from asyncio import sleep
import discord import discord
from discord.ext import commands from discord.ext import commands
import yt_dlp
from config import * from config import *
from pytube import YouTube, Playlist
import shutil
config_path = "config.json" 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") await ctx.send("you do not have the role to play music")
return return
ydl_opts = {
'format': 'mp4',
'quiet': True,
'paths': {
'home': './session/'
},
'outtmpl': {
'default': '%(autonumber)s.%(ext)s',
},
'postprocessors': [{
'key': 'FFmpegExtractAudio',
}],
}
try: try:
await ctx.voice_client.disconnect() await ctx.voice_client.disconnect()
except: except:
@ -92,20 +79,31 @@ async def play(ctx, *arg):
url = arg[0] url = arg[0]
with yt_dlp.YoutubeDL(ydl_opts) as ydl: yt = YouTube(url)
info = ydl.extract_info(url, download=False) name = yt.title
duration = info.get('duration') duration = yt.length
name = info.get('title')
filepath = 'session/'
fileprefix = ''
filename = name
if duration < 1200: if duration < 1200:
await ctx.send("downloading music requested: {0}".format(name)) await ctx.send("downloading music requested: {0}".format(name))
ydl.download([url])
audio = "session/00001.m4a" 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() 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) 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(): while ctx.voice_client.is_playing():
await sleep(0.01) await sleep(0.01)
await ctx.voice_client.disconnect() await ctx.voice_client.disconnect()
else: else:
await ctx.send("music requested was too long ({0} > 1200)".format(duration)) 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) bot.run(token)