You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Telegram Bot实现发送超过50MB的大体积视频文件

可行实现方案

首先明确:Telegram官方公共Bot API的单文件上传上限为50MB,要发送1GB级大文件可选择以下3种方案:

  • 自建本地Telegram Bot API服务(最推荐)
    官方提供的本地Bot API服务可将单文件上传上限提升至2GB,完全满足需求。仅需三步即可完成适配:
    1. 前往Telegram官方开发者平台申请自己的api_id和api_hash
    2. 部署本地Bot API服务,配置上述参数和你的Bot Token
    3. 调整Telegraf初始化配置,将API请求地址指向本地服务,同时把文件大小上限调整为2000MB
  • 视频分片/压缩发送
    引入FFmpeg工具,对超过50MB的视频进行码率压缩,或者切割为多个50MB以内的分片依次发送,可附带合并教程方便用户操作
  • 生成私有下载链接
    将下载完成的大视频上传至自建对象存储或文件服务,生成临时下载链接发送给用户,完全绕开Telegram文件大小限制
适配自建本地API的代码修改示例

仅需修改Telegraf初始化配置和大小阈值即可,原有业务逻辑几乎无需改动:

const { Telegraf } = require('telegraf');
const fs = require('fs');
// 建议替换为维护更活跃的yt-dlp,下载成功率更高
const youtubedl = require('youtube-dl');

// 本地Bot API服务地址,根据你实际部署的地址修改
const LOCAL_API_ROOT = 'http://127.0.0.1:8081';
const bot = new Telegraf('mytoken', {
  telegram: {
    apiRoot: LOCAL_API_ROOT,
    // 调整大文件上传超时时间,单位为毫秒
    timeout: 600000
  }
});

var DOWN_URL = "https://www.youtube.com/watch?v=";
// 调整上限为2000MB,即本地API支持的最大单文件大小
var TeleMaxData = 2000;
var videosize;
let downloaded = 0

bot.command('/video', async (ctx) => {
    try {
        let videoURL = 'someYoutubeUrl';
        ctx.reply(`Youtube video URL: ${videoURL}`);

        var video = youtubedl(videoURL,
            ['--format=18'],
            { cwd: __dirname });

        video.on('info', function (info) {
            infor = info;
            ctx.reply('info', info)
            videosize = infor.size / 1000000;

            if (videosize < TeleMaxData) {
                ctx.reply('Download Started')
                video.pipe(fs.createWriteStream(`test.mp4`, { flags: 'a' }));

                video.on('end', async function () {
                    ctx.reply("Download completed");
                    try {
                        ctx.reply(`Download completed!\nVideo gets Send! - This might take a few Seconds! \n \n Title: \n ${infor.title}. It's ${videosize}mb big.`);
                        await ctx.replyWithVideo({
                            source: fs.createReadStream(`test.mp4`)
                        })
                    } catch (err) {
                        ctx.reply('Error: sendVideo' + err);
                    }
                })
            } else {
                ctx.reply(`The Video is ${videosize}mb. The maximum size for sending videos from Telegram is ${TeleMaxData}mb.`);
            }
        });
    } catch (err) {
        ctx.reply("ERROR" + err);
    }
})

内容的提问来源于stack exchange,提问作者Stasoz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 14:33:00