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
- Go to my.telegram.org
- Log in with your phone number
- Click "API development tools"
- Create a new app
- 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)
Note: For colorful Mermaid diagrams, see the GitHub README
Architecture
┌─────────────────────────────────────────────────────────────┐
│ User Layer (👤) │
│ ┌─────────────────┐ ┌─────────────────────┐ │
│ │ CLI Interface │ │ Python API │ │
│ │ (telegram-dl) │ │ TelegramDownloader │ │
│ └─────────────────┘ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Core Layer (⚙️) │
│ TelegramDownloader │
│ ├─ connect() / disconnect() │
│ ├─ get_channel_videos() │
│ ├─ download_video() │
│ └─ download_all_videos() │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Protocol Layer (🔌) │
│ Telethon Library │
│ ├─ MTProto Protocol │
│ ├─ Session Management │
│ └─ OTP/2FA Authentication │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ External Services (🌐) │
│ Telegram Servers (api.telegram.org) │
└─────────────────────────────────────────────────────────────┘
Data Flow
User ──► CLI/API ──► TelegramDownloader ──► Telethon ──► Telegram
│ │ │ │
│ │ │ │
Credentials Connect & Auth MTProto Server
│ │ │ │
▼ ▼ ▼ ▼
Input Session Created Protocol Video Stream
Class Structure
TelegramDownloader
├── api_id: int
├── api_hash: str
├── phone: str
├── client: TelegramClient
│
├── 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]
Exceptions
├── TelegramDLError (base)
├── AuthenticationError
├── ChannelNotFoundError
└── VideoNotFoundError
Session Management Flow
First Run ──► OTP Verification ──► Save Session ──► Ready
│ │ │
│ │ ▼
│ └───────────────────► Done!
│
▼
Next Run ──► Session Valid? ──► Yes ──► Ready
│
└── No ──► OTP Verification
Error Handling
┌─────────────────────────────────────────┐
│ Telethon Errors │
│ ConnectionError │ SessionPasswordError │
└─────────────────┴───────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Custom Exceptions │
│ TelegramDLError (base) │
│ ├─ AuthenticationError │
│ ├─ ChannelNotFoundError │
│ └─ VideoNotFoundError │
└─────────────────────────────────────────┘
│
▼
User fixes & retries
Components
| Component | File | Responsibility |
|---|---|---|
| CLI | cli.py |
Command-line interface |
| Client | client.py |
Core download logic |
| Exceptions | exceptions.py |
Error classes |
Key Features
- CLI Interface - Simple commands, progress tracking, channel listing
- Core Logic - Async download, auto retry, skip existing files
- Session Management - Persistent login, OTP/2FA support
- File Handling - Named preservation, custom output, 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
- Python 3.10+
- Telegram API credentials (get from my.telegram.org)
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.3.tar.gz
(8.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telegram_dl-1.0.3.tar.gz.
File metadata
- Download URL: telegram_dl-1.0.3.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2492ff34df15f2061cf70c91589d640f047661c98499a810e2ede3c1ab8349e7
|
|
| MD5 |
a0e5f9b49f660d7f4ce6bf9bd7cc9e42
|
|
| BLAKE2b-256 |
88f2c0ce8b14470f3abaa661d4d2a0f0b0cb975554ce250fc30827d1241bfea5
|
File details
Details for the file telegram_dl-1.0.3-py3-none-any.whl.
File metadata
- Download URL: telegram_dl-1.0.3-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d94bb36a26440735287477624cb63fa8820b2e0f0c799d0bd589468e4e20ac5
|
|
| MD5 |
3a89bd687a82df3fc91b8cbd9c51e1e0
|
|
| BLAKE2b-256 |
b6767d3bdf77ba9135725a81927806e2d010023d4459ae4c77c434a7d622fa5e
|