Mega fix on YouTube video downloader

This commit is contained in:
Lucas Gabriel 2024-09-28 17:24:27 -03:00
parent 2d9cb55a87
commit 37cb595999
No known key found for this signature in database
GPG key ID: D9B075FC6DC93985
9 changed files with 188 additions and 33 deletions

View file

@ -1,33 +1,88 @@
var exec = require('child_process').exec;
const { getStrings } = require('../plugins/checklang.js');
const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js');
const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch);
const { exec } = require('child_process');
const os = require('os');
const fs = require('fs');
const { getStrings } = require('../plugins/checklang');
const path = require('path');
async function DownloadFromYoutube(command) {
return new Promise((resolve, reject) => {
const ytDlpPaths = {
linux: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp'),
win32: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp.exe'),
darwin: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp_macos'),
};
exec(command, (error, stdout, stderr) => {
if (error) {
reject({ error, stdout, stderr });
} else {
resolve({ error, stdout, stderr });
}
});
function getYtDlpPath() {
const platform = os.platform();
return ytDlpPaths[platform] || ytDlpPaths.linux;
};
async function downloadFromYoutube(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject({ error, stdout, stderr });
} else {
resolve({ stdout, stderr });
}
});
}
});
};
module.exports = (bot) => {
bot.command('yt', async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const args = ctx.message.text.split(' ').slice(1).join(' ');
const ytCommand = 'yt-dlp ' + args + ' -o video.mp4';
ctx.reply(Strings.downloading);
await DownloadFromYoutube(ytCommand);
try {
ctx.reply(Strings.uploading);
await ctx.replyWithVideo({ source: 'video.mp4' });
} catch (error) {
ctx.reply('Error!')
}
fs.unlinkSync('video.mp4');
})
}
bot.command('yt', spamwatchMiddleware, async (ctx) => {
const strings = getStrings(ctx.from.language_code);
const ytDlpPath = getYtDlpPath();
const userId = ctx.from.id;
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
const mp4File = `tmp/${userId}.mp4`;
const cmdArgs = "--max-filesize 2G --no-playlist --merge-output-format mp4 -o";
const videoFormat = "-f bestvideo+bestaudio";
const dlpCommand = `${ytDlpPath} ${videoUrl} ${videoFormat} ${cmdArgs} ${mp4File}`;
const downloadingMessage = await ctx.reply(strings.ytDownloading, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
try {
await downloadFromYoutube(dlpCommand);
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
strings.ytUploading, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
if (fs.existsSync(mp4File)) {
const userId = parseInt(ctx.match[2]);
const userName = ctx.from.first_name;
const message = strings.ytUploadDesc
.replace("{userId}", userId)
.replace("{userName}", userName);
await ctx.replyWithVideo({
source: mp4File,
caption: `${message}`,
parse_mode: 'Markdown'
});
fs.unlinkSync(mp4File);
}
} catch (error) {
fs.unlinkSync(mp4File);
const message = strings.ytDownloadErr
.replace("{err}", error)
.replace("{userName}", ctx.from.first_name);
ctx.reply(message, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
}
});
};