Workaround for 50MB+ videos

This commit is contained in:
Lucas Gabriel 2024-09-28 21:35:05 -03:00
parent cdf7446647
commit 26b6d3fdc4
No known key found for this signature in database
GPG Key ID: D9B075FC6DC93985
3 changed files with 60 additions and 44 deletions

View File

@ -29,8 +29,29 @@ async function downloadFromYoutube(command, args) {
});
};
async function getApproxSize(command, videoUrl) {
const args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx'];
return new Promise((resolve, reject) => {
execFile(command, args, (error, stdout, stderr) => {
if (error) {
reject({ error, stdout, stderr });
} else {
const sizeInBytes = parseInt(stdout.trim(), 10);
if (!isNaN(sizeInBytes)) {
const sizeInMB = sizeInBytes / (1024 * 1024);
resolve(sizeInMB);
} else {
reject(new Error('Invalid size received from yt-dlp'));
}
}
});
});
};
module.exports = (bot) => {
bot.command('yt', spamwatchMiddleware, async (ctx) => {
bot.command(['yt', 'ytdl'], spamwatchMiddleware, async (ctx) => {
const strings = getStrings(ctx.from.language_code);
const ytDlpPath = getYtDlpPath();
const userId = ctx.from.id;
@ -48,57 +69,52 @@ module.exports = (bot) => {
});
try {
await downloadFromYoutube(dlpCommand, dlpArgs);
const approxSizeInMB = await getApproxSize(ytDlpPath, videoUrl);
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 (approxSizeInMB >= 50) {
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
strings.ytUploadLimit, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
} else {
await downloadFromYoutube(dlpCommand, dlpArgs);
if (fs.existsSync(mp4File)) {
const message = strings.ytUploadDesc
.replace("{userId}", userId)
.replace("{userName}", ctx.from.first_name);
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
strings.ytUploading, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
fs.stat(mp4File, async (err, stats) => {
if (err) {
console.error(err);
return;
};
if (fs.existsSync(mp4File)) {
const message = strings.ytUploadDesc
.replace("{userId}", userId)
.replace("{userName}", ctx.from.first_name);
const mbSize = stats.size / (1024 * 1024);
if (mbSize > 50) {
await ctx.reply(strings.ytUploadLimit, {
try {
await ctx.replyWithVideo({
source: mp4File,
caption: `${message}`,
parse_mode: 'Markdown',
});
} catch (error) {
await ctx.reply(`\`${error}\``, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
fs.unlinkSync(mp4File);
} else {
try {
await ctx.replyWithVideo({
source: mp4File,
caption: `${message}`,
parse_mode: 'Markdown',
});
} catch (error) {
await ctx.reply(`\`${error}\``, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
};
};
});
};
}
}
}
} catch (downloadError) {
fs.unlinkSync(mp4File);
const message = strings.ytDownloadErr
.replace("{err}", error)
.replace("{err}", downloadError.error ? downloadError.error.message : 'Unknown error')
.replace("{userName}", ctx.from.first_name);
await ctx.reply(message, {

View File

@ -62,7 +62,7 @@
"ytUploading": "*Uploading video...*",
"ytUploadDesc": "*[{userName}](tg://user?id={userId}), there is your downloaded video.*",
"ytDownloadErr": "*Error during YT video download:*\n\n`{err}`",
"ytUploadLimit": "*You have reached the 50MB upload limit for the bot. Try another video. We're trying our best to increase the limit.*",
"ytUploadLimit": "*This video exceeds the 50 MB upload limit imposed by Telegram on our bot. Please try another video. We're doing our best to increase this limit.*",
"fileError": "Error uploading file",
"botUpdated": "Bot updated with success.",
"errorUpdatingBot": "Error updating bot\n\n{error}"

View File

@ -62,7 +62,7 @@
"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}`",
"ytUploadLimit": "*Você atingiu o limite de upload de 50MB para o bot. Tente outro vídeo. Estamos tentando o máximo para aumentar o limite.*",
"ytUploadLimit": "*Este vídeo excede o limite de carregamento de 50 MB imposto pelo Telegram ao nosso bot. Por favor, tente outro vídeo. Estamos fazendo o possível para aumentar esse limite.*",
"fileError": "Erro ao enviar o arquivo",
"botUpdated": "Bot atualizado com sucesso.",
"errorUpdatingBot": "Erro ao atualizar o bot\n\n{error}"