Compare commits
3 commits
d4e3693be6
...
aacd7aa9fa
Author | SHA1 | Date | |
---|---|---|---|
aacd7aa9fa | |||
f294e8cee1 | |||
3780fad9ae |
5 changed files with 50 additions and 14 deletions
|
@ -41,7 +41,7 @@ export default {
|
||||||
const file = fs.statSync(`${os.tmpdir()}/${args.audio.name}.png`);
|
const file = fs.statSync(`${os.tmpdir()}/${args.audio.name}.png`);
|
||||||
const fileSize = (file.size / 1000000.0).toFixed(2);
|
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` });
|
interaction.editReply({ content: `Image file is ${fileSize} MB` });
|
||||||
return interaction.followUp({ files: [`${os.tmpdir()}/${args.audio.name}.png`] });
|
return interaction.followUp({ files: [`${os.tmpdir()}/${args.audio.name}.png`] });
|
||||||
},
|
},
|
||||||
|
|
|
@ -40,7 +40,7 @@ export default {
|
||||||
const file = fs.statSync(`${os.tmpdir()}/${args.img.name}.mp3`);
|
const file = fs.statSync(`${os.tmpdir()}/${args.img.name}.mp3`);
|
||||||
const fileSize = (file.size / 1000000.0).toFixed(2);
|
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` });
|
interaction.editReply({ content: `Audio file is ${fileSize} MB` });
|
||||||
return interaction.followUp({ files: [`${os.tmpdir()}/${args.img.name}.mp3`] });
|
return interaction.followUp({ files: [`${os.tmpdir()}/${args.img.name}.mp3`] });
|
||||||
},
|
},
|
||||||
|
|
|
@ -6,6 +6,7 @@ import utils from '../../utils/videos.js';
|
||||||
|
|
||||||
let client;
|
let client;
|
||||||
let cleanUp;
|
let cleanUp;
|
||||||
|
let maxFileSize;
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
|
@ -30,7 +31,9 @@ export default {
|
||||||
client = c;
|
client = c;
|
||||||
const url = args.url;
|
const url = args.url;
|
||||||
const format = args.format;
|
const format = args.format;
|
||||||
|
maxFileSize = await utils.getMaxFileSize(interaction.guild);
|
||||||
interaction.doCompress = args.compress;
|
interaction.doCompress = args.compress;
|
||||||
|
|
||||||
if (interaction.cleanUp) {
|
if (interaction.cleanUp) {
|
||||||
cleanUp = 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 });
|
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) {
|
if (format) {
|
||||||
let qualitys = await new Promise((resolve, reject) => {
|
let qualitys = await new Promise((resolve, reject) => {
|
||||||
exec(`./bin/yt-dlp "${url}" --print "%()j"`, (err, stdout, stderr) => {
|
exec(`./bin/yt-dlp "${url}" --print "%()j"`, (err, stdout, stderr) => {
|
||||||
|
@ -116,11 +128,13 @@ export default {
|
||||||
|
|
||||||
async function download(url, interaction, originalInteraction) {
|
async function download(url, interaction, originalInteraction) {
|
||||||
let format = 'bestvideo*+bestaudio/best';
|
let format = 'bestvideo*+bestaudio/best';
|
||||||
|
|
||||||
const Embed = new EmbedBuilder()
|
const Embed = new EmbedBuilder()
|
||||||
.setColor(interaction.member ? interaction.member.displayHexColor : 'Navy')
|
.setColor(interaction.member ? interaction.member.displayHexColor : 'Navy')
|
||||||
.setAuthor({ name: `Downloaded by ${interaction.user.tag}`, iconURL: interaction.user.displayAvatarURL(), url: url })
|
.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!` });
|
.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}`) {
|
if (interaction.customId === `downloadQuality${interaction.user.id}`) {
|
||||||
format = interaction.values[0];
|
format = interaction.values[0];
|
||||||
if (interaction.values[1]) format += '+' + interaction.values[1];
|
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));
|
const file = fs.readdirSync(os.tmpdir()).filter(fn => fn.startsWith(interaction.id));
|
||||||
let output = `${os.tmpdir()}/${file}`;
|
let output = `${os.tmpdir()}/${file}`;
|
||||||
|
|
||||||
const fileStat = fs.statSync(output);
|
|
||||||
const fileSize = fileStat.size / 1000000.0;
|
|
||||||
const compressInteraction = originalInteraction ? originalInteraction : interaction;
|
const compressInteraction = originalInteraction ? originalInteraction : interaction;
|
||||||
if (compressInteraction.doCompress) {
|
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' ];
|
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);
|
const codec = await utils.getVideoCodec(output);
|
||||||
|
|
||||||
if (bannedFormats.includes(codec)) {
|
if (bannedFormats.includes(codec)) {
|
||||||
console.log('Reencoding video');
|
|
||||||
const oldOutput = output;
|
const oldOutput = output;
|
||||||
output = `${os.tmpdir()}/264${file}`;
|
output = `${os.tmpdir()}/264${file}`;
|
||||||
await utils.ffmpeg(`-i ${oldOutput} -vcodec libx264 -acodec aac ${output}`);
|
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) {
|
if (fileSize > 100) {
|
||||||
await interaction.deleteReply();
|
await interaction.deleteReply();
|
||||||
await interaction.followUp('Uh oh! The video you tried to download is too big!', { ephemeral: true });
|
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)
|
const fileurl = await utils.upload(output)
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(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 });
|
await interaction.followUp({ content: fileurl, ephemeral: false });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -206,14 +223,19 @@ async function download(url, interaction, originalInteraction) {
|
||||||
async function compress(input, interaction, embed) {
|
async function compress(input, interaction, embed) {
|
||||||
const output = `compressed${input}.mp4`;
|
const output = `compressed${input}.mp4`;
|
||||||
// Delete the file as it apparently don't overwrite?
|
// 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])
|
utils.compressVideo(`${os.tmpdir()}/${input}`, output, interaction.values[0])
|
||||||
.then(async () => {
|
.then(async () => {
|
||||||
const fileStat = fs.statSync(`${os.tmpdir()}/${output}`);
|
const fileStat = fs.statSync(`${os.tmpdir()}/${output}`);
|
||||||
const fileSize = fileStat.size / 1000000.0;
|
const fileSize = fileStat.size / 1000000.0;
|
||||||
|
|
||||||
if (fileSize > utils.getMaxFileSize(interaction.guild)) {
|
embed.setAuthor({ name: `${embed.data.author.name} (${fileSize.toFixed(2)} MB)`, iconURL: embed.data.author.icon_url, url: embed.data.author.url });
|
||||||
await interaction.editReply({ content: 'File was bigger than 8 mb. but due to the compression it is not being uploaded externally.', ephemeral: true });
|
|
||||||
|
if (fileSize > maxFileSize) {
|
||||||
|
await interaction.editReply({ content: `File was bigger than ${maxFileSize} mb. It has been uploaded to an external site.`, ephemeral: false });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await interaction.editReply({ embeds: [embed], files: [`${os.tmpdir()}/${output}`], ephemeral: false });
|
await interaction.editReply({ embeds: [embed], files: [`${os.tmpdir()}/${output}`], ephemeral: false });
|
||||||
|
|
|
@ -46,7 +46,7 @@ export default {
|
||||||
await interaction.deleteReply();
|
await interaction.deleteReply();
|
||||||
await interaction.followUp('❌ Uh oh! The video once converted is too big!', { ephemeral: true });
|
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)
|
const fileURL = await utils.upload(gifsicleOutput)
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
|
@ -9,6 +9,7 @@ export default {
|
||||||
stringIsAValidurl,
|
stringIsAValidurl,
|
||||||
compressVideo,
|
compressVideo,
|
||||||
getVideoCodec,
|
getVideoCodec,
|
||||||
|
getVideoSize,
|
||||||
getMaxFileSize,
|
getMaxFileSize,
|
||||||
};
|
};
|
||||||
async function downloadVideo(urlArg, output, format = 'bestvideo*+bestaudio/best') {
|
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) => {
|
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;
|
const tier = guild.premiumTier;
|
||||||
console.log(tier);
|
|
||||||
switch (tier) {
|
switch (tier) {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
|
@ -109,7 +123,7 @@ async function getMaxFileSize(guild) {
|
||||||
resolve('100');
|
resolve('100');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
reject('An error has occured');
|
resolve('8');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue