download_media()¶
- Client.download_media()¶
Download the media from a message.
Usable by Users Bots
- Parameters:¶
message (:obj:`~pyrogram.types.Message` | :obj:`~pyrogram.types.Audio` | :obj:`~pyrogram.types.Document` | :obj:`~pyrogram.types.Photo` | :obj:`~pyrogram.types.Sticker` | :obj:`~pyrogram.types.Video` | :obj:`~pyrogram.types.Animation` | :obj:`~pyrogram.types.Voice` | :obj:`~pyrogram.types.VideoNote` | :obj:`~pyrogram.types.Story` | :obj:`~pyrogram.types.PaidMediaInfo` | :obj:`~pyrogram.types.PaidMediaPhoto` | :obj:`~pyrogram.types.PaidMediaVideo` | :obj:`~pyrogram.types.Thumbnail` | :obj:`~pyrogram.types.StrippedThumbnail` | :obj:`~pyrogram.types.PaidMediaPreview` | :obj:`~pyrogram.types.Story` | ``str``) – Pass a Message containing the media, the media itself (message.audio, message.video, …) or a file id as string.
file_name (``str``, *optional*) – A custom file_name to be used instead of the one provided by Telegram. By default, all files are downloaded in the downloads folder in your working directory. You can also specify a path for downloading files in a custom location: paths that end with “/” are considered directories. All non-existent folders will be created automatically.
in_memory (``bool``, *optional*) – Pass True to download the media in-memory. A binary file-like object with its attribute “.name” set will be returned. Defaults to False.
block (``bool``, *optional*) – Blocks the code execution until the file has been downloaded. Defaults to True.
idx (``int``, *optional*) – In case of a
PaidMediaInfowith more than onepaid_media, the zero based index of thePaidMediato download. RaisesIndexErrorif the index specified does not exist in the originalmessage.progress (``Callable``, *optional*) – Pass a callback function to view the file transmission progress. The function must take (current, total) as positional arguments (look at Other Parameters below for a detailed description) and will be called back each time a new file chunk has been successfully transmitted.
progress_args (``tuple``, *optional*) – Extra custom arguments for the progress callback function. You can pass anything you need to be available in the progress callback scope; for example, a Message object or a Client instance in order to edit the message with the updated progress status.
- Other Parameters:¶
current (``int``) – The amount of bytes transmitted so far.
total (``int``) – The total size of the file.
*args (``tuple``, *optional*) – Extra custom arguments as defined in the
progress_argsparameter. You can either keep*argsor add every single extra argument in your function signature.
- Returns:¶
``str`` | ``None`` | :obj:`io.BytesIO` (On success, the absolute path of the downloaded file is returned,)
otherwise, in case the download failed or was deliberately stopped with
stop_transmission(), None is returned.Otherwise, in case
in_memory=True, a binary file-like object with its attribute “.name” set is returned.If the message is a
PaidMediaInfowith more than onepaid_mediacontainingminithumbnailandidxis not specified, then a list of paths or binary file-like objects is returned.
- Raises:¶
IndexError – In case of wrong value of
idx.:ValueError – If the message doesn’t contain any downloadable media.:
RPCError –
Example
Download media to file
# Download from Message await app.download_media(message) # Download from file id await app.download_media(message.photo.file_id) # Keep track of the progress while downloading async def progress(current, total): print(f"{current * 100 / total:.1f}%") await app.download_media(message, progress=progress)Download media in-memory
file = await app.download_media(message, in_memory=True) file_name = file.name file_bytes = bytes(file.getbuffer())