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)
Architecture
┌─────────────────────────────────────────────────────────────┐
│ User │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ CLI / API │ │
│ │ (telegram_dl.cli / client) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ TelegramDownloader │ │
│ │ (Client Layer) │ │
│ │ • connect() / disconnect() │ │
│ │ • get_channel_videos() │ │
│ │ • download_video() │ │
│ │ • download_all_videos() │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Telethon Library │ │
│ │ (Telegram Protocol Layer) │ │
│ │ • MTProto Protocol │ │
│ │ • Session Management │ │
│ │ • Authentication (OTP/2FA) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
└──────────────────────────────│──────────────────────────────┘
│
▼
┌─────────────────────┐
│ Telegram Servers │
│ api.telegram.org │
└─────────────────────┘
Components
| Component | Responsibility |
|---|---|
cli.py |
Command-line interface, argument parsing |
client.py |
Core download logic, Telegram client management |
exceptions.py |
Custom exception classes |
Data Flow
1. User provides credentials (API_ID, API_HASH, Phone)
│
▼
2. TelegramDownloader.connect()
- Creates Telethon client
- Establishes MTProto connection
- Handles authentication (OTP verification)
│
▼
3. User calls download_all_videos(channel_id)
- Fetches channel entity
- Iterates messages via iter_messages()
- Filters videos by message.video attribute
│
▼
4. For each video:
- Check if file already exists (skip if yes)
- Download via download_media()
- Progress callback (optional)
│
▼
5. Session saved for future use
Class Diagram
TelegramDownloader
├── __init__(api_id, api_hash, phone, session_name)
├── 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 (exceptions.py)
├── TelegramDLError (base)
├── AuthenticationError
├── ChannelNotFoundError
└── VideoNotFoundError
Session Management
- Sessions are stored locally as
.sessionfiles - First login requires OTP verification
- Session persists for future runs (no re-auth needed)
- Each unique session name creates a separate session
Error Handling
ConnectionError → TelegramDLError
SessionPasswordNeededError → AuthenticationError
ChannelNotFoundError → Custom exception
VideoNotFoundError → Custom exception
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.1.tar.gz
(7.7 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.1.tar.gz.
File metadata
- Download URL: telegram_dl-1.0.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5657faa5e633af29e85d00d95815015247603d0e804ac27a321fece0c58ed14
|
|
| MD5 |
aeaba87256e727d8f2450aa8d71cbebe
|
|
| BLAKE2b-256 |
03ca67512a3faf13ced30f70d34f4d5818a5ba75e45d1a24b8243fc28f092915
|
File details
Details for the file telegram_dl-1.0.1-py3-none-any.whl.
File metadata
- Download URL: telegram_dl-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.3 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 |
a3ac667d941f1cda7f80b00ba721ed2057c76fa73f9fa73abdaacf51b79a22d9
|
|
| MD5 |
fcb322de86bb51eba027b955eb43c807
|
|
| BLAKE2b-256 |
e6bc79f684169a2d01dd5645e8f94af7834ff5c675a1f5ef41f03abe678d9a35
|