MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / ConvertPlugin

Class ConvertPlugin

convert/convert.ts:291–440  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

289
290 description: string = help_text;
291
292 cmdHandlers: Record<string, (msg: Api.Message) => Promise<void>> = {
293 convert: async (msg: Api.Message) => {
294 const parts = msg.text?.trim()?.split(/\s+/) || [];
295 const args = parts.slice(1);
296 const subCommand = (args[0] || "").toLowerCase();
297
298 try {
299 if (subCommand === "help" || subCommand === "h" || args.length === 0 && !msg.replyTo) {
300 await msg.edit({ text: help_text, parseMode: "html" });
301 } else if (subCommand === "clear") {
302 await this.handleClearCommand(msg);
303 } else if (subCommand === "apikey") {
304 await this.handleApiKeyCommand(msg, args.slice(1).join(" "));
305 } else {
306 await this.handleVideoConversion(msg, args);
307 }
308 } catch (error: any) {
309 console.error("[convert] Plugin execution failed:", error);
310 await msg.edit({
311 text: `❌ <b>插件执行失败:</b> ${htmlEscape(error.message)}`,
312 parseMode: "html"
313 });
314 }
315 }
316 };
317
318 private async handleVideoConversion(msg: Api.Message, args: string[]): Promise<void> {
319 const client = await getGlobalClient();
320 const reply = await safeGetReplyMessage(msg);
321
322 if (!client || !reply || (!reply.document && !reply.video)) {
323 await msg.edit({ text: `❌ <b>使用错误</b>\n\n请回复一个视频消息后再使用此命令。\n\n请回复一个视频消息后再试。`, parseMode: "html" });
324 return;
325 }
326
327 const doc = reply.video || reply.document;
328 const fileNameAttr = doc?.attributes?.find((a: any) => a.fileName) as Api.DocumentAttributeFilename | undefined;
329 const originalFileName = fileNameAttr?.fileName || "video.mp4";
330
331 await msg.edit({ text: "📥 正在下载视频...", parseMode: "html" });
332
333 const timestamp = Date.now();
334 const tempVideoPath = path.join(converter.tempDir, `${timestamp}_video`);
335 const tempAudioPath = path.join(converter.tempDir, `${timestamp}.mp3`);
336 let finalAudioPath = tempAudioPath;
337 let tempCoverPath: string | undefined;
338
339 try {
340 await client.downloadMedia(reply, { outputFile: tempVideoPath });
341 if (!fs.existsSync(tempVideoPath)) throw new Error("视频下载失败");
342
343 await msg.edit({ text: "🔄 正在转换为 MP3...", parseMode: "html" });
344 if (!await converter.convertVideoToMp3(tempVideoPath, tempAudioPath)) {
345 throw new Error("视频转换失败,请检查 FFmpeg 是否已安装");
346 }
347
348 const useAi = (args[0] || "").toLowerCase() === 'u';

Callers

nothing calls this directly

Calls 6

handleClearCommandMethod · 0.95
handleApiKeyCommandMethod · 0.95
handleVideoConversionMethod · 0.95
errorMethod · 0.80
htmlEscapeFunction · 0.70
editMethod · 0.45

Tested by

no test coverage detected