Advanced Usage
Master advanced features and techniques for power users.
Built-in Advanced Features
tgdl includes several powerful features automatically enabled:
🔄 Automatic Rate Limit Handling (FloodWait)
What it does: - Automatically detects when Telegram rate-limits your downloads - Waits the required time before resuming (up to 5 minutes max) - Shows countdown so you know what's happening - Continues automatically after the wait
Example:
⏳ Rate limited by Telegram. Auto-waiting 45s...
Resuming in 45s...
Resuming in 44s...
...
Resuming in 1s...
Resuming now...
When it happens: - Making too many requests too quickly - Can occur with high concurrent values (20+) - Normal and recoverable - no action needed
How to prevent:
# Reduce concurrent downloads if frequently rate-limited
tgdl download -c 1234567890 --concurrent 5 # Instead of 20
🔗 Connection Pooling
What it does: - Reuses single Telegram connection for all downloads - Reduces connection overhead - Improves speed for many small files - Automatically handled
Benefits: - Faster multi-file downloads - Lower memory usage - More stable connection - No configuration needed
🔐 Hash-Based Deduplication
What it does: - Computes MD5 hash of each downloaded file - Detects identical files (even if renamed) - Prevents duplicate downloads automatically - Works even after resume
How it works:
1. First file downloaded: 12345.jpg → hash stored
2. Later, identical file in different message: 12346.jpg
3. Hash matches existing file: automatically skipped
4. Shows: ⊘ Duplicate of 12345.jpg — removing copy.
Benefits: - Saves bandwidth - Saves storage - Automatic duplicate detection - Works across interruptions
Example:
# Download initially
tgdl download -c 1234567890 --limit 100
# Later, download more
tgdl download -c 1234567890 # Skips exact duplicates automatically
♻️ Automatic Retry with Backoff
What it does: - Automatically retries failed downloads - Uses exponential backoff (2 seconds minimum) - Up to 3 attempts per file - Continues with next file on failure
How it works:
File download fails
↓ (Attempt 1 of 3)
Wait 2 seconds
↓ (Retry)
Fails again
↓ (Attempt 2 of 3)
Wait 4 seconds (2 × 2)
↓ (Retry)
Fails again
↓ (Attempt 3 of 3)
Wait 8 seconds (4 × 2)
↓ (Final retry)
Fails → Move to next file
When it triggers: - Network timeout - Connection interrupted - Temporary server error - File temporarily unavailable
Benefits: - Handles temporary network issues - Recovers from transient errors - No user intervention needed - Continues download automatically
📝 Partial File Resume
What it does:
- Automatically resumes incomplete downloads
- Detects .tgdl_partial files
- Continues from last byte (not from start)
- Excluded from deduplication
How it works:
Download interrupted at 25% → 12345.tgdl_partial (25MB of 100MB)
↓
Run same command again
↓
Automatically resumes from 25%
↓
Downloads remaining 75MB
↓
Final file: 12345.jpg (complete 100MB)
Benefits: - Saves time on re-download - Saves bandwidth - Works with large files - Automatic and transparent
Message ID Ranges
Overview
Download specific message ID ranges from channels, groups, or bots.
Use cases: - Get specific portion of a channel - Re-download specific messages - Skip old content - Targeted backups
Basic Range Syntax
Understanding Message IDs
Message IDs are sequential:
Message ID 1 - First message in channel
Message ID 2 - Second message
Message ID 3 - Third message
...
Message ID N - Latest message
Finding message IDs:
- Right-click message in Telegram Desktop:
- Select "Copy Message Link"
- Link contains ID:
https://t.me/channel/12345 -
Last number (12345) is message ID
-
From tgdl output:
-
Filename starts with message ID (12345)
-
Try download-link:
- Downloads that specific message
Range Examples
Get Recent Messages
Get Old Messages
Get Middle Section
Skip First N Messages
Single Message
Combining with Filters
# Videos in range 100-200
tgdl download -c 1234567890 --min-id 100 --max-id 200 -v
# Photos under 5MB in range 500-1000
tgdl download -c 1234567890 --min-id 500 --max-id 1000 -p --max-size 5MB
Range Limitations
Range vs Limit
When using both:
- Range: Messages 100-200 considered
- Limit: Only first 10 files downloaded
- Result: Up to 10 files from range 100-200
Empty Ranges
If range has no media:
Parallel Downloads
Concurrent Downloads
Default: 5 parallel downloads
Range: 1-50
Flag: --concurrent N
Performance Tuning
# Slow/unstable connection (1 at a time)
tgdl download -c 1234567890 --concurrent 1
# Normal connection (default)
tgdl download -c 1234567890
# Fast connection (10 parallel)
tgdl download -c 1234567890 --concurrent 10
# Very fast connection (20 parallel)
tgdl download -c 1234567890 --concurrent 20
# Maximum (50 parallel - use with caution)
tgdl download -c 1234567890 --concurrent 50
Finding Optimal Value
Test Different Values
# Test with small limit
time tgdl download -c ID --limit 20 --concurrent 5
time tgdl download -c ID --limit 20 --concurrent 10
time tgdl download -c ID --limit 20 --concurrent 20
Choose value with: - Fastest speed - No errors/timeouts - Stable progress bars
Factors Affecting Performance
Internet speed: - Slow (< 10 Mbps): 1-3 - Medium (10-50 Mbps): 5-10 - Fast (50-100 Mbps): 10-20 - Very fast (> 100 Mbps): 20-50
File sizes: - Large files (> 100MB): Lower concurrent (5-10) - Small files (< 10MB): Higher concurrent (10-20)
System resources: - Limited RAM/CPU: Lower concurrent (1-5) - Powerful system: Higher concurrent (10-20)
Telegram limits: - Too high may trigger rate limits - May see temporary flood waits - If errors occur, reduce value
Automation & Scripting
Scheduled Downloads
Windows Task Scheduler
Create batch file update_channel.bat:
Schedule in Task Scheduler:
1. Open Task Scheduler
2. Create Basic Task
3. Trigger: Daily at 2 AM
4. Action: Start program update_channel.bat
Linux/macOS Cron
Edit crontab:
Add job:
With logging:
Backup Script
backup_all.sh (Linux/macOS):
#!/bin/bash
# Configuration
CHANNELS=(1234567890 9876543210)
OUTPUT_BASE="/backups/telegram"
LOG_FILE="/var/log/tgdl_backup.log"
# Function to log messages
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}
log "Starting Telegram backup..."
# Loop through channels
for channel in "${CHANNELS[@]}"; do
log "Backing up channel $channel..."
if tgdl download -c "$channel" -o "$OUTPUT_BASE/$channel" >> "$LOG_FILE" 2>&1; then
log "✓ Successfully backed up channel $channel"
else
log "✗ Failed to backup channel $channel"
fi
done
log "Backup complete!"
Make executable:
Run:
backup_all.bat (Windows):
@echo off
setlocal enabledelayedexpansion
set CHANNELS=1234567890 9876543210
set OUTPUT_BASE=C:\backups\telegram
set LOG_FILE=C:\logs\tgdl_backup.log
echo [%date% %time%] Starting Telegram backup... >> %LOG_FILE%
for %%c in (%CHANNELS%) do (
echo [%date% %time%] Backing up channel %%c... >> %LOG_FILE%
tgdl download -c %%c -o %OUTPUT_BASE%\%%c >> %LOG_FILE% 2>&1
if !errorlevel! equ 0 (
echo [%date% %time%] Successfully backed up channel %%c >> %LOG_FILE%
) else (
echo [%date% %time%] Failed to backup channel %%c >> %LOG_FILE%
)
)
echo [%date% %time%] Backup complete! >> %LOG_FILE%
Multi-Channel Download
Download from multiple channels:
#!/bin/bash
# Channel IDs
TECH_NEWS=1234567890
DAILY_PICS=9876543210
VIDEO_ARCHIVE=1122334455
# Download videos from tech channel
tgdl download -c $TECH_NEWS -v --max-size 100MB -o ~/Videos/Tech
# Download photos from pictures channel
tgdl download -c $DAILY_PICS -p -o ~/Pictures/Daily
# Download everything from archive
tgdl download -c $VIDEO_ARCHIVE -o ~/Archive/Videos
Error Handling
Robust script with error handling:
#!/bin/bash
CHANNEL_ID=1234567890
MAX_RETRIES=3
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if tgdl download -c $CHANNEL_ID; then
echo "Download successful!"
exit 0
else
RETRY_COUNT=$((RETRY_COUNT + 1))
echo "Download failed. Retry $RETRY_COUNT of $MAX_RETRIES..."
sleep 60 # Wait 1 minute before retry
fi
done
echo "Download failed after $MAX_RETRIES attempts."
exit 1
Output Organization
Custom Directory Structure
By entity type:
tgdl download -c 1234567890 -o ~/Telegram/Channels/TechNews
tgdl download -g 9876543210 -o ~/Telegram/Groups/MyGroup
tgdl download -b 1122334455 -o ~/Telegram/Bots/FileBot
By media type:
tgdl download -c 1234567890 -v -o ~/Videos/Telegram
tgdl download -c 1234567890 -p -o ~/Pictures/Telegram
tgdl download -c 1234567890 -a -o ~/Music/Telegram
By date:
Hierarchical:
BASE=~/Telegram
CHANNEL=TechNews
DATE=$(date +%Y-%m-%d)
tgdl download -c 1234567890 -o $BASE/$CHANNEL/$DATE
# Result: ~/Telegram/TechNews/2025-12-25/
Filename Understanding
Format: {message_id}.{extension}
Examples:
12345.jpg - Photo from message 12345
12346.mp4 - Video from message 12346
12347.pdf - Document from message 12347
12348_2.jpg - Second file from message 12348
Post-processing script:
#!/bin/bash
# Rename files with timestamp
for file in *.jpg; do
# Get message ID
id=$(echo $file | cut -d'.' -f1)
# Get file modification time
timestamp=$(stat -f %Sm -t "%Y%m%d_%H%M%S" "$file")
# Rename
mv "$file" "${timestamp}_${id}.jpg"
done
Advanced Filtering
Complex Filter Combinations
# Only large videos
tgdl download -c 1234567890 -v --min-size 100MB --max-size 1GB
# Small photos and documents
tgdl download -c 1234567890 -p -d --max-size 10MB
# Audio files in specific range
tgdl download -c 1234567890 -a --min-id 1000 --max-id 2000
Selective Downloads by Type
Photos only:
Videos only:
Documents only:
Everything except videos:
Size-Based Strategies
Quick preview (small files only):
High quality only (large files):
Storage-conscious:
Performance Optimization
Network Optimization
Bandwidth limit simulation:
Avoid peak hours:
Storage Optimization
Free up space before download:
# Check available space
df -h
# Clean old files
find ~/downloads -type f -mtime +30 -delete # Remove files older than 30 days
# Then download
tgdl download -c 1234567890
Download to external drive:
# Linux/macOS
tgdl download -c 1234567890 -o /mnt/external/telegram
# Windows
tgdl download -c 1234567890 -o E:\Telegram
Resume Strategy
For large channels:
# Download in batches
tgdl download -c 1234567890 --max-id 1000
tgdl download -c 1234567890 --min-id 1001 --max-id 2000
tgdl download -c 1234567890 --min-id 2001 --max-id 3000
# etc...
With automatic retry:
#!/bin/bash
until tgdl download -c 1234567890; do
echo "Download interrupted, retrying in 60s..."
sleep 60
done
Working with Private Channels
Private Channel Links
Format:
Extract channel ID:
- From link: https://t.me/c/1234567890/123
- Channel ID: 1234567890
Download:
Access Requirements
Private Channel Access
You must be a member of the private channel!
Symptoms of no access:
Solution:
1. Join channel in Telegram app
2. Run tgdl channels to verify it's listed
3. Try download again
Monitoring & Logging
Save Download Logs
# Redirect output to file
tgdl download -c 1234567890 > download.log 2>&1
# Append to existing log
tgdl download -c 1234567890 >> download.log 2>&1
# View log in real-time
tail -f download.log
Extract Statistics
# Count downloaded files
grep "✓ Downloaded:" download.log | wc -l
# Total size downloaded
grep "✓ Downloaded:" download.log | grep -o "[0-9.]*[KMGT]B" | paste -sd+ | bc
# Failed downloads
grep "✗" download.log
Progress Tracking File
Location: ~/.tgdl/progress.json
Format:
Usage:
# View last downloaded message ID
cat ~/.tgdl/progress.json | jq
# Reset progress (start over)
echo "{}" > ~/.tgdl/progress.json
# Backup progress
cp ~/.tgdl/progress.json ~/.tgdl/progress.json.backup
Best Practices
1. Test Before Bulk Download
Always Test First
2. Use Appropriate Filters
Save Time & Space
3. Regular Backups
Automate Updates
4. Monitor Progress Files
Track State
5. Handle Errors Gracefully
Robust Scripts
Troubleshooting Advanced Issues
FloodWaitError
Symptom:
Cause: Too many requests to Telegram
Solutions:
# Reduce concurrent downloads
tgdl download -c 1234567890 --concurrent 3
# Add delay between batches (script)
tgdl download -c 1234567890 --max-id 1000
sleep 300 # Wait 5 minutes
tgdl download -c 1234567890 --min-id 1001
Memory Issues
Symptom: System slows down or crashes
Solutions:
# Reduce concurrent downloads
tgdl download -c 1234567890 --concurrent 2
# Download in smaller batches
tgdl download -c 1234567890 --limit 50
# Use smaller files
tgdl download -c 1234567890 --max-size 50MB
Incomplete Downloads
Symptom: Files partially downloaded
Check:
# Find files with unusual sizes
find downloads -type f -size 0 # Empty files
find downloads -type f -size -1k # Very small files
Solution:
# Delete incomplete files
find downloads -type f -size -1k -delete
# Re-download
tgdl download -c 1234567890