-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.js
204 lines (190 loc) · 6.61 KB
/
telegram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const TelegramBot = require("node-telegram-bot-api");
const fs = require("fs");
const ytdl = require("ytdl-core");
const ffmpeg = require("fluent-ffmpeg");
const ffmpegStatic = require("ffmpeg-static");
const { authorize, uploadFile } = require("./drive.js");
const { PassThrough } = require("stream");
const { info } = require("console");
ffmpeg.setFfmpegPath(ffmpegStatic);
const token = "7156204414:AAFo4NcukUJkbagKOWDluRNd_KGOsYU2KrM";
const bot = new TelegramBot(token, { polling: true });
async function extractYouTubeVideoID(url) {
return new Promise((resolve, reject) => {
try {
const pattern =
/^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
const match = url.match(pattern);
resolve(match ? match[1] : null);
} catch (error) {
console.log("Error:", error);
reject(error);
}
});
}
async function getAvailableFormats(videoUrl) {
const info = await ytdl.getInfo(videoUrl);
let masterArray = [];
for (const format of info.formats) {
if (format.mimeType.includes("video/mp4")) {
extradata = {
url: videoUrl,
itag: format.itag,
};
const obj = {
callback_data: JSON.stringify(extradata),
text:
format.qualityLabel +
" " +
format.container +
" " +
(format.contentLength / (1024 * 1024)).toFixed(2) +
"MB",
};
let arr = [];
arr.push(obj);
masterArray.push(arr);
}
}
return masterArray;
}
bot.onText(
/^((http(s)?:\/\/)?(www\.)?youtube\.com\/(watch\?v=|embed\/|v\/|.+\?v=)|((http(s)?:\/\/)?(www\.)?youtu\.be\/))([a-zA-Z0-9_-]{11})(\S*)?$/,
async (message) => {
try {
const chatId = message.chat.id;
const videoUrl = await extractYouTubeVideoID(message.text);
const data = await getAvailableFormats(videoUrl);
const options = {
reply_markup: {
inline_keyboard: data,
},
parse_mode: "HTML",
};
bot.sendMessage(chatId, "Choose a format:", options);
} catch (error) {
console.error("Error:", error);
}
}
);
async function processVideo(chatId, videoUrl, videoFormat, audioFormat, info) {
try {
await Promise.all([
downloadVideo(chatId, videoUrl, videoFormat, info),
downloadAudio(chatId, videoUrl, audioFormat, info),
]);
let id = await mergeAudioAndVideo(chatId, info);
bot.sendMessage(chatId, `Video processed successfully!`);
bot.sendMessage(chatId, `https://drive.google.com/file/d/${id}/view`);
} catch (error) {
console.error("Error processing video:", error);
bot.sendMessage(chatId, "An error occurred while processing the video.");
}
}
async function downloadVideo(chatId, videoUrl, videoFormat, info) {
return new Promise((resolve, reject) => {
try {
bot.sendMessage(chatId, "Downloading video...");
ytdl(videoUrl, { format: videoFormat })
.pipe(fs.createWriteStream(`tempv${info.videoDetails.title}.mp4`))
.on("finish", resolve)
.on("error", reject);
} catch (error) {
console.error("Error downloading video:", error);
bot.sendMessage(chatId, "An error occurred while downloading the video.");
}
});
}
async function downloadAudio(chatId, videoUrl, audioFormat, info) {
return new Promise((resolve, reject) => {
try {
bot.sendMessage(chatId, "Downloading audio...");
ytdl(videoUrl, { format: audioFormat })
.pipe(fs.createWriteStream(`tempa${info.videoDetails.title}.mp4`))
.on("finish", resolve)
.on("error", reject);
} catch (error) {
console.error("Error downloading audio:", error);
bot.sendMessage(chatId, "An error occurred while downloading the audio.");
}
});
}
async function mergeAudioAndVideo(chatId, info) {
return new Promise((resolve, reject) => {
bot.sendMessage(chatId, "Merging audio and video...Please be patient!!");
try {
const passThroughStream = new PassThrough();
ffmpeg()
.input(`tempv${info.videoDetails.title}.mp4`)
.input(`tempa${info.videoDetails.title}.mp4`)
.videoCodec("copy")
.audioCodec("aac")
.on("end", () => {
console.log("Merging completed!");
fs.unlinkSync(`tempv${info.videoDetails.title}.mp4`);
fs.unlinkSync(`tempa${info.videoDetails.title}.mp4`);
})
.on("error", (err) => {
console.error("Error during merging:", err);
fs.unlinkSync(`tempv${info.videoDetails.title}.mp4`);
fs.unlinkSync(`tempa${info.videoDetails.title}.mp4`);
})
.format("mp4")
.outputOptions("-movflags frag_keyframe+empty_moov")
.pipe(passThroughStream);
authorize().then((client) => {
let fid = uploadFile(
client,
info.videoDetails.title,
"video/mp4",
passThroughStream
);
console.log("File Id: this", fid);
resolve(fid);
});
} catch (error) {
console.error("Error in mergeAudioAndVideoStreams:", error);
reject(error);
}
});
}
// async function createDownloadLink(info) {
// return new Promise((resolve, reject) => {
// const url = "http://localhost:3000/create-download-link";
// const headers = {
// "Content-Type": "application/json",
// };
// const data = {
// fileName: `${info.videoDetails.title.replace(/[^\w\s-]/g, "")}.mp4`,
// expiryMinutes: 10,
// };
// fetch(url, {
// method: "POST",
// headers: headers,
// body: JSON.stringify(data),
// })
// .then((response) => response.json())
// .then((responseData) => {
// resolve(responseData.downloadLink);
// })
// .catch((error) => {
// console.log("Error creating download link: ", error);
// reject(error);
// });
// });
// }
bot.on("callback_query", async (query) => {
chatId = query.message.chat.id;
const data = JSON.parse(query.data);
const videoUrl = data.url;
const itag = data.itag;
const info = await ytdl.getInfo(videoUrl);
const videoFormat = info.formats.find((format) => format.itag == itag);
const audioFormat = ytdl.chooseFormat(info.formats, {});
try {
processVideo(chatId, videoUrl, videoFormat, audioFormat, info);
} catch (error) {
console.error("Error processing video:", error);
bot.sendMessage(chatId, "An error occurred while processing the video.");
}
});