Telethon发送带流式播放支持视频失败,如何实现流式播放?
解决Telethon发送支持流式播放视频的问题
要让发送的视频支持Telegram流式播放,需从文件本身兼容性和代码参数设置两方面调整,具体如下:
先确保视频文件符合要求
Telegram仅支持编码为H.264(视频流)+ AAC(音频流)的MP4文件,且要求文件的moov原子位于头部(否则无法流式加载)。可以用ffmpeg快速修正文件:ffmpeg -i video.mp4 -movflags faststart fixed_video.mp4之后用修正后的
fixed_video.mp4进行发送。修正代码参数设置
你当前的代码未在手动传入的DocumentAttributeVideo中指定流式播放属性,且Telethon可自动检测视频参数,无需手动传入属性。推荐使用简化版代码:client.send_file( userentity, 'fixed_video.mp4', supports_streaming=True, force_document=False, progress_callback=handle_progress )若你需要手动指定视频属性(比如自定义时长、分辨率),必须在
DocumentAttributeVideo内也加上supports_streaming=True:from telethon.tl.types import DocumentAttributeVideo client.send_file( userentity, 'fixed_video.mp4', attributes=(DocumentAttributeVideo( duration=duration, w=width, h=height, supports_streaming=True # 此参数必须添加 ),), supports_streaming=True, force_document=False, progress_callback=handle_progress )核心要点总结
supports_streaming=True需要同时在send_file参数和DocumentAttributeVideo属性中生效(手动传属性时);- 视频文件的编码和结构是流式播放的前提,文件不兼容时代码设置再正确也无效。
内容的提问来源于stack exchange,提问作者Volkodaff
相关产品推荐
相关产品推荐

