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
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
- 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.1.0.tar.gz
(8.8 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.1.0.tar.gz.
File metadata
- Download URL: telegram_dl-1.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
246b6e534d38fc4f753b76cb6a9cead026b41a491757df2c00e9439690141a59
|
|
| MD5 |
a847826bfa7d68374ea571477b59c982
|
|
| BLAKE2b-256 |
1ed9efa0d319a0db2f227bf8d0ac85e21b69d07acdb9219cd174ec4fb7c648de
|
File details
Details for the file telegram_dl-1.1.0-py3-none-any.whl.
File metadata
- Download URL: telegram_dl-1.1.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7af3a38f4dd03550e3a2bc4e04d9c126a5ccc72d3b78333633361fa7b49a7458
|
|
| MD5 |
e607feb8ff9884c9b689b3e20d2e9c0f
|
|
| BLAKE2b-256 |
222eca6a9f852905539199d25d787888dcc0ba9780b2cf66924b1191ca5ae26a
|