I’m encountering several issues when uploading videos to Telegram using the Telethon
library. Specifically:
- Videos often show black thumbnails.
- The videos are either unstreamable or have streaming issues.
- Incorrect video metadata (duration, width, height) is displayed.
- Sometimes the video duration appears as
00:00
, or the size is smaller than expected.
To manage metadata, I’ve used the hachoir
library to extract details like duration, width, and height, but this hasn’t fully resolved the problem. The videos work fine when uploaded manually through the Telegram app, but not when uploaded via the bot.
Code Sample:
import os
from telethon import TelegramClient
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from telethon.tl.types import DocumentAttributeVideo
api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
bot_token = 'YOUR_BOT_TOKEN'
chat_id = 'CHAT_ID'
video_path = "videos/sample_video.mp4"
client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
async def upload_video():
metadata = extractMetadata(createParser(video_path))
duration = metadata.get('duration').seconds if metadata.has('duration') else 0
width = metadata.get('width') if metadata.has('width') else 640
height = metadata.get('height') if metadata.has('height') else 360
await client.send_file(
chat_id,
video_path,
attributes=[
DocumentAttributeVideo(
duration=duration,
w=width,
h=height,
supports_streaming=True
)
]
)
with client:
client.loop.run_until_complete(upload_video())
Environment
Telethon==1.34.0
cryptg==0.4.0
tqdm==4.66.2
hachoir==3.3.0
Steps to Reproduce:
- Use
Telethon
to upload a video using extracted metadata (duration, width, height) from thehachoir
parser. - Observe that the uploaded video may show a black thumbnail, have an incorrect duration (e.g.,
00:00
), and might not stream properly. - Manually upload the same video via the Telegram app to see the correct behavior.
Expected Behavior:
- Videos should upload with the correct thumbnail, stream properly, and display accurate metadata (duration, width, height).
Actual Behavior:
- Videos frequently display incorrect metadata (e.g.,
00:00
duration, incorrect width/height). - Videos show a black thumbnail or fail to stream.
- Occasionally, the video file size is smaller than expected when uploaded via the bot.
New contributor
Unique Shadows is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.