Async Inline YouTube-DL for Pyrogram-based Telegram Bots
A production-ready Python library that wraps yt-dlp with an async, inline-friendly interface for Pyrogram Telegram bots β search, download, and upload media from 1000+ sites directly inside Telegram.
| Feature | Details | |
|---|---|---|
| β‘ | Fully Async | Built on asyncio, aiohttp, and aiosqlite β non-blocking from search to upload |
| ποΈ | Smart Caching | SHA-1 hashed SQLite cache eliminates duplicate downloads and speeds up repeat queries |
| π | 1000+ Sites | Powered by yt-dlp β YouTube, Dailymotion, Vimeo, SoundCloud, Twitter, and many more |
| ποΈ | Quality Selector | Inline buttons let users pick video resolution (144p β 1440p) or audio bitrate (128β320 kbps) |
| π€ | Telegram Upload | Uploads audio/video to Telegram with real-time progress bars, metadata, and thumbnails |
| π | External Downloader | Optional Aria2c integration for faster, resumable downloads |
| πΌοΈ | Thumbnails | Auto-fetches best thumbnail, uploads to Telegraph, and embeds album art for audio |
| π§Ή | Context Manager | async with iYTDL(...) as ytdl: β clean resource management out of the box |
src/iytdl/
βββ main.py # iYTDL β primary class (inherits Extractor + Downloader + Uploader)
βββ extractors.py # yt-dlp format extraction & inline button generation
βββ downloader.py # async video/audio downloading with progress hooks
βββ upload_lib/
β βββ uploader.py # Telegram upload with progress tracking
β βββ progress.py # upload progress bar renderer
β βββ functions.py # media metadata helpers (ffprobe, thumbnails)
βββ sql_cache.py # aiosqlite-backed search/URL cache
βββ formatter.py # search result β Telegram message formatter
βββ processes.py # cancellable download/upload process manager
βββ types/ # SearchResult, ExternalDownloader, Aria2c dataclasses
βββ constants.py # URLs, regex patterns
βββ exceptions.py # NoResultFoundError, DownloadFailedError
βββ utils.py # run_sync, humanbytes, time_formater, telegraph upload
pip install iytdlfrom iytdl import iYTDL
async def main():
async with iYTDL(log_group_id=LOG_GROUP_ID, cache_path="cache") as ytdl:
# π Search YouTube
result = await ytdl.search("never gonna give you up")
print(result.msg)
# π Parse any supported URL
result = await ytdl.parse("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
# β¬οΈ Download video
key = await ytdl.download(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
uid="mp4",
downtype="video",
update=message, # Pyrogram Message or CallbackQuery
)
# π€ Upload to Telegram
await ytdl.upload(client, key, "video", message)iYTDL(
log_group_id: Union[int, str], # Telegram chat ID for uploading media
session: Optional[ClientSession], # aiohttp session (auto-created if None)
silent: bool = False, # suppress yt-dlp stdout
download_path: str = "downloads", # local download directory
cache_path: str = "", # SQLite cache directory
delete_media: bool = False, # auto-cleanup after upload
external_downloader: Optional[ExternalDownloader] = None, # e.g. Aria2c(...)
ffmpeg_location: str = "ffmpeg", # custom ffmpeg path
)| Method | Description |
|---|---|
search(query) |
Search YouTube, returns paginated SearchResult with inline buttons |
parse(search_query, extract=True) |
Auto-detect: YouTube URL β extract, generic URL β extract, text β search |
next_result(key, index) |
Navigate cached search results (pagination) |
extract_info_from_key(key) |
Get download buttons for a cached URL/video ID |
download(url, uid, downtype, update) |
Download media with live progress bar in Telegram |
upload(client, key, downtype, update) |
Upload downloaded media to Telegram with progress |
get_download_button(yt_id) |
Generate quality-selection inline keyboard for a YouTube video |
listview(key) |
Grid view of all cached search results |
paste_to_tg(title, content) |
Paste content to Telegraph |
get_ytthumb(yt_id) |
Fetch best available YouTube thumbnail |
# Pagination (Back / Next)
r"^yt_(back|next)\|(?P<key>[\w-]{5,11})\|(?P<pg>\d+)$"
# List all results
r"^yt_listall\|(?P<key>[\w-]{5,11})$"
# Extract info
r"^yt_extract_info\|(?P<key>[\w-]{5,11})$"
# Download (generic + YouTube)
r"yt_(?P<mode>gen|dl)\|(?P<key>[\w-]+)\|(?P<choice>[\w-]+)\|(?P<dl_type>a|v)$"
# Cancel download/upload
r"^yt_cancel\|(?P<process_id>[\w\.]+)$"from iytdl.types.external_downloader import Aria2c
async with iYTDL(
log_group_id=LOG_GROUP_ID,
external_downloader=Aria2c(executable_path="/usr/bin/aria2c"),
cache_path="cache",
) as ytdl:
...| Dependency | Purpose |
|---|---|
| Python β₯ 3.8 | Runtime |
| yt-dlp | Media extraction & download engine |
| Pyrogram | Telegram Bot framework |
| FFmpeg | Audio/video post-processing |
| Aria2c | (optional) External download accelerator |
git clone https://github.com/iytdl/iytdl.git
cd iytdl
poetry installRun tests:
pytest tests/Pre-commit hooks:
pre-commit installBuild wheel:
chmod +x scripts/install.sh && ./scripts/install.shLive Demo Bot: @iytdl_bot
YouTube.py β Full bot module built with iYTDL
This project is licensed under the GNU General Public License v3.0.
Built with β€οΈ by Himanshu







