Skip to main content

Download videos from Telegram channels

Project description

telegram-dl

A Python CLI tool to download videos from Telegram channels.

Installation

pip install telegram-dl

Quick Start

1. Get Telegram API Credentials

  1. Go to my.telegram.org
  2. Log in with your phone number
  3. Click "API development tools"
  4. Create a new app
  5. Copy your API ID and API Hash

2. Download Videos

# List your channels first
telegram-dl --api-id 12345 --api-hash abc123def456 --phone +1234567890 --list-channels

# Download all videos from a channel
telegram-dl --api-id 12345 --api-hash abc123def456 --phone +1234567890 --channel -1001234567890

3. Use Custom Output Directory

telegram-dl --api-id 12345 --api-hash abc123def456 --phone +1234567890 --channel -1001234567890 --output ./my_videos

Programmatic Usage

from telegram_dl import TelegramDownloader

async def main():
    async with TelegramDownloader(
        api_id=12345,
        api_hash="abc123def456",
        phone="+1234567890"
    ) as dl:
        # List channels
        async for dialog in dl.client.iter_dialogs():
            if dialog.is_channel:
                print(f"{dialog.id} | {dialog.title}")
        
        # Download all videos
        await dl.download_all_videos(channel_id, "./videos")

asyncio.run(main())

High Level Design (HLD)

Architecture

flowchart TB
    subgraph User["👤 User Layer"]
        CLI["🖥️ CLI Interface<br/>(telegram-dl)"]
        API["📦 Python API<br/>(TelegramDownloader)"]
    end
    
    subgraph Core["⚙️ Core Layer"]
        TD["🔷 TelegramDownloader<br/>• connect() / disconnect()<br/>• get_channel_videos()<br/>• download_video()<br/>• download_all_videos()"]
    end
    
    subgraph Protocol["🔌 Protocol Layer"]
        TL["🔶 Telethon Library<br/>• MTProto<br/>• Session Management<br/>• OTP/2FA Auth"]
    end
    
    subgraph External["🌐 External Services"]
        TS["📡 Telegram Servers<br/>api.telegram.org"]
    end
    
    User --> Core
    Core --> Protocol
    Protocol --> External
    
    style User fill:#e1f5fe
    style Core fill:#fff3e0
    style Protocol fill:#f3e5f5
    style External fill:#e8f5e9

Data Flow

sequenceDiagram
    participant U as 👤 User
    participant C as 🖥️ CLI/API
    participant TD as 🔷 TelegramDownloader
    participant TL as 🔶 Telethon
    participant TS as 📡 Telegram

    U->>C: Provide credentials
    C->>TD: Initialize
    TD->>TL: connect()
    TL->>TS: Establish connection
    TS-->>TL: OTP verification
    TL-->>TD: Authenticated
    TD-->>C: Ready
    
    U->>C: download_all_videos(channel)
    C->>TD: Fetch channel
    TD->>TL: iter_messages()
    TL->>TS: Get messages
    TS-->>TL: Return messages
    TL-->>TD: Video messages
    TD->>TL: download_media()
    loop For each video
        TL->>TS: Request file
        TS-->>TL: Stream file
        TL-->>TD: Save to disk
    end
    TD-->>C: Done!

Class Diagram

classDiagram
    class TelegramDownloader {
        +int api_id
        +str api_hash
        +str phone
        +TelegramClient client
        
        +connect() TelegramClient
        +disconnect()
        +get_dialogs() List[Dict]
        +get_channel_videos(channel_id) List[Dict]
        +download_video(channel_id, video_id, output_dir, filename) str
        +download_all_videos(channel_id, output_dir, progress_callback) List[str]
    }
    
    class TelegramDLError {
        +Exception base
    }
    
    class AuthenticationError {
        +TelegramDLError base
    }
    
    class ChannelNotFoundError {
        +TelegramDLError base
    }
    
    class VideoNotFoundError {
        +TelegramDLError base
    }
    
    TelegramDownloader o-- TelegramDLError : raises
    TelegramDLError <|-- AuthenticationError
    TelegramDLError <|-- ChannelNotFoundError
    TelegramDLError <|-- VideoNotFoundError
    
    TelegramDownloader --> "uses" TelethonClient : TelegramClient

Session Management Flow

flowchart LR
    A([🚀 First Run]) --> B{Has Session?}
    B -->|No| C["📱 OTP Verification"]
    C --> D["💾 Save Session"]
    D --> E[✅ Ready]
    
    B -->|Yes| F{Session Valid?}
    F -->|Yes| E
    F -->|No| C
    
    G([♻️ Next Run]) --> H{Session Exists?}
    H -->|Yes| E
    H -->|No| C

Error Handling

flowchart TB
    E[❌ Error Occurs] --> A
    
    subgraph TelethonErrors
        A1["🔴 ConnectionError"]
        A2["🔴 SessionPasswordNeededError"]
        A3["🔴 ChannelInvalidError"]
    end
    
    subgraph CustomErrors
        B1["🟠 TelegramDLError (base)"]
        B2["🟠 AuthenticationError"]
        B3["🟠 ChannelNotFoundError"]
        B4["🟠 VideoNotFoundError"]
    end
    
    subgraph Handling
        C1["📋 User retries with valid creds"]
        C2["📋 Check channel ID"]
        C3["📋 Verify video exists"]
    end
    
    A1 --> B1
    A2 --> B2
    A3 --> B3
    
    B1 <|-- B2
    B1 <|-- B3
    B1 <|-- B4
    
    B2 --> C1
    B3 --> C2
    B4 --> C3
    
    style TelethonErrors fill:#ffebee
    style CustomErrors fill:#fff8e1
    style Handling fill:#e8f5e9

Components

Component Responsibility
cli.py Command-line interface, argument parsing
client.py Core download logic, Telegram client management
exceptions.py Custom exception classes

Key Features

mindmap
  root((🔧 Features))
    CLI Interface
      Simple commands
      Progress tracking
      Channel listing
    Core Logic
      Async download
      Auto retry
      Skip existing
    Session Management
      Persistent login
      OTP support
      2FA support
    File Handling
      Named preservation
      Custom output dir
      Progress callbacks

Features

  • Download all videos from Telegram channels
  • Automatic session management (login once, use many times)
  • Progress tracking
  • Named file preservation from Telegram
  • Skip already downloaded files
  • CLI and programmatic API

Requirements

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_dl-1.0.2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_dl-1.0.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file telegram_dl-1.0.2.tar.gz.

File metadata

  • Download URL: telegram_dl-1.0.2.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for telegram_dl-1.0.2.tar.gz
Algorithm Hash digest
SHA256 92efb2b97d64ca3f36d1c67e28ba9087ded85e6f8156c853dad2fc8db74af410
MD5 619c85263a3aa1c30189bf5a450f7db8
BLAKE2b-256 b577a625fdf218b1483b6f77d06be56779e5c29ab14e42ba9d6f59045a6e2482

See more details on using hashes here.

File details

Details for the file telegram_dl-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: telegram_dl-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for telegram_dl-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a61e8128d03ac2e7285823b7ed8820d526d909c694d8e5ae15e6afd255206c52
MD5 d0fa21151ea14423c862d52c6851a58c
BLAKE2b-256 faa1afd79382de1aafd95ea358411c9e89baaf561108072326f2f81a4b69a2c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page