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

2
.gitignore vendored
View File

@ -6,3 +6,5 @@ __pycache__
props props
!props/resources.json !props/resources.json
*.mp4 *.mp4
plugins/yt-dlp
tmp

1
bot.js
View File

@ -3,6 +3,7 @@ const path = require('path');
const fs = require('fs'); const fs = require('fs');
const Config = require('./props/config.json'); const Config = require('./props/config.json');
const { isOnSpamWatch } = require('./plugins/lib-spamwatch/spamwatch.js'); const { isOnSpamWatch } = require('./plugins/lib-spamwatch/spamwatch.js');
require('./plugins/ytdlp-wrapper.js');
// require('./plugins/termlogger.js'); // require('./plugins/termlogger.js');
const bot = new Telegraf(Config.botToken); const bot = new Telegraf(Config.botToken);

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 fs = require('fs');
const { getStrings } = require('../plugins/checklang'); const path = require('path');
async function DownloadFromYoutube(command) { 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'),
};
function getYtDlpPath() {
const platform = os.platform();
return ytDlpPaths[platform] || ytDlpPaths.linux;
};
async function downloadFromYoutube(command) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => { exec(command, (error, stdout, stderr) => {
if (error) { if (error) {
reject({ error, stdout, stderr }); reject({ error, stdout, stderr });
} else { } else {
resolve({ error, stdout, stderr }); resolve({ stdout, stderr });
} }
}); });
}); });
} };
module.exports = (bot) => { module.exports = (bot) => {
bot.command('yt', async (ctx) => { bot.command('yt', spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code); const strings = getStrings(ctx.from.language_code);
const args = ctx.message.text.split(' ').slice(1).join(' '); const ytDlpPath = getYtDlpPath();
const ytCommand = 'yt-dlp ' + args + ' -o video.mp4'; const userId = ctx.from.id;
ctx.reply(Strings.downloading); const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
await DownloadFromYoutube(ytCommand);
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 { try {
ctx.reply(Strings.uploading); await downloadFromYoutube(dlpCommand);
await ctx.replyWithVideo({ source: 'video.mp4' });
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) { } catch (error) {
ctx.reply('Error!') fs.unlinkSync(mp4File);
} const message = strings.ytDownloadErr
fs.unlinkSync('video.mp4'); .replace("{err}", error)
}) .replace("{userName}", ctx.from.first_name);
ctx.reply(message, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
} }
});
};

View File

@ -58,6 +58,8 @@
"goBack": "Back", "goBack": "Back",
"maInvalidModule": "Please provide a valid module ID from The Mod Archive.\nExample: `/modarchive 81574`", "maInvalidModule": "Please provide a valid module ID from The Mod Archive.\nExample: `/modarchive 81574`",
"maDownloadError": "Error downloading the file. Check the module ID and try again.", "maDownloadError": "Error downloading the file. Check the module ID and try again.",
"downloading": "Downloading...", "ytDownloading": "*Downloading video...*",
"uploading": "Uploading. Please, hold on..." "ytUploading": "*Uploading video...*",
"ytUploadDesc": "*[{userName}](tg://user?id={userId}), there is your downloaded video.*",
"ytDownloadErr": "*Error during YT video download:*\n\n`{err}`"
} }

View File

@ -58,6 +58,8 @@
"goBack": "Voltar", "goBack": "Voltar",
"maInvalidModule": "Por favor, forneça um ID de módulo válido do The Mod Archive.\nExemplo: `/modarchive 81574`", "maInvalidModule": "Por favor, forneça um ID de módulo válido do The Mod Archive.\nExemplo: `/modarchive 81574`",
"maDownloadError": "Erro ao baixar o arquivo. Verifique o ID do módulo e tente novamente.", "maDownloadError": "Erro ao baixar o arquivo. Verifique o ID do módulo e tente novamente.",
"downloading": "Fazendo download...", "ytDownloading": "*Baixando vídeo...*",
"uploading": "Fazendo upload. Por favor, aguarde um momento..." "ytUploading": "*Enviando video...*",
"ytUploadDesc": "*[{userName}](tg://user?id={userId}), aqui está o seu vídeo baixado.*",
"ytDownloadErr": "*Erro durante o download do vídeo do YT:*\n\n`{err}`"
} }

View File

@ -1,3 +1,3 @@
{ {
"ignore": ["props"] "ignore": ["props", "tmp"]
} }

37
package-lock.json generated
View File

@ -12,6 +12,7 @@
"axios": "^1.7.7", "axios": "^1.7.7",
"child_process": "^1.0.2", "child_process": "^1.0.2",
"commander": "^12.1.0", "commander": "^12.1.0",
"fluent-ffmpeg": "^2.1.3",
"node-html-parser": "^6.1.13", "node-html-parser": "^6.1.13",
"nodemon": "^3.1.4", "nodemon": "^3.1.4",
"telegraf": "^4.16.3", "telegraf": "^4.16.3",
@ -421,6 +422,36 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/fluent-ffmpeg": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/fluent-ffmpeg/-/fluent-ffmpeg-2.1.3.tgz",
"integrity": "sha512-Be3narBNt2s6bsaqP6Jzq91heDgOEaDCJAXcE3qcma/EJBSy5FB4cvO31XBInuAuKBx8Kptf8dkhjK0IOru39Q==",
"license": "MIT",
"dependencies": {
"async": "^0.2.9",
"which": "^1.1.1"
},
"engines": {
"node": ">=18"
}
},
"node_modules/fluent-ffmpeg/node_modules/async": {
"version": "0.2.10",
"resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz",
"integrity": "sha512-eAkdoKxU6/LkKDBzLpT+t6Ff5EtfSF4wx1WfJiPEEV7WNLnDaRXk0oVysiEPm262roaachGexwUv94WhSgN5TQ=="
},
"node_modules/fluent-ffmpeg/node_modules/which": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
"integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==",
"license": "ISC",
"dependencies": {
"isexe": "^2.0.0"
},
"bin": {
"which": "bin/which"
}
},
"node_modules/fn.name": { "node_modules/fn.name": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz", "resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz",
@ -577,6 +608,12 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/isexe": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
"license": "ISC"
},
"node_modules/kuler": { "node_modules/kuler": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/kuler/-/kuler-2.0.0.tgz", "resolved": "https://registry.npmjs.org/kuler/-/kuler-2.0.0.tgz",

View File

@ -12,6 +12,7 @@
"axios": "^1.7.7", "axios": "^1.7.7",
"child_process": "^1.0.2", "child_process": "^1.0.2",
"commander": "^12.1.0", "commander": "^12.1.0",
"fluent-ffmpeg": "^2.1.3",
"node-html-parser": "^6.1.13", "node-html-parser": "^6.1.13",
"nodemon": "^3.1.4", "nodemon": "^3.1.4",
"telegraf": "^4.16.3", "telegraf": "^4.16.3",

55
plugins/ytdlp-wrapper.js Normal file
View File

@ -0,0 +1,55 @@
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const os = require('os');
const downloadDir = path.resolve(__dirname, 'yt-dlp');
const urls = {
linux: 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp',
win32: 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe',
darwin: 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_macos',
};
function getDownloadUrl() {
const platform = os.platform();
return urls[platform] || urls.linux;
};
async function downloadYtDlp() {
const url = getDownloadUrl();
const fileName = url.split('/').pop();
const filePath = path.join(downloadDir, fileName);
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir, { recursive: true });
};
if (!fs.existsSync(filePath)) {
try {
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
});
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
writer.on('finish', () => {
if (os.platform() !== 'win32') {
fs.chmodSync(filePath, '755');
}
});
writer.on('error', (err) => {
console.error('WARN: yt-dlp download failed:', err);
});
} catch (err) {
console.error('WARN: yt-dlp download failed:', err.message);
};
};
};
downloadYtDlp();