From aacd7aa9facb240dcaa91dd54093af10a2a55c87 Mon Sep 17 00:00:00 2001 From: Supositware Date: Tue, 11 Apr 2023 20:31:25 +0200 Subject: [PATCH] Fix compression, add file size to author --- commands/utility/download.js | 38 ++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/commands/utility/download.js b/commands/utility/download.js index f96b96e9..da6ceda8 100644 --- a/commands/utility/download.js +++ b/commands/utility/download.js @@ -6,6 +6,7 @@ import utils from '../../utils/videos.js'; let client; let cleanUp; +let maxFileSize; export default { data: new SlashCommandBuilder() @@ -30,7 +31,9 @@ export default { client = c; const url = args.url; const format = args.format; + maxFileSize = await utils.getMaxFileSize(interaction.guild); interaction.doCompress = args.compress; + if (interaction.cleanUp) { cleanUp = interaction.cleanUp; } @@ -46,6 +49,15 @@ export default { return interaction.editReply({ content: '❌ This does not look like a valid url!', ephemeral: true }); } + const aproxFileSize = await utils.getVideoSize(url, format); + + if (aproxFileSize > 100 && !args.compress) { + await interaction.followUp('Uh oh! The video you tried to download is larger than 100 mb! Try again with compression.', { ephemeral: true }); + } + else if (aproxFileSize > 500) { + await interaction.followUp('Uh oh! The video you tried to download is larger than 500 mb!', { ephemeral: true }); + } + if (format) { let qualitys = await new Promise((resolve, reject) => { exec(`./bin/yt-dlp "${url}" --print "%()j"`, (err, stdout, stderr) => { @@ -116,11 +128,13 @@ export default { async function download(url, interaction, originalInteraction) { let format = 'bestvideo*+bestaudio/best'; + const Embed = new EmbedBuilder() .setColor(interaction.member ? interaction.member.displayHexColor : 'Navy') .setAuthor({ name: `Downloaded by ${interaction.user.tag}`, iconURL: interaction.user.displayAvatarURL(), url: url }) .setFooter({ text: `You can get the original video by clicking on the "Downloaded by ${interaction.user.tag}" message!` }); + if (interaction.customId === `downloadQuality${interaction.user.id}`) { format = interaction.values[0]; if (interaction.values[1]) format += '+' + interaction.values[1]; @@ -131,8 +145,6 @@ async function download(url, interaction, originalInteraction) { const file = fs.readdirSync(os.tmpdir()).filter(fn => fn.startsWith(interaction.id)); let output = `${os.tmpdir()}/${file}`; - const fileStat = fs.statSync(output); - const fileSize = fileStat.size / 1000000.0; const compressInteraction = originalInteraction ? originalInteraction : interaction; if (compressInteraction.doCompress) { const presets = [ 'Social 8 MB 3 Minutes 360p30', 'Social 50 MB 10 Minutes 480p30', 'Social 50 MB 5 Minutes 720p30', 'Social 100 MB 5 Minutes 1080p30' ]; @@ -172,22 +184,27 @@ async function download(url, interaction, originalInteraction) { const codec = await utils.getVideoCodec(output); if (bannedFormats.includes(codec)) { - console.log('Reencoding video'); const oldOutput = output; output = `${os.tmpdir()}/264${file}`; await utils.ffmpeg(`-i ${oldOutput} -vcodec libx264 -acodec aac ${output}`); } + const fileStat = fs.statSync(output); + const fileSize = fileStat.size / 1000000.0; + + Embed.setAuthor({ name: `${Embed.data.author.name} (${fileSize.toFixed(2)} MB)`, iconURL: Embed.data.author.icon_url, url: Embed.data.author.url }); + + if (fileSize > 100) { await interaction.deleteReply(); await interaction.followUp('Uh oh! The video you tried to download is too big!', { ephemeral: true }); } - else if (fileSize > utils.getMaxFileSize(interaction.guild)) { + else if (fileSize > maxFileSize) { const fileurl = await utils.upload(output) .catch(err => { console.error(err); }); - await interaction.editReply({ content: 'File was bigger than 8 mb. It has been uploaded to an external site.', embeds: [Embed], ephemeral: false }); + await interaction.editReply({ content: `File was bigger than ${maxFileSize} mb. It has been uploaded to an external site.`, embeds: [Embed], ephemeral: false }); await interaction.followUp({ content: fileurl, ephemeral: false }); } else { @@ -206,14 +223,19 @@ async function download(url, interaction, originalInteraction) { async function compress(input, interaction, embed) { const output = `compressed${input}.mp4`; // Delete the file as it apparently don't overwrite? - fs.rmSync(output); + if (fs.existsSync(output)) { + fs.rmSync(output); + } + utils.compressVideo(`${os.tmpdir()}/${input}`, output, interaction.values[0]) .then(async () => { const fileStat = fs.statSync(`${os.tmpdir()}/${output}`); const fileSize = fileStat.size / 1000000.0; - if (fileSize > utils.getMaxFileSize(interaction.guild)) { - await interaction.editReply({ content: 'File was bigger than 8 mb. but due to the compression it is not being uploaded externally.', ephemeral: true }); + embed.setAuthor({ name: `${embed.data.author.name} (${fileSize.toFixed(2)} MB)`, iconURL: embed.data.author.icon_url, url: embed.data.author.url }); + + if (fileSize > maxFileSize) { + await interaction.editReply({ content: `File was bigger than ${maxFileSize} mb. It has been uploaded to an external site.`, ephemeral: false }); } else { await interaction.editReply({ embeds: [embed], files: [`${os.tmpdir()}/${output}`], ephemeral: false });