fix: yt: check the size of the video before uploading
This commit is contained in:
parent
3814a49128
commit
01d5cc0f05
1 changed files with 46 additions and 23 deletions
|
@ -40,6 +40,14 @@ const downloadFromYoutube = async (command, args) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const timeoutPromise = (timeout) => {
|
||||||
|
return new Promise((_, reject) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('Timeout: Check took too long'));
|
||||||
|
}, timeout);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const getApproxSize = async (command, videoUrl) => {
|
const getApproxSize = async (command, videoUrl) => {
|
||||||
const args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx'];
|
const args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx'];
|
||||||
try {
|
try {
|
||||||
|
@ -61,13 +69,20 @@ module.exports = (bot) => {
|
||||||
const ytDlpPath = getYtDlpPath();
|
const ytDlpPath = getYtDlpPath();
|
||||||
const userId = ctx.from.id;
|
const userId = ctx.from.id;
|
||||||
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
|
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
|
||||||
const mp4File = `tmp/${userId}.mp4`;
|
const tempMp4File = path.resolve(`tmp/${userId}.f137.mp4`);
|
||||||
const tempMp4File = `tmp/${userId}.f137.mp4`;
|
const tempWebmFile = path.resolve(`tmp/${userId}.f251.webm`);
|
||||||
const tempWebmFile = `tmp/${userId}.f251.webm`;
|
const mp4File = path.resolve(`tmp/${userId}.mp4`);
|
||||||
let cmdArgs = "";
|
let cmdArgs = "";
|
||||||
const dlpCommand = ytDlpPath;
|
const dlpCommand = ytDlpPath;
|
||||||
const ffmpegPath = getFfmpegPath();
|
const ffmpegPath = getFfmpegPath();
|
||||||
const ffmpegArgs = ['-i', tempMp4File, '-i', tempWebmFile, '-c:v copy -c:a copy -strict -2', mp4File];
|
const ffmpegArgs = [
|
||||||
|
'-i', tempMp4File,
|
||||||
|
'-i', tempWebmFile,
|
||||||
|
'-c:v', 'copy',
|
||||||
|
'-c:a', 'copy',
|
||||||
|
'-strict', '-2',
|
||||||
|
mp4File
|
||||||
|
];
|
||||||
|
|
||||||
if (!videoUrl) {
|
if (!videoUrl) {
|
||||||
return ctx.reply(Strings.ytDownload.noLink, {
|
return ctx.reply(Strings.ytDownload.noLink, {
|
||||||
|
@ -77,22 +92,14 @@ module.exports = (bot) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) {
|
|
||||||
cmdArgs = "--max-filesize 2G --no-playlist --cookies src/props/cookies.txt --merge-output-format mp4 -o";
|
|
||||||
} else {
|
|
||||||
cmdArgs = `--max-filesize 2G --no-playlist --merge-output-format mp4 -o`;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const downloadingMessage = await ctx.reply(Strings.ytDownload.checkingSize, {
|
const downloadingMessage = await ctx.reply(Strings.ytDownload.checkingSize, {
|
||||||
parse_mode: 'Markdown',
|
parse_mode: 'Markdown',
|
||||||
reply_to_message_id: ctx.message.message_id,
|
reply_to_message_id: ctx.message.message_id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
if (fs.existsSync(ytDlpPath)) {
|
if (fs.existsSync(ytDlpPath)) {
|
||||||
const approxSizeInMB = await Promise.race([
|
let videoFormat = '-f bestvideo+bestaudio';
|
||||||
getApproxSize(ytDlpPath, videoUrl),
|
|
||||||
]);
|
|
||||||
|
|
||||||
await ctx.telegram.editMessageText(
|
await ctx.telegram.editMessageText(
|
||||||
ctx.chat.id,
|
ctx.chat.id,
|
||||||
|
@ -103,9 +110,12 @@ module.exports = (bot) => {
|
||||||
reply_to_message_id: ctx.message.message_id,
|
reply_to_message_id: ctx.message.message_id,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) {
|
||||||
const dlpArgs = [videoUrl, ...cmdArgs.split(' '), mp4File];
|
cmdArgs = "--max-filesize 2G --no-playlist --cookies src/props/cookies.txt --merge-output-format mp4 -o";
|
||||||
await downloadFromYoutube(dlpCommand, dlpArgs);
|
} else {
|
||||||
|
cmdArgs = `--max-filesize 2G --no-playlist --merge-output-format mp4 -o`;
|
||||||
|
}
|
||||||
|
let dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File];
|
||||||
|
|
||||||
await ctx.telegram.editMessageText(
|
await ctx.telegram.editMessageText(
|
||||||
ctx.chat.id,
|
ctx.chat.id,
|
||||||
|
@ -117,11 +127,22 @@ module.exports = (bot) => {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await downloadFromYoutube(dlpCommand, dlpArgs);
|
||||||
|
|
||||||
if(fs.existsSync(tempMp4File)){
|
if(fs.existsSync(tempMp4File)){
|
||||||
await downloadFromYoutube(ffmpegPath, ffmpegArgs);
|
await downloadFromYoutube(ffmpegPath, ffmpegArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs.existsSync(mp4File)) {
|
if (fs.existsSync(mp4File)) {
|
||||||
|
const videoStats = fs.statSync(mp4File);
|
||||||
|
const videoSize = videoStats.size;
|
||||||
|
const videoSizeInMb = videoSize/(1024*1024);
|
||||||
|
if(videoSizeInMb >= 50){
|
||||||
|
fs.unlinkSync(mp4File);
|
||||||
|
videoFormat = '-f best';
|
||||||
|
dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File];
|
||||||
|
await downloadFromYoutube(dlpCommand, dlpArgs);
|
||||||
|
}
|
||||||
const message = Strings.ytDownload.msgDesc.replace("{userMention}", `[${ctx.from.first_name}](tg://user?id=${userId})`)
|
const message = Strings.ytDownload.msgDesc.replace("{userMention}", `[${ctx.from.first_name}](tg://user?id=${userId})`)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -133,6 +154,8 @@ module.exports = (bot) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.unlinkSync(mp4File);
|
fs.unlinkSync(mp4File);
|
||||||
|
fs.unlinkSync(tempMp4File);
|
||||||
|
fs.unlinkSync(tempWebmFile);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (toString(error).includes("Request Entity Too Large")) {
|
if (toString(error).includes("Request Entity Too Large")) {
|
||||||
await ctx.telegram.editMessageText(
|
await ctx.telegram.editMessageText(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue