mirror of
https://github.com/thewesker/Video-Archive-Discord-Bot.git
synced 2025-12-20 04:11:05 -05:00
Add fun messages, improve video quality, error handling
This commit is contained in:
17
compressionMessages.py
Normal file
17
compressionMessages.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from random import randrange
|
||||||
|
|
||||||
|
messages = [
|
||||||
|
"What a chungus of a TikTok, compressing it for you. Smaller boi incoming soon.",
|
||||||
|
"TikBot will compress your TikTok for you as it is too chonky, please give me a sec.",
|
||||||
|
"Taking your big TikTok and making it smaller. Just a moment.",
|
||||||
|
"Compressing TikTok to fit in Discord's size limitation, please wait a moment.",
|
||||||
|
"Your TikTok needs to lay off the Maccas, performing digital liposuction.",
|
||||||
|
"That's a PATT (Phat Ass Tik Tok), squeezing it through the door for ya.",
|
||||||
|
"That's a big TikTok, are you compensating for something? Compressing it now.",
|
||||||
|
"Flat Is Justice. Making your video conform.",
|
||||||
|
"Thick thighs save lives, but this thick-tok won't fit into these Discord jeans. Shrinking it down.",
|
||||||
|
]
|
||||||
|
|
||||||
|
def getCompressionMessage():
|
||||||
|
number = randrange(len(messages))
|
||||||
|
return messages[number]
|
||||||
@@ -1,26 +1,29 @@
|
|||||||
import youtube_dl
|
import youtube_dl
|
||||||
|
|
||||||
def download(videoUrl):
|
def download(videoUrl):
|
||||||
|
response = {'fileName': '', 'duration': 0, 'messages': ''}
|
||||||
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.mp4'})
|
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.mp4'})
|
||||||
|
|
||||||
ydl_opts = {}
|
|
||||||
|
|
||||||
with ydl:
|
with ydl:
|
||||||
result = ydl.extract_info(
|
try:
|
||||||
videoUrl,
|
result = ydl.extract_info(
|
||||||
download=True
|
videoUrl,
|
||||||
)
|
download=True
|
||||||
|
)
|
||||||
|
except:
|
||||||
|
response['messages'] = 'Error: Download Failed'
|
||||||
|
return response
|
||||||
|
|
||||||
if 'entries' in result:
|
if 'entries' in result:
|
||||||
# Can be a playlist or a list of videos
|
# Can be a playlist or a list of videos
|
||||||
video = result['entries'][0]
|
response['messages'] = 'Error: More than 1 result found. Please supply a single video only.'
|
||||||
return "Error: More than 1 result found"
|
return response
|
||||||
else:
|
else:
|
||||||
# Just a video
|
# Just a video
|
||||||
video = result
|
video = result
|
||||||
|
|
||||||
print(video)
|
response['duration'] = video['duration']
|
||||||
video_url = video['title']
|
response['fileName'] = video['id'] + ".mp4"
|
||||||
videoId = video['id']
|
|
||||||
|
|
||||||
return videoId + ".mp4"
|
return response
|
||||||
|
|||||||
47
main.py
47
main.py
@@ -3,6 +3,7 @@ import os
|
|||||||
import ffmpeg
|
import ffmpeg
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from downloader import download
|
from downloader import download
|
||||||
|
from compressionMessages import getCompressionMessage
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@@ -14,44 +15,60 @@ 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")):
|
if(not message.channel.name.startswith("tik-tok")):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Ignore our own messages
|
||||||
if message.author == client.user:
|
if message.author == client.user:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Be polite!
|
||||||
if message.content.startswith('$hello'):
|
if message.content.startswith('$hello'):
|
||||||
await message.channel.send('Hello!')
|
await message.channel.send('Hello!')
|
||||||
|
|
||||||
downloadResult = ""
|
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!')
|
||||||
downloadResult = download(message.content)
|
downloadResponse = download(message.content)
|
||||||
print(downloadResult)
|
fileName = downloadResponse['fileName']
|
||||||
|
duration = downloadResponse['duration']
|
||||||
|
messages = downloadResponse['messages']
|
||||||
|
|
||||||
if(downloadResult.startswith("Error")):
|
print("Downloaded: " + fileName + " For User: " + str(message.author))
|
||||||
await message.channel.send('TikBot has failed you. Consider berating my human if this was not expected.')
|
|
||||||
|
if(messages.startswith("Error")):
|
||||||
|
await message.channel.send('TikBot has failed you. Consider berating my human if this was not expected.\Message: ' + messages)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check file size, if it's small enough just send it!
|
# Check file size, if it's small enough just send it!
|
||||||
fileSize = os.stat(downloadResult).st_size
|
fileSize = os.stat(fileName).st_size
|
||||||
|
|
||||||
if(fileSize < 8000000):
|
if(fileSize < 8000000):
|
||||||
with open(downloadResult, 'rb') as fp:
|
with open(fileName, 'rb') as fp:
|
||||||
await message.channel.send(file=discord.File(fp, str(downloadResult)))
|
await message.channel.send(file=discord.File(fp, str(fileName)))
|
||||||
os.remove(downloadResult)
|
os.remove(fileName)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# We need to compress the file below 8MB or discord will make a sad
|
# We need to compress the file below 8MB or discord will make a sad
|
||||||
await message.channel.send('TikBot will compress your video for you as it is too chonky, please give me a sec.')
|
compressionMessage = getCompressionMessage()
|
||||||
ffmpeg.input(downloadResult).output("small_" + downloadResult, **{'b:v': '1000k'}).run()
|
await message.channel.send(compressionMessage)
|
||||||
with open("small_" + downloadResult, 'rb') as fp:
|
print("Duration = " + str(duration))
|
||||||
await message.channel.send(file=discord.File(fp, str("small_" + downloadResult)))
|
# Give us 7MB files with VBR encoding to allow for some overhead
|
||||||
|
bitrateKilobits = (7000 * 8)/duration
|
||||||
|
bitrateKilobits = round(bitrateKilobits)
|
||||||
|
print("Calced bitrate = " + str(bitrateKilobits))
|
||||||
|
ffmpeg.input(fileName).output("small_" + fileName, **{'b:v': str(bitrateKilobits) + 'k'}).run()
|
||||||
|
with open("small_" + fileName, 'rb') as fp:
|
||||||
|
await message.channel.send(file=discord.File(fp, str("small_" + fileName)))
|
||||||
# Delete the compressed and original file
|
# Delete the compressed and original file
|
||||||
os.remove(downloadResult)
|
os.remove(fileName)
|
||||||
os.remove("small_" + downloadResult)
|
os.remove("small_" + fileName)
|
||||||
|
|
||||||
|
|
||||||
client.run(os.getenv('TOKEN'))
|
client.run(os.getenv('TOKEN'))
|
||||||
|
|||||||
Reference in New Issue
Block a user