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

iPhone端Telegram Python机器人视频压缩与显示异常问题排查

解决Telegram机器人视频发送的两个问题:压缩与iPhone显示异常

问题1:部分视频被压缩

原因

Telegram的send_video方法默认会对视频进行压缩以适配平台规则,即便使用本地API服务器,未明确指定参数时仍可能触发自动压缩。另外,yt-dlp的下载配置若未严格锁定原画质,也可能导致下载的视频本身已被压缩。

解决方法

  1. 锁定yt-dlp原画质下载:确保下载的是原分辨率、原码率的视频,避免工具自动转码压缩。
  2. 调整Telegram发送策略:要么用send_document发送大视频(文档形式不会被压缩),要么给send_video添加参数禁用压缩逻辑。

修改downloader.py的ydl_opts:

ydl_opts = {
    'format': 'bestvideo[ext=mp4][height<=1080]+bestaudio[ext=m4a]/best[ext=mp4]/best',
    'postprocessors': [
        {
            'key': 'FFmpegVideoConvertor',
            'preferedformat': 'mp4'
        },
        {
            'key': 'FFmpegFixupMp4',
        }
    ],
    'outtmpl': '%(title)s.%(ext)s',
    'restrictfilenames': True,
    'nooverwrites': True,
    'ignoreerrors': True,
    'continuedl': True,
    'keepvideo': True,
    'noplaylist': True,
}

修改bot.py中的发送逻辑,二选一即可:

  • 方案1:用send_document发送(无压缩)
# 替换原send_video代码
document = FSInputFile(video_path)
await bot.send_document(user_msg.chat.id, document, reply_to_message_id=user_msg.message_id)
  • 方案2:优化send_video参数(减少压缩)
# 先添加获取视频分辨率的工具函数
import ffmpeg

def get_video_dimensions(video_path):
    probe = ffmpeg.probe(video_path)
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    if video_stream:
        return int(video_stream['width']), int(video_stream['height'])
    return None, None

# 发送视频时传入分辨率等参数
width, height = get_video_dimensions(video_path)
video = FSInputFile(video_path)
await bot.send_video(
    user_msg.chat.id, 
    video, 
    reply_to_message_id=user_msg.message_id,
    width=width,
    height=height,
    supports_streaming=True,
    disable_content_type_detection=True
)

问题2:1920x1080视频在iPhone显示为方形

原因

视频文件包含旋转元数据(比如竖屏拍摄时的旋转标记),iPhone会优先读取该元数据调整显示方向,但桌面端会忽略元数据直接按原始分辨率渲染,导致显示效果不一致。

解决方法

在yt-dlp下载后,用ffmpeg强制修正视频旋转信息,将视频帧旋转为正确显示方向,并清除旋转元数据,确保iPhone和桌面端显示一致。

修改downloader.py的ydl_opts,添加旋转处理处理器:

ydl_opts = {
    # 原有配置...
    'postprocessors': [
        {
            'key': 'FFmpegVideoConvertor',
            'preferedformat': 'mp4'
        },
        {
            'key': 'FFmpegRotate',
            'video_format': 'mp4',
        },
        {
            'key': 'FFmpegFixupMp4',
        }
    ],
    # 原有配置...
}

这个处理器会自动识别旋转元数据,将视频帧旋转为正确方向,并移除冗余的旋转标记,消除跨设备显示差异。

额外注意事项

  • 确保服务器安装了最新版本的ffmpeg,否则yt-dlp的后置处理器无法正常工作。
  • 若视频文件过大,需确认本地API服务器配置允许上传对应大小的文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 05:14:57