Fix compression, add file size to author

This commit is contained in:
Supositware 2023-04-11 20:31:25 +02:00
parent f294e8cee1
commit aacd7aa9fa

View file

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