Add a very basic ability to download audio only

This commit is contained in:
Matthew Adams
2021-03-13 18:00:12 +10:00
parent f08b4ac4df
commit 2b0c005f8b
3 changed files with 47 additions and 9 deletions

10
.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
.env*
*.pyc
*.mp4
*.mp3
*.mkv
.vscode/*
.vs/*
__*

View File

@@ -2,7 +2,7 @@ import youtube_dl
def download(videoUrl): def download(videoUrl):
response = {'fileName': '', 'duration': 0, 'messages': ''} response = {'fileName': '', 'duration': 0, 'messages': ''}
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.mp4'}) ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.mp4', 'merge_output_format': 'mp4'})
with ydl: with ydl:

44
main.py
View File

@@ -15,21 +15,49 @@ async def on_ready():
@client.event @client.event
async def on_message(message): async def on_message(message):
# Only do anything in TikTok channels
if(not message.channel.name.startswith("tik-tok")):
return
# Ignore our own messages # Ignore our own messages
if message.author == client.user: if message.author == client.user:
return return
fileName = ""
duration = 0
messages = ""
# Do special things in DMs
if(type(message.channel) is discord.DMChannel):
if message.content.startswith('🎵'):
url = message.content.replace('🎵', '')
downloadResponse = download(url)
fileName = downloadResponse['fileName']
duration = downloadResponse['duration']
messages = downloadResponse['messages']
print("Downloaded: " + fileName + " For User: " + str(message.author))
if(messages.startswith("Error")):
await message.author.send('TikBot has failed you. Consider berating my human if this was not expected.\nMessage: ' + messages)
return
audioFilename = "audio_" + fileName + ".mp3"
ffmpeg.input(fileName).output(audioFilename, **{'b:a': '320k', 'threads': '1'}).run()
with open(audioFilename, 'rb') as fp:
await message.author.send(file=discord.File(fp, str(audioFilename)))
# Delete the compressed and original file
os.remove(fileName)
os.remove(audioFilename)
else:
await message.author.send('👋')
return
# Only do anything in TikTok channels
if(not message.channel.name.startswith("tik-tok")):
return
# Be polite! # Be polite!
if message.content.startswith('$hello'): if message.content.startswith('$hello'):
await message.channel.send('Hello!') await message.channel.send('Hello!')
fileName = ""
duration = 0
messages = ""
if message.content.startswith('https'): if message.content.startswith('https'):
await message.channel.send('TikBot downloading video now!') await message.channel.send('TikBot downloading video now!')
@@ -41,7 +69,7 @@ async def on_message(message):
print("Downloaded: " + fileName + " For User: " + str(message.author)) print("Downloaded: " + fileName + " For User: " + str(message.author))
if(messages.startswith("Error")): if(messages.startswith("Error")):
await message.channel.send('TikBot has failed you. Consider berating my human if this was not expected.\Message: ' + messages) await message.channel.send('TikBot has failed you. Consider berating my human if this was not expected.\nMessage: ' + messages)
return return
else: else:
return return