Compare commits

..

3 commits

Author SHA1 Message Date
aacd7aa9fa Fix compression, add file size to author 2023-04-11 20:31:25 +02:00
f294e8cee1 getVideoSize 2023-04-11 20:16:18 +02:00
3780fad9ae await getMaxFIleSize 2023-04-11 20:16:07 +02:00
5 changed files with 50 additions and 14 deletions

View file

@ -41,7 +41,7 @@ export default {
const file = fs.statSync(`${os.tmpdir()}/${args.audio.name}.png`);
const fileSize = (file.size / 1000000.0).toFixed(2);
if (fileSize > utils.getMaxFileSize(interaction.guild)) return interaction.editReply('error');
if (fileSize > await utils.getMaxFileSize(interaction.guild)) return interaction.editReply('error');
interaction.editReply({ content: `Image file is ${fileSize} MB` });
return interaction.followUp({ files: [`${os.tmpdir()}/${args.audio.name}.png`] });
},

View file

@ -40,7 +40,7 @@ export default {
const file = fs.statSync(`${os.tmpdir()}/${args.img.name}.mp3`);
const fileSize = (file.size / 1000000.0).toFixed(2);
if (fileSize > utils.getMaxFileSize(interaction.guild)) return interaction.editReply('error');
if (fileSize > await utils.getMaxFileSize(interaction.guild)) return interaction.editReply('error');
interaction.editReply({ content: `Audio file is ${fileSize} MB` });
return interaction.followUp({ files: [`${os.tmpdir()}/${args.img.name}.mp3`] });
},

View file

@ -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?
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 });

View file

@ -46,7 +46,7 @@ export default {
await interaction.deleteReply();
await interaction.followUp('❌ Uh oh! The video once converted is too big!', { ephemeral: true });
}
else if (fileSize > utils.getMaxFileSize(interaction.guild)) {
else if (fileSize > await utils.getMaxFileSize(interaction.guild)) {
const fileURL = await utils.upload(gifsicleOutput)
.catch(err => {
console.error(err);

View file

@ -9,6 +9,7 @@ export default {
stringIsAValidurl,
compressVideo,
getVideoCodec,
getVideoSize,
getMaxFileSize,
};
async function downloadVideo(urlArg, output, format = 'bestvideo*+bestaudio/best') {
@ -93,10 +94,23 @@ async function getVideoCodec(input) {
});
}
async function getMaxFileSize(guild) {
async function getVideoSize(urlArg, format = 'bestvideo*+bestaudio/best') {
return await new Promise((resolve, reject) => {
exec(`yt-dlp ${urlArg} -f ${format} -O "%(filesize,filesize_approx)s"`, (err, stdout, stderr) => {
if (err) {
reject(stderr);
}
if (stderr) {
console.error(stderr);
}
resolve(stdout / 1000000.0);
});
});
}
async function getMaxFileSize(guild) {
return await new Promise((resolve) => {
const tier = guild.premiumTier;
console.log(tier);
switch (tier) {
case 0:
case 1:
@ -109,7 +123,7 @@ async function getMaxFileSize(guild) {
resolve('100');
break;
default:
reject('An error has occured');
resolve('8');
break;
}
});