2024-03-14 05:51:30 +08:00
## yt-dlp-bot - Video Download Telegram Bot 🇺🇦
2022-11-08 03:25:11 +08:00
2024-03-14 05:51:30 +08:00
Simple and reliable self-hosted Video Download Telegram Bot.
2022-02-04 06:21:27 +08:00
2024-05-31 03:39:51 +08:00
Version: 1.7. [Release details ](RELEASES.md ).
2022-11-08 03:25:11 +08:00
2022-11-06 04:20:27 +08:00

2022-02-04 06:21:27 +08:00
2023-02-26 01:58:34 +08:00
## Support the development
2023-07-05 02:08:28 +08:00
- [Buy me a coffee ](https://www.buymeacoffee.com/terletsky )
2023-02-26 01:58:34 +08:00
- PayPal [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick& hosted_button_id=MA6RKYAZH9DSA)
- Bitcoin wallet `14kMRS8SvfD2ydMSMEyAmefHV3Yynf9kAd`
2022-11-08 03:25:11 +08:00
## 😂 Features
2024-03-14 05:51:30 +08:00
* Download audio and free videos with Creative Commons (CC) License from [yt-dlp ](https://github.com/yt-dlp/yt-dlp ) sites to your storage.
2023-09-15 06:19:56 +08:00
* Upload downloaded media to Telegram.
* Interact with the bot in private or group chats.
* Trigger video downloads via link to the API.
* Track download tasks using the API.
2022-02-04 06:21:27 +08:00
2024-03-14 05:51:30 +08:00
## Disclaimer
- Intended to use only with videos that are under Creative Commons (CC) License
2022-02-04 06:21:27 +08:00
## ⚙ Quick Setup
2022-11-08 03:25:11 +08:00
2022-10-14 03:55:18 +08:00
1. Create Telegram bot using [BotFather ](https://t.me/BotFather ) and get your `token`
2022-11-08 03:25:11 +08:00
2. [Get your Telegram API Keys ](https://my.telegram.org/apps ) (`api_id` and `api_hash` )
2022-10-14 03:55:18 +08:00
3. [Find your Telegram User ID ](https://stackoverflow.com/questions/32683992/find-out-my-own-user-id-for-sending-a-message-with-telegram-api )
2023-02-03 01:31:07 +08:00
4. Copy `app_bot/config-example.yml` to `app_bot/config.yml`
5. Write `token` , `api_id` , `api_hash` to `app_bot/config.yml` by changing respective
2022-11-08 03:25:11 +08:00
placeholders
2023-04-02 05:33:45 +08:00
6. Write your Telegram user or group ID to the `allowed_users` -> `id` by replacing dummy
2023-03-28 06:38:50 +08:00
value and change `forward_group_id` value if you want to forward the video to
2023-07-12 04:36:09 +08:00
some group/channel when upload is enabled. Bot should be added to the group/channel to be able to send messages.
2023-09-15 06:19:56 +08:00
7. Change download media type for the user/group: `AUDIO` , `VIDEO` or `AUDIO_VIDEO`
2023-04-02 05:33:45 +08:00
in `app_bot/config.yml` 's variable `download_media_type` . Default `VIDEO`
2023-09-15 06:19:56 +08:00
8. If you want your downloaded audio/video to be uploaded back to the Telegram, set `upload_video_file` config variable
for your user/group in the `app_bot/config.yml` to `True`
2023-02-26 01:43:06 +08:00
9. Media `STORAGE_PATH` environment variable is located in
2022-11-08 03:25:11 +08:00
the `envs/.env_worker` file. By default, it's `/filestorage` path inside the
container. What you want is to map the real path to this inside
the `docker-compose.yml` file for `worker` service, e.g. if you're on Windows, next
strings mean container path `/filestorage` is mapped to real `D:/Videos` so your
videos will be saved to your `Videos` folder.
2022-10-14 03:55:18 +08:00
```yml
worker:
volumes:
- "D:/Videos:/filestorage"
```
2023-09-15 06:19:56 +08:00
10. Change application's `LOG_LEVEL` in `envs/.env_common` to `DEBUG` , `INFO` , `WARNING` , `ERROR` , `CRITICAL` if needed
2022-02-04 06:21:27 +08:00
## 🏃 Run
2022-11-08 03:25:11 +08:00
2022-11-03 01:56:19 +08:00
```bash
# Build base image
docker compose build base-image
# Build and run all services in detached mode
docker compose up --build -d -t 0 & & docker compose logs --tail 100 -f
2022-11-05 00:45:33 +08:00
# Stop all services
docker compose stop -t 0
2022-11-03 01:56:19 +08:00
```
2022-02-04 06:21:27 +08:00
Your telegram bot should send you a startup message:
2023-04-02 05:33:45 +08:00
`✨ <YOUR_BOT_NAME> started, paste a video URL(s) to start download` and that's it. After
2022-11-08 03:25:11 +08:00
pasting video URL(s) bot will send you appropriate message whether they were downloaded
or something went wrong.
2022-02-04 06:21:27 +08:00
2022-11-08 03:25:11 +08:00
## 💻 Advanced setup
2022-02-04 06:21:27 +08:00
2023-02-03 01:31:07 +08:00
1. If you want to change `yt-dlp` download options, go to the `app_worker/ytdl_opts`
2022-11-08 03:25:11 +08:00
directory, copy content from `default.py` to `user.py` and modify as you wish by
checking [available options ](https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/YoutubeDL.py#L180 )
.
2. Default max simultaneous video downloads by worker service is 2. Change
2023-02-05 03:34:03 +08:00
the `MAX_SIMULTANEOUS_DOWNLOADS` variable in `envs/.env_worker` to desired value but
2023-02-17 03:12:32 +08:00
keep in mind that default mounted volume size is 7168m (7GB) in `docker-compose.yml`
so it may be not enough if you download a lot of large videos at once.
3. `yt-dlp` will try to download video thumbnail if it exists. In other case Worker
service (particularly the FFmpeg process) will make a JPEG thumbnail from the
2022-11-08 03:25:11 +08:00
video. It's needed when you choose to upload the video to the Telegram chat. By
2023-02-17 03:12:32 +08:00
default, it will try to make it on the 10th second of the video, but if the video is
2022-11-08 03:25:11 +08:00
shorter, it will make it on `video length / 2` time point because the FFmpeg process
will error out. Change the `THUMBNAIL_FRAME_SECOND` variable if needed in
the `envs/.env_worker` file.
2023-01-22 06:50:45 +08:00
4. Max upload file size for non-premium Telegram user is 2GB (2147483648 bytes) which is
2023-02-03 01:31:07 +08:00
reflected in the example config `app_bot/config-example.yml` . If the configured user
2023-01-22 06:50:45 +08:00
is the premium user, you're allowed to upload files up to 4GB (4294967296 bytes) and
can change the default value stored in the `upload_video_max_file_size` config
variable.
2023-07-07 04:27:43 +08:00
5. If the website you want to download from requires authentication you can use your cookies by putting them into
the `app_worker/cookies/cookies.txt` file in the Netscape format.
2023-07-12 04:36:09 +08:00
6. If your country has an [Alpine Linux Mirror ](https://mirrors.alpinelinux.org/ ), you can speed up the image builds by:
1. Creating `apk_mirrors` text file and putting there your mirror urls, for example for Ukraine they are:
```
https://alpine.astra.in.ua/v3.17/main
https://alpine.astra.in.ua/v3.17/community
```
2. Adding `COPY apk_mirrors /etc/apk/repositories` to the third line in `base.Dockerfile` :
```dockerfile
FROM python:3.11-alpine
COPY apk_mirrors /etc/apk/repositories
...
```
3. Rebuild the images.
2022-03-18 04:09:03 +08:00
2022-02-04 06:21:27 +08:00
## 🛑 Failed download
2022-11-08 03:25:11 +08:00
If your URL can't be downloaded for some reason, you will see a message with error
details
2022-02-04 06:21:27 +08:00
2022-11-06 04:20:27 +08:00

2022-02-04 06:21:27 +08:00
## Access
2022-11-08 03:25:11 +08:00
- **API**: default port is `1984` and no auth. Port can be changed
in `docker-compose.yml`
2023-04-02 05:33:45 +08:00
- **RabbitMQ**: default credentials are located in `envs/.env_common`
2023-04-17 23:51:26 +08:00
- **PostgreSQL**: default credentials are located in `envs/.env_common` .
2022-02-04 06:21:27 +08:00
## API
2022-11-08 03:25:11 +08:00
By default, API service will run on your `localhost` and `1984` port. API endpoint
documentations lives at `http://127.0.0.1:1984/docs` .
2022-02-04 06:21:27 +08:00
2023-01-22 06:50:45 +08:00
| Endpoint | Method | Description |
|--------------------------------------------------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------|
| `/status` | `GET` | Get API healthcheck status, usually response is `{"status": "OK"}` |
| `/v1/yt-dlp` | `GET` | Get latest and currently installed `yt-dlp` version |
| `/v1/tasks/?include_meta=False&status=DONE` | `GET` | Get all tasks with filtering options like to include large file metadata and by task status: `PENDING` , `PROCESSING` , `FAILED` and `DONE` . |
| `/v1/tasks/f828714a-5c50-45de-87c0-3b51b7e04039?include_meta=True` | `GET` | Get info about task by ID |
| `/v1/tasks/latest?include_meta=True` | `GET` | Get info about latest task |
| `/v1/tasks/f828714a-5c50-45de-87c0-3b51b7e04039` | `DELETE` | Delete task by ID |
| `/v1/tasks` | `POST` | Create a download task by sending json payload `{"url": "<URL>"}` |
| `/v1/tasks/stats` | `GET` | Get overall tasks stats |
2022-02-04 06:21:27 +08:00
### API examples
2022-11-08 03:25:11 +08:00
2022-02-04 06:21:27 +08:00
1. `GET http://localhost:1984/v1/tasks/?include_meta=False&status=DONE&limit=2&offset=0`
2022-11-08 03:25:11 +08:00
Response
```json
[
{
"id": "7ab91ef7-461c-4ef6-a35b-d3704fe28e6c",
2024-03-14 05:51:30 +08:00
"url": "https://www.youtube.com/watch?v=PavYAOpVpJI",
2022-11-08 03:25:11 +08:00
"status": "DONE",
"source": "BOT",
"added_at": "2022-02-14T02:29:55.981622",
"created": "2022-02-14T02:29:57.211622",
"updated": "2022-02-14T02:29:59.595551",
"message_id": 621,
"file": {
"id": "4b1c63ed-3e32-43e6-a0b7-c7fc8713b268",
"created": "2022-02-14T02:29:59.597839",
"updated": "2022-02-14T02:29:59.597845",
2024-03-14 05:51:30 +08:00
"name": "[Drone Freestyle] Mountain Landscape With Snow | Free Stock Footage | Creative Common Video",
2022-11-08 03:25:11 +08:00
"ext": "mp4"
}
}
]
```
2022-02-04 06:21:27 +08:00
2. `POST http://localhost:1984/v1/tasks`
2022-11-08 03:25:11 +08:00
Request
```json
{
2024-03-14 05:51:30 +08:00
"url": "https://www.youtube.com/watch?v=PavYAOpVpJI",
2023-02-26 02:14:29 +08:00
"download_media_type": "AUDIO_VIDEO",
2024-04-26 04:09:50 +08:00
"save_to_storage": false,
"custom_filename": "cool.mp4",
"automatic_extension": false
2022-11-08 03:25:11 +08:00
}
```
Response
```json
{
"id": "5ac05808-b29c-40d6-b250-07e3e769d8a6",
2024-03-14 05:51:30 +08:00
"url": "https://www.youtube.com/watch?v=PavYAOpVpJI",
2022-11-08 03:25:11 +08:00
"source": "API",
"added_at": "2022-02-14T00:35:25.419962+00:00"
}
```
2022-02-04 06:21:27 +08:00
3. `GET http://localhost:1984/v1/tasks/stats`
2022-11-08 03:25:11 +08:00
Response
```json
{
"total": 39,
"unique_urls": 5,
"pending": 0,
"processing": 0,
"failed": 26,
"done": 13
}
```