A simple Telegram bot that downloads videos or extracts audio from YouTube links using yt-dlp and exposes the downloaded files via a public URL you host.
The bot provides two commands:
- /v <youtube_url> — download the full video
- /a <youtube_url> — download audio only (MP3)
- Uses yt-dlp for reliable YouTube downloading
- Optional audio-only extraction via ffmpeg
- Generates a public download link based on your configured domain
- Asynchronous, built on python-telegram-bot v20+
- Python 3.10+
- Telegram Bot Token (from @BotFather)
- System packages:
- yt-dlp
- ffmpeg
- A publicly accessible domain or URL to serve the downloaded files (e.g., Nginx serving a directory)
- Debian/Ubuntu:
- sudo apt update && sudo apt install -y yt-dlp ffmpeg
- Fedora:
- sudo dnf install -y yt-dlp ffmpeg
- Arch:
- sudo pacman -S yt-dlp ffmpeg
- macOS (Homebrew):
- brew install yt-dlp ffmpeg
- Open Telegram and start a chat with @BotFather.
- Send the command /newbot and follow the prompts (choose a name and a username).
- BotFather will reply with an HTTP API token — copy this token; you will use it as BOT_TOKEN.
- You can also set your bot description, profile photo, and commands via BotFather if you wish.
Create a .env file in the project root or provide these variables via your environment.
Required variables:
- BOT_TOKEN — the token from @BotFather
- DOWNLOAD_DOMAIN — the base public URL from which your files will be served (e.g., https://files.example.com)
- DOWNLOAD_PATH — absolute path on the server where the bot will write files (this directory must be served by your web server)
Notes:
- The bot will create the DOWNLOAD_PATH directory if it doesn’t exist.
- Ensure your web server (e.g., Nginx/Apache/Caddy) serves DOWNLOAD_PATH at DOWNLOAD_DOMAIN so that generated links are reachable.
-
Clone the repository: git clone https://github.com/innerfly/yt-dlp-telegram-bot.git cd yt-dlp-telegram-bot
-
Create and activate a virtual environment (recommended): python -m venv .venv source .venv/bin/activate
-
Install Python dependencies: pip install -r requirements.txt
-
Ensure yt-dlp and ffmpeg are installed at the system level (see above).
-
Set environment variables (via .env or your process manager) as described.
-
python bot.py
-
Use a process manager (supervisor, systemd, docker, etc.) to keep the bot running.
The bot will start polling. In your Telegram client, message your bot /v <youtube_url>, and if the download succeeds, the bot replies with a public link like:
https://files.example.com/Some_Title-VIDEOID.mp4
- Downloads are publicly accessible if your DOWNLOAD_DOMAIN is public. Consider periodic cleanup or restricted access if necessary.
- Never commit your .env or bot token to version control.