How to download a video using telethon
Asked Answered
N

2

6

I'm working on telethon download_media and _download_document methods for downloading video from telegram. My code is something like this:

def callback(update): 
        Channel_Entity = client.get_entity(Channel_List) #Get specific Channel information

        file_name = str(document_id)+'.mp4'
        current_path = os.getcwd()
        file_path_gif = current_path+'/media/gif'
        file = open(os.path.join(file_path_gif,file_name),'wb')

        if isinstance(update, UpdateNewChannelMessage): #Check Update message in channel
            if update.message.to_id.channel_id == Channel_Entity.id:

                client._download_document(update.message.media, file, update.message.date, progress_callback=None)

                # OR 

                client.download_media(update.message, file, progress_callback=None)

But when a video is sent to channel and downloaded with this code, the video is not playable and the player prints this message: Cannot render the file. This code works on images and gif files but does not work on video files. What should i do?

Narcoanalysis answered 5/12, 2017 at 18:7 Comment(0)
F
17

I hope this code will help you. I used Telethon V0.19, but the previous versions are pretty much the same.

from telethon import TelegramClient

api_id = XXXXXXX
api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
phone_number = '+98XXXXXXXXX'
################################################
channel_username = 'tehrandb'
################################################

client = TelegramClient('session_name',
                    api_id,
                    api_hash)

assert client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))

# ---------------------------------------
msgs = client.get_messages(channel_username, limit=100)
for msg in msgs.data:
    if msg.media is not None:
        client.download_media(message=msg)
Furey answered 18/5, 2018 at 15:48 Comment(0)
J
0

2024 updated code!

Save this file into a python file and run it....

from telethon import TelegramClient, events
import telethon.tl.types

api_id = id
api_hash = hash

channel = group/channel username

client = TelegramClient('anon', api_id, api_hash)

async def process_messages():
   # You can use any chat object here
   chat = await client.get_entity(channel)

   async for message in client.iter_messages(chat):
    if (
          hasattr(message, 'media') and
          hasattr(message.media, 'document') and
          hasattr(message.media.document, 'mime_type') and
          message.media.document.mime_type == 'video/mp4'
      ):

        await  client.download_media(message=message , file='file_name.mp4')
        # The message is a video message
        print(f'Found video message: {message.id}')

with client:
    client.loop.run_until_complete(process_messages())
Jaeger answered 2/2, 2024 at 14:4 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Plash

© 2022 - 2025 — McMap. All rights reserved.