From 3aa22a6b90032bbe3b12c56934a357e6d283f8f1 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Date: Sat, 28 Sep 2024 22:01:14 -0300 Subject: [PATCH] Workaround for 50MB+ video (pt. 2) --- commands/youtube.js | 88 ++++++++++++++++++++++++----------------- locales/english.json | 1 + locales/portuguese.json | 1 + 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/commands/youtube.js b/commands/youtube.js index c49cb55..53fb7f2 100644 --- a/commands/youtube.js +++ b/commands/youtube.js @@ -59,9 +59,7 @@ module.exports = (bot) => { 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; - const dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File]; const downloadingMessage = await ctx.reply(strings.ytDownloading, { parse_mode: 'Markdown', @@ -70,51 +68,67 @@ module.exports = (bot) => { try { const approxSizeInMB = await getApproxSize(ytDlpPath, videoUrl); + let videoFormat = ""; 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, - }); + videoFormat = `-f best`; } else { - await downloadFromYoutube(dlpCommand, dlpArgs); + videoFormat = "-f bestvideo+bestaudio"; + } - await ctx.telegram.editMessageText( - ctx.chat.id, - downloadingMessage.message_id, - null, - strings.ytUploading, { - parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id, - }); + const dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File]; + 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, + }); - 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, - }); - } + if (fs.existsSync(mp4File)) { + const message = strings.ytUploadDesc + .replace("{userId}", userId) + .replace("{userName}", ctx.from.first_name); + + try { + await ctx.replyWithVideo({ + source: mp4File, + caption: `${message}`, + parse_mode: 'Markdown', + }); + + await ctx.telegram.editMessageText( + ctx.chat.id, + downloadingMessage.message_id, + null, + strings.ytUploadLimit2, { + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }); + + fs.unlinkSync(mp4File); + } catch (error) { + await ctx.reply(`\`${error}\``, { + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }); } } - } catch (downloadError) { + } catch (error) { fs.unlinkSync(mp4File); + let errStatus = ""; + + if (error == "Error: 413: Request Entity Too Large") { + errStatus = Strings.ytUploadLimit; + } else { + errStatus = error.error ? error.error.message : 'Unknown error'; + } + const message = strings.ytDownloadErr - .replace("{err}", downloadError.error ? downloadError.error.message : 'Unknown error') + .replace("{err}", errStatus) .replace("{userName}", ctx.from.first_name); await ctx.reply(message, { diff --git a/locales/english.json b/locales/english.json index 77aed33..48c7864 100644 --- a/locales/english.json +++ b/locales/english.json @@ -63,6 +63,7 @@ "ytUploadDesc": "*[{userName}](tg://user?id={userId}), there is your downloaded video.*", "ytDownloadErr": "*Error during YT video download:*\n\n`{err}`", "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.*", + "ytUploadLimit2": "*This video had its quality reduced because it exceeded the 50MB limit for uploads imposed by Telegram.*", "fileError": "Error uploading file", "botUpdated": "Bot updated with success.", "errorUpdatingBot": "Error updating bot\n\n{error}" diff --git a/locales/portuguese.json b/locales/portuguese.json index c4e63cf..26bf445 100644 --- a/locales/portuguese.json +++ b/locales/portuguese.json @@ -63,6 +63,7 @@ "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": "*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.*", + "ytUploadLimit2": "*Esse vídeo teve a qualidade reduzida por estar excedendo o limite de 50MB para uploads imposto pelo Telegram.*", "fileError": "Erro ao enviar o arquivo", "botUpdated": "Bot atualizado com sucesso.", "errorUpdatingBot": "Erro ao atualizar o bot\n\n{error}"