Self-hosted YouTube Download Telegram Bot 🇺🇦
Find a file
Taras Terletsky 45f2d33161
Merge pull request #264 from tropicoo/dependabot/pip/sqlalchemy-utils-0.41.2
Bump sqlalchemy-utils from 0.41.1 to 0.41.2
2024-03-27 22:08:35 +02:00
.assets Update README 2022-11-05 22:20:27 +02:00
.github Refinements 2022-10-13 22:55:18 +03:00
app_api Version 1.5 2024-03-20 19:26:35 +02:00
app_bot Update 2024-03-23 19:12:27 +02:00
app_worker Update 2024-03-23 19:12:27 +02:00
envs Fix #163 [Caption too long] 2023-09-23 19:18:05 +03:00
yt_shared Bump sqlalchemy-utils from 0.41.1 to 0.41.2 2024-03-27 20:08:30 +00:00
.dockerignore Version 1.5 2024-03-20 19:26:35 +02:00
.editorconfig Fix #185 2023-10-04 20:46:39 +03:00
.env Version 1.0. Details in /.releases/release_1.0.md 2023-02-25 19:25:39 +02:00
.gitignore Version 1.1 2023-03-11 00:13:16 +02:00
.ruff.toml Version 1.5 2024-03-20 19:26:35 +02:00
base.Dockerfile Update 2024-03-23 19:12:27 +02:00
docker-compose.yml Add api workers config value 2023-08-02 23:53:49 +03:00
LICENSE Initial commit 2022-02-04 00:16:17 +02:00
README.md Version 1.5 2024-03-20 19:26:35 +02:00
RELEASES.md Version 1.5 2024-03-20 19:26:35 +02:00
start.py Refinements and fix #252 2024-03-13 23:51:30 +02:00

yt-dlp-bot - Video Download Telegram Bot 🇺🇦

Simple and reliable self-hosted Video Download Telegram Bot.

Version: 1.5. Release details.

frames

Support the development

😂 Features

  • Download audio and free videos with Creative Commons (CC) License from yt-dlp sites to your storage.
  • 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.

Disclaimer

  • Intended to use only with videos that are under Creative Commons (CC) License

⚙ Quick Setup

  1. Create Telegram bot using BotFather and get your token
  2. Get your Telegram API Keys (api_id and api_hash)
  3. Find your Telegram User ID
  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 placeholders
  6. Write your Telegram user or group ID to the allowed_users -> id by replacing dummy value and change forward_group_id value if you want to forward the video to some group/channel when upload is enabled. Bot should be added to the group/channel to be able to send messages.
  7. Change download media type for the user/group: AUDIO, VIDEO or AUDIO_VIDEO in app_bot/config.yml's variable download_media_type. Default VIDEO
  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
  9. Media STORAGE_PATH environment variable is located in 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.
      worker:
        volumes:
          - "D:/Videos:/filestorage"
    
  10. Change application's LOG_LEVEL in envs/.env_common to DEBUG, INFO, WARNING, ERROR, CRITICAL if needed

🏃 Run

# 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

# Stop all services
docker compose stop -t 0

Your telegram bot should send you a startup message: ✨ <YOUR_BOT_NAME> started, paste a video URL(s) to start download and that's it. After pasting video URL(s) bot will send you appropriate message whether they were downloaded or something went wrong.

💻 Advanced setup

  1. If you want to change yt-dlp download options, go to the app_worker/ytdl_opts directory, copy content from default.py to user.py and modify as you wish by checking available options .
  2. Default max simultaneous video downloads by worker service is 2. Change the MAX_SIMULTANEOUS_DOWNLOADS variable in envs/.env_worker to desired value but 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 video. It's needed when you choose to upload the video to the Telegram chat. By default, it will try to make it on the 10th second of the video, but if the video is 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.
  4. Max upload file size for non-premium Telegram user is 2GB (2147483648 bytes) which is reflected in the example config app_bot/config-example.yml. If the configured user 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.
  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.
  6. If your country has an Alpine Linux Mirror, 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:
      FROM python:3.11-alpine
      
      COPY apk_mirrors /etc/apk/repositories
      ...
      
    3. Rebuild the images.

🛑 Failed download

If your URL can't be downloaded for some reason, you will see a message with error details

frames

Access

  • API: default port is 1984 and no auth. Port can be changed in docker-compose.yml
  • RabbitMQ: default credentials are located in envs/.env_common
  • PostgreSQL: default credentials are located in envs/.env_common.

API

By default, API service will run on your localhost and 1984 port. API endpoint documentations lives at http://127.0.0.1:1984/docs.

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

API examples

  1. GET http://localhost:1984/v1/tasks/?include_meta=False&status=DONE&limit=2&offset=0

    Response

    [
        {
            "id": "7ab91ef7-461c-4ef6-a35b-d3704fe28e6c",
            "url": "https://www.youtube.com/watch?v=PavYAOpVpJI",
            "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",
                "name": "[Drone Freestyle] Mountain Landscape With Snow | Free Stock Footage | Creative Common Video",
                "ext": "mp4"
            }
        }
    ]
    
  2. POST http://localhost:1984/v1/tasks

    Request

    {
        "url": "https://www.youtube.com/watch?v=PavYAOpVpJI",
        "download_media_type": "AUDIO_VIDEO",
        "save_to_storage": false
    }
    

    Response

    {
        "id": "5ac05808-b29c-40d6-b250-07e3e769d8a6",
        "url": "https://www.youtube.com/watch?v=PavYAOpVpJI",
        "source": "API",
        "added_at": "2022-02-14T00:35:25.419962+00:00"
    }
    
  3. GET http://localhost:1984/v1/tasks/stats

    Response

    {
        "total": 39,
        "unique_urls": 5,
        "pending": 0,
        "processing": 0,
        "failed": 26,
        "done": 13
    }