Compare commits

..

2 commits

Author SHA1 Message Date
a7cc663a4d Show more dependency in the readme 2022-08-22 20:25:38 +02:00
1dbcb83029 Compress video option 2022-08-22 20:23:27 +02:00
4 changed files with 86 additions and 12 deletions

View file

@ -14,8 +14,12 @@ export default {
.setDescription('url of the video you want to download.')
.setRequired(true))
.addBooleanOption(option =>
option.setName('advanced')
option.setName('format')
.setDescription('Choose the quality of the video.')
.setRequired(false))
.addBooleanOption(option =>
option.setName('compress')
.setDescription('Compress the video?')
.setRequired(false)),
async execute(interaction) {
@ -27,7 +31,7 @@ export default {
return interaction.editReply({ content: '❌ This does not look like a valid url!', ephemeral: true });
}
if (interaction.options.getBoolean('advanced')) {
if (interaction.options.getBoolean('format')) {
let qualitys = await new Promise((resolve, reject) => {
exec(`./bin/yt-dlp "${url}" --print "%()j"`, (err, stdout, stderr) => {
if (err) {
@ -84,7 +88,7 @@ export default {
if (!interactionMenu.isSelectMenu()) return;
if (interactionMenu.customId === 'downloadQuality') {
await interactionMenu.deferReply({ ephemeral: false });
download(url, interactionMenu);
download(url, interactionMenu, interaction);
}
});
return;
@ -93,7 +97,7 @@ export default {
},
};
async function download(url, interaction) {
async function download(url, interaction, originalInteraction) {
let format = 'bestvideo*+bestaudio/best';
const Embed = new MessageEmbed()
.setColor(interaction.member ? interaction.member.displayHexColor : 'NAVY')
@ -112,6 +116,37 @@ async function download(url, interaction) {
const fileStat = fs.statSync(output);
const fileSize = fileStat.size / 1000000.0;
const compressInteraction = originalInteraction ? originalInteraction : interaction;
if (compressInteraction.options.getBoolean('compress')) {
const presets = [ 'Discord Tiny 5 Minutes 240p30', 'Discord Small 2 Minutes 360p30', 'Discord Nitro Small 10-20 Minutes 480p30', 'Discord Nitro Medium 5-10 Minutes 720p30', 'Discord Nitro Large 3-6 Minutes 1080p30' ];
const options = [];
presets.forEach(p => {
options.push({
label: p,
value: p,
});
});
const row = new MessageActionRow()
.addComponents(
new MessageSelectMenu()
.setCustomId('preset')
.setPlaceholder('Nothing selected')
.addOptions(options),
);
await interaction.deleteReply();
await interaction.followUp({ content: 'Which compression preset do you want?', ephemeral: true, components: [row] });
interaction.client.once('interactionCreate', async (interactionMenu) => {
if (!interactionMenu.isSelectMenu()) return;
if (interactionMenu.customId === 'preset') {
await interactionMenu.deferReply({ ephemeral: false });
compress(file, interactionMenu, Embed);
}
});
return;
}
if (fileSize > 100) {
await interaction.deleteReply();
@ -136,3 +171,19 @@ async function download(url, interaction) {
});
return;
}
async function compress(input, interaction, embed) {
const output = `compressed${input}.mp4`;
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 > 8) {
await interaction.editReply({ content: 'File was bigger than 8 mb. but due to the compression it is not being uploaded externally.', ephemeral: true });
}
else {
await interaction.editReply({ embeds: [embed], files: [`${os.tmpdir()}/${output}`], ephemeral: false });
}
});
}

View file

@ -12,9 +12,10 @@ You need to install the following
* ffmpeg (Optional but very recommanded: for yt-dlp to merge video/audio formats)
* yt-dlp ([a file can download it for you](prereq.js))
* gifsicle (For [vid2gif](commands/vid2gif.js))
* giski (For [vid2gif](commands/vid2gif.js))
* yt-dlp ([a file can download it for you](prereq.cjs))
* HandBrakeCLI (For [download](commands/utility/download.js))
* gifsicle (For [vid2gif](commands/utility/vid2gif.js))
* gifki (For [vid2gif](commands/utility/vid2gif.js))
* Somewhere to upload files larger than 8 mb (I use a self hosted [XBackBone](https://github.com/SergiX44/XBackBone/) with the upload.sh script made from it, you can use anything else just need to be located in bin/upload.sh)
### Installing
@ -25,8 +26,8 @@ git checkout slash
npm install
```
To run the bot for the first time you need to execute [deploy-guild-commands.js](deploy-guild-commands.js) so the commands can be registered, don't forget to set your .env accordingly.
``node deploy-commands.cjs``
To run the bot for the first time you need to execute [deploy-commands.js](scripts/deploy-commands.js) so the commands can be registered, don't forget to set your .env accordingly.
``node scripts/deploy-commands.cjs``
then you can just run it normally.
``node index.js``
@ -43,6 +44,9 @@ If you are on linux and don't need automatic restart on crash you can just do
* [Discord.JS](https://github.com/discordjs/discord.js)
* [yt-dlp](https://github.com/yt-dlp/yt-dlp)
* [HandBrakeCLI](https://github.com/HandBrake/HandBrake)
* [gifsicle](https://github.com/kohler/gifsicle)
* [gifski](https://github.com/ImageOptim/gifski)
## Authors
@ -50,7 +54,7 @@ If you are on linux and don't need automatic restart on crash you can just do
## Donation link
[![Paypal](https://www.paypalobjects.com/en_US/CH/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/paypalme2/supositware/)
https://libtar.de/donate.html
## License

View file

@ -11,14 +11,18 @@ const commands = [
new SlashCommandBuilder()
.setName('download')
.setDescription('Download a video. (100 mb max)')
.setDescription('Download a video.')
.addStringOption(option =>
option.setName('url')
.setDescription('URL of the video you want to download.')
.setDescription('url of the video you want to download.')
.setRequired(true))
.addBooleanOption(option =>
option.setName('advanced')
.setDescription('Choose the quality of the video.')
.setRequired(false))
.addBooleanOption(option =>
option.setName('compress')
.setDescription('Compress the video?')
.setRequired(false)),

View file

@ -6,6 +6,7 @@ export default {
upload,
ffmpeg,
stringIsAValidurl,
compressVideo,
};
async function downloadVideo(urlArg, output, format = 'bestvideo*+bestaudio/best') {
await new Promise((resolve, reject) => {
@ -58,3 +59,17 @@ async function stringIsAValidurl(s) {
return false;
}
}
async function compressVideo(input, output, preset) {
await new Promise((resolve, reject) => {
exec(`./bin/HandBrakeCLI -i '${input}' -Z '${preset}' -o '${os.tmpdir()}/${output}'`, (err, stdout, stderr) => {
if (err) {
reject(stderr);
}
if (stderr) {
console.error(stderr);
}
resolve(stdout);
});
});
}