youtube: add function that downloads in case ffmpeg doesn't work properly on yt-dlp (#23)

Co-authored-by: GiovaniFZ <giovanifinazzi@gmail.com>
This commit is contained in:
Lucas Gabriel 2024-12-08 18:13:10 -03:00 committed by GitHub
parent 5efb8cab58
commit db21fe2a21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 480 additions and 255 deletions

2
.gitignore vendored
View File

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

View File

@ -1,58 +0,0 @@
const { exec } = require('child_process');
const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js');
const path = require('path');
const fs = require('fs');
const os = require('os');
const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch);
const { getStrings } = require('../plugins/checklang.js');
const ffmpegPaths = {
linux: '/usr/bin/ffmpeg',
win32: path.resolve(__dirname, '../plugins/ffmpeg/ffmpeg.exe'),
};
const getFfmpegPath = () => {
const platform = os.platform();
return ffmpegPaths[platform] || ffmpegPaths.linux;
};
async function getVideo(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(`Error: ${stderr}`);
} else {
resolve(stdout.trim());
}
})
})
}
module.exports = (bot) => {
const ffmpegPath = getFfmpegPath();
bot.command('getvideo', spamwatchMiddleware, async (ctx) => {
const strings = getStrings(ctx.from.language_code);
const userId = ctx.from.id;
const mp4File = userId + '.f137.mp4';
const webmFile = userId + '.f251.webm';
const ffmpegCommand = 'cd tmp && ' + ffmpegPath + ' -i ' + mp4File + ' -i ' + webmFile + ' -c:v copy -c:a copy -strict -2 output.mp4';
await getVideo(ffmpegCommand);
const message = strings.ytUploadDesc
.replace("{userId}", userId)
.replace("{userName}", ctx.from.first_name);
try {
await ctx.replyWithVideo({
source: 'tmp/output.mp4',
caption: `${message}`,
parse_mode: 'Markdown',
});
} catch (error) {
ctx.reply(error, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}
// Delete tmp folder
fs.rmSync('tmp', { recursive: true, force: true })
});
}

View File

@ -17,6 +17,17 @@ const getYtDlpPath = () => {
return ytDlpPaths[platform] || ytDlpPaths.linux;
};
const ffmpegPaths = {
linux: '/usr/bin/ffmpeg',
win32: path.resolve(__dirname, '../plugins/ffmpeg/bin/ffmpeg.exe'),
};
const getFfmpegPath = () => {
const platform = os.platform();
return ffmpegPaths[platform] || ffmpegPaths.linux;
};
const downloadFromYoutube = async (command, args) => {
return new Promise((resolve, reject) => {
execFile(command, args, (error, stdout, stderr) => {
@ -44,14 +55,6 @@ const getApproxSize = async (command, videoUrl) => {
}
};
const timeoutPromise = (timeout) => {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Timeout: Check took too long'));
}, timeout);
});
};
module.exports = (bot) => {
bot.command(['yt', 'ytdl'], spamwatchMiddleware, async (ctx) => {
const strings = getStrings(ctx.from.language_code);
@ -60,8 +63,12 @@ module.exports = (bot) => {
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
const mp4File = `tmp/${userId}.mp4`;
const tempMp4File = `tmp/${userId}.f137.mp4`;
const tempWebmFile = `tmp/${userId}.f251.webm`;
const cmdArgs = "--max-filesize 2G --no-playlist --cookies props/cookies.txt --merge-output-format mp4 -o";
const dlpCommand = ytDlpPath;
const ffmpegPath = getFfmpegPath();
const ffmpegArgs = ['-i', tempMp4File, '-i', tempWebmFile, '-c:v copy -c:a copy -strict -2', mp4File];
try {
const downloadingMessage = await ctx.reply(strings.ytCheckingSize, {
@ -74,8 +81,6 @@ module.exports = (bot) => {
getApproxSize(ytDlpPath, videoUrl)
]);
let videoFormat = approxSizeInMB >= 50 ? '-f best' : "-f bestvideo+bestaudio";
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
@ -86,7 +91,7 @@ module.exports = (bot) => {
},
);
const dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File];
const dlpArgs = [videoUrl, ...cmdArgs.split(' '), mp4File];
await downloadFromYoutube(dlpCommand, dlpArgs);
await ctx.telegram.editMessageText(
@ -99,6 +104,10 @@ module.exports = (bot) => {
},
);
if(fs.existsSync(tempMp4File)){
await downloadFromYoutube(ffmpegPath, ffmpegArgs);
}
if (fs.existsSync(mp4File)) {
const message = strings.ytUploadDesc
.replace("{userId}", userId)
@ -133,7 +142,7 @@ module.exports = (bot) => {
fs.unlinkSync(mp4File);
}
} else {
await ctx.reply(`\`${error.message}\``, {
await ctx.reply(mp4File, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});

650
package-lock.json generated

File diff suppressed because it is too large Load Diff