-
Notifications
You must be signed in to change notification settings - Fork 0
/
YoutubeDownloader.py
73 lines (68 loc) · 2.29 KB
/
YoutubeDownloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import discord
from discord.ext import commands
import asyncio
import youtube_dl
import os
import sys
from discord.utils import get
from discord import FFmpegPCMAudio
bot = commands.Bot(command_prefix = ["Your Prefix Here"])
token = 'Your Token Here'
@bot.event
async def on_ready():
print("YoutubeDownloader가 시작되었어요!")
@bot.command(pass_context=True)
async def 다운로드(ctx, url: str):
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '%(title)s.mp3',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '128',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.cache.remove()
dictMeta = ydl.extract_info(url, download=True)
global MusicName
MusicName = dictMeta["title"]
embed = discord.Embed(color=0xabcdef, description=f"다운로드를 완료했어요!")
embed.add_field(name="제목", value=f"{MusicName}", inline=False)
await ctx.author.send(embed=embed)
@bot.command()
async def 재생(ctx, url: str):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice is None:
if ctx.author.voice and ctx.author.voice.channel:
channel = ctx.author.voice.channel
await channel.connect()
await ctx.send("채널에 들어갔어요!")
voice = get(bot.voice_clients, guild=ctx.guild)
else:
await ctx.send("채널에 연결해주세요!")
#queue? make a list!
await ctx.send(f"{ctx.author.mention}, 잠시 기다려주세요!")
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '%(title)s.mp3',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '128',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.cache.remove()
dictMeta = ydl.extract_info(url, download=True)
iDuration = dictMeta["duration"]
global MusicName
MusicName = dictMeta["title"]
embed = discord.Embed(color=0xabcdef, description=f"{ctx.author.mention}님이 신청하신 곡을 재생할게요!")
embed.add_field(name="제목", value=f"{MusicName}", inline=False)
embed.add_field(name="동영상 길이", value=f"{iDuration}초", inline=False)
await ctx.send(embed=embed)
voice.play(discord.FFmpegPCMAudio(f'{MusicName}.mp3'))
#voice.is_playing()
bot.run(token)