mirror of
https://github.com/abocn/TelegramBot.git
synced 2025-04-28 17:15:57 +00:00
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:
parent
5efb8cab58
commit
db21fe2a21
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,5 +6,5 @@ __pycache__
|
|||||||
props
|
props
|
||||||
!props/resources.json
|
!props/resources.json
|
||||||
*.mp4
|
*.mp4
|
||||||
plugins/yt-dlp
|
plugins/*
|
||||||
tmp
|
tmp
|
@ -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 })
|
|
||||||
});
|
|
||||||
}
|
|
@ -17,6 +17,17 @@ const getYtDlpPath = () => {
|
|||||||
return ytDlpPaths[platform] || ytDlpPaths.linux;
|
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) => {
|
const downloadFromYoutube = async (command, args) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
execFile(command, args, (error, stdout, stderr) => {
|
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) => {
|
module.exports = (bot) => {
|
||||||
bot.command(['yt', 'ytdl'], spamwatchMiddleware, async (ctx) => {
|
bot.command(['yt', 'ytdl'], spamwatchMiddleware, async (ctx) => {
|
||||||
const strings = getStrings(ctx.from.language_code);
|
const strings = getStrings(ctx.from.language_code);
|
||||||
@ -60,8 +63,12 @@ module.exports = (bot) => {
|
|||||||
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
|
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
|
||||||
|
|
||||||
const mp4File = `tmp/${userId}.mp4`;
|
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 cmdArgs = "--max-filesize 2G --no-playlist --cookies props/cookies.txt --merge-output-format mp4 -o";
|
||||||
const dlpCommand = ytDlpPath;
|
const dlpCommand = ytDlpPath;
|
||||||
|
const ffmpegPath = getFfmpegPath();
|
||||||
|
const ffmpegArgs = ['-i', tempMp4File, '-i', tempWebmFile, '-c:v copy -c:a copy -strict -2', mp4File];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const downloadingMessage = await ctx.reply(strings.ytCheckingSize, {
|
const downloadingMessage = await ctx.reply(strings.ytCheckingSize, {
|
||||||
@ -74,8 +81,6 @@ module.exports = (bot) => {
|
|||||||
getApproxSize(ytDlpPath, videoUrl)
|
getApproxSize(ytDlpPath, videoUrl)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let videoFormat = approxSizeInMB >= 50 ? '-f best' : "-f bestvideo+bestaudio";
|
|
||||||
|
|
||||||
await ctx.telegram.editMessageText(
|
await ctx.telegram.editMessageText(
|
||||||
ctx.chat.id,
|
ctx.chat.id,
|
||||||
downloadingMessage.message_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 downloadFromYoutube(dlpCommand, dlpArgs);
|
||||||
|
|
||||||
await ctx.telegram.editMessageText(
|
await ctx.telegram.editMessageText(
|
||||||
@ -99,6 +104,10 @@ module.exports = (bot) => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if(fs.existsSync(tempMp4File)){
|
||||||
|
await downloadFromYoutube(ffmpegPath, ffmpegArgs);
|
||||||
|
}
|
||||||
|
|
||||||
if (fs.existsSync(mp4File)) {
|
if (fs.existsSync(mp4File)) {
|
||||||
const message = strings.ytUploadDesc
|
const message = strings.ytUploadDesc
|
||||||
.replace("{userId}", userId)
|
.replace("{userId}", userId)
|
||||||
@ -133,7 +142,7 @@ module.exports = (bot) => {
|
|||||||
fs.unlinkSync(mp4File);
|
fs.unlinkSync(mp4File);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await ctx.reply(`\`${error.message}\``, {
|
await ctx.reply(mp4File, {
|
||||||
parse_mode: 'Markdown',
|
parse_mode: 'Markdown',
|
||||||
reply_to_message_id: ctx.message.message_id,
|
reply_to_message_id: ctx.message.message_id,
|
||||||
});
|
});
|
||||||
|
642
package-lock.json
generated
642
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user