【问题标题】:How to download a video using telethon如何使用 Telethon 下载视频
【发布时间】:2018-05-19 11:49:26
【问题描述】:

我正在研究 telethon download_media_download_document 从电报下载视频的方法。我的代码是这样的:

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)

但是当视频发送到频道并使用此代码下载时,视频无法播放,播放器会打印以下消息: 无法渲染文件。 此代码适用于图像和 gif 文件,但不适用于视频文件。我该怎么办?

【问题讨论】:

    标签: python-3.x telegram telethon


    【解决方案1】:

    我希望这段代码对您有所帮助。我使用了Telethon V0.19,但以前的版本几乎相同。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-22
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多