Skip to main content

tg-mpv

PyPI Python Tests License: GPL v3+

Search for videos through a Telegram bot and play them in mpv — without downloading the whole file first.

tg-mpv "movie name"

tg-mpv signs in to Telegram as your own account, asks the bot, lets you pick a result, and starts a tiny localhost-only HTTP server that turns mpv's Range requests into partial Telegram downloads. mpv plays and seeks freely; only the bytes you actually watch are fetched.

Works on Linux, macOS and Windows.

Features

  • Streams straight into mpv — seeking works, nothing is saved to disk.
  • Video list with load more, filter (f 1080p, f S04) and sort by size.
  • Next episode offer when an episode ends; resume where you stopped; --start 00:36:06.
  • --continue picks up the last video (or the next episode) — from your catalog, no search needed.
  • Quality preference (--quality 1080p, --max-size 2GB, --auto).
  • Subtitles sent by the bot are loaded into mpv.
  • Smarter searching: retries with reworded queries and learns how your bot behaves.
  • --doctor diagnoses problems; --install-mpv installs mpv for you.
  • Safe by design: localhost only, no shell, session never printed.

Install

You need Python 3.11+, mpv, a Telegram account and Telegram API credentials.

pipx install tg-mpv        # recommended: isolated install
# or
pip install tg-mpv

pip installs the Python dependencies (Telethon, aiohttp). mpv is a native program, so pip cannot install it — run:

tg-mpv --install-mpv

It finds your package manager (Scoop/Chocolatey, Homebrew, apt/dnf/pacman/zypper), shows the exact command and runs it only after you confirm. If you prefer to do it yourself:

OS Install mpv
Linux sudo apt install mpv · sudo dnf install mpv · sudo pacman -S mpv
macOS brew install mpv
Windows scoop install mpv · choco install mpvio · or download from https://mpv.io/installation

Make sure mpv is on your PATH, or set MPV to its full path.

If the tg-mpv command is not found (common on Windows), use python -m tg_mpv ....

Quick start

1. Get API credentials at https://my.telegram.org/apps (an api_id and an api_hash) and set them:

Shell Command
bash / zsh export TG_MPV_API_ID=123456 and export TG_MPV_API_HASH='xxxxxxxx'
PowerShell / cmd setx TG_MPV_API_ID "123456" and setx TG_MPV_API_HASH "xxxxxxxx", then open a new terminal

Credentials are read only from the environment; they are never written to disk, logged or printed.

2. Log in once:

tg-mpv --login

You are asked for your phone number, the code Telegram sends, and your 2FA password if enabled. Only the resulting session is stored. That session file is a credential (anyone holding it can act as your account) — treat it like an SSH private key. It is separate from Telegram Desktop; tg-mpv never touches Telegram Desktop's data.

3. Check everything and play:

tg-mpv --doctor
tg-mpv "movie name"

Usage

tg-mpv [options] "query"
Option What it does
--bot @name Bot to ask (default @weeceebot, or TG_MPV_BOT)
--start HH:MM:SS / --start begin Start position (otherwise you are asked)
--quality 1080p Preferred resolution; the best match is marked, press Enter to take it
--max-size 2GB Prefer files not bigger than this
--auto Pick automatically when the list is just one episode in several qualities
--filter TEXT / --sort size Pre-filter or sort the list
--no-subs Do not load subtitles
--continue Resume the last unfinished video, or open the next episode of the last finished one
--history Show recent videos and replay one
--doctor Check the setup and explain any problem
--config Create/show config.toml and the effective settings
--install-mpv Install mpv with your package manager (asks first)
--login / --logout / --status Manage the Telegram session
--debug Print what the bot sent (never secrets)
--version Print the version

Typical session:

$ tg-mpv "Mr Robot"
Searching @weeceebot for: Mr Robot

Available videos:
  [1] Mr.Robot S04E12   1.1 GB  (1080p, 49:30)   <- best match
  [2] Mr.Robot S04E13   1.2 GB  (1080p, 50:55)
  number = play | Enter = 1 | n = load more | f TEXT = filter | s = sort by size | q = quit
Select [1]: 2

Start playback:
  [1] From the beginning
  [2] From a specific time
Select: 1
Launching mpv...

In the video list: a number plays it, n loads more results, f 1080p / f S04 filters, f clears the filter, s toggles sorting by size, q quits.

Configuration

Everything works from environment variables; a config file is optional (tg-mpv --config creates a commented one).

Variable Default Meaning
TG_MPV_API_ID, TG_MPV_API_HASH (required) Telegram API credentials
TG_MPV_BOT @weeceebot Bot username
TG_MPV_QUALITY — Preferred resolution, e.g. 1080p
TG_MPV_MAX_SIZE — Preferred maximum size, e.g. 2GB
TG_MPV_AUTO false Auto-pick when unambiguous
TG_MPV_NO_SUBS false Never load subtitles
TG_MPV_HOST / TG_MPV_PORT 127.0.0.1 / 0 Local server (host is forced to localhost)
TG_MPV_CHUNK_SIZE 524288 Telegram request size in bytes
TG_MPV_CACHE_SECS 45 mpv --cache-secs
MPV mpv Path to the mpv binary
TG_MPV_CONFIG_DIR (see below) Where settings and data live

Settings, session and history are stored in:

  • Linux / macOS: ~/.config/tg-mpv (or $XDG_CONFIG_HOME/tg-mpv)
  • Windows: %APPDATA%\tg-mpv

config.toml (all optional, environment variables win):

[telegram]
bot = "@weeceebot"

[preferences]
quality = "1080p"
max_size = "2GB"
auto = false
no_subs = false

[mpv]
cache_secs = 45

tg-mpv also keeps small JSON files there: history.json (resume positions), catalog.json (videos it has seen, so the next episode can be opened without a search) and prefs.json (what it learned about your bot and series).

How it works

 Telegram ──MTProto (your account)──▶ tg-mpv ──HTTP Range, 127.0.0.1 only──▶ mpv
   bot search · partial downloads      │ bot.py        finds the video
                                       │ server.py     localhost Range server
                                       └ stream.py     Range → Telegram iter_download
Module Responsibility
cli.py Arguments, menus, playback loop, next episode / resume
bot.py Talks to the bot: bursts of messages, buttons, "load more"
media.py Video/subtitle detection, episode and resolution helpers
server.py, stream.py Localhost Range server; Range → Telegram partial download, retries
player.py mpv process, position tracking, cleanup
state.py History, catalog, learned preferences, single-instance lock
config.py, platform_utils.py Settings and everything OS-specific
doctor.py, installer.py --doctor and --install-mpv

Only the one selected video is ever served, and only at 127.0.0.1. Range requests are validated (416 for out-of-bounds, 400 for malformed); chunks are forwarded as they arrive, so memory stays flat for any file size.

Security

  • The HTTP server binds only to 127.0.0.1; it serves exactly one path for exactly one media object.
  • No shell=True / os.system() anywhere; mpv and the mpv installer are started with argument lists.
  • The Telegram session, API hash, login codes and 2FA password are never logged or printed (not even with --debug).
  • The server and mpv are stopped when you quit, press Ctrl+C, or close the terminal.

Troubleshooting

Start with tg-mpv --doctor: it checks Python, libraries, mpv, credentials, the session, the bot and your saved data, and says how to fix each problem.

  • "Telegram credentials are missing" — set TG_MPV_API_ID and TG_MPV_API_HASH (see Quick start).
  • "Telegram session is not authorized" — run tg-mpv --login.
  • "mpv was not found" — run tg-mpv --install-mpv, or set MPV.
  • "Another tg-mpv instance is already running" — only one tg-mpv can use the session at a time; close the other one (the message shows its PID).
  • "No playable video was found" — tg-mpv already retries reworded queries; run with --debug to see exactly what the bot sent.

Limitations

  • Bots differ. tg-mpv uses generic detection of videos, buttons and "load more", and learns your bot's behaviour, but a bot with an unusual interface may need a small adjustment in bot.py (all bot-specific logic lives there).
  • Telegram applies its own rate limits to downloads; very aggressive seeking can be throttled.
  • On Windows, resume-position tracking talks to mpv through a named pipe; if that fails, playback still works and only "resume" is skipped. Ctrl+Z handling exists on Linux/macOS only; use Ctrl+C on Windows.
  • Skip-intro is a remembered start time you chose earlier, not automatic intro detection.
  • You are responsible for complying with Telegram's Terms of Service and the law where you live; tg-mpv only accesses content exposed by the bot you choose.

Development

git clone https://github.com/quillai-dev/tg-mpv.git
cd tg-mpv
python -m pip install -e ".[dev]"
python -m pytest

The tests need no Telegram account or network. CI runs them on Linux, macOS and Windows.

Releasing (maintainers): bump __version__ in tg_mpv/__init__.py and add a line to CHANGELOG.md, push, then create a GitHub Release — the publish workflow builds the package and uploads it to PyPI. Check a build locally with python -m build && python -m twine check dist/*.

Author

Quillai Mohammed Eisa — quillai.dev@gmail.com — github.com/quillai-dev

Repository: https://github.com/quillai-dev/tg-mpv · Issues: https://github.com/quillai-dev/tg-mpv/issues

License

GPL-3.0-or-later — see LICENSE.

Metadata

Release files for tg-mpv 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tg-mpv 0.2.0
File Size Uploaded Provenance Actions
tg_mpv-0.2.0.tar.gz 84.4 kB With provenance Details for tg_mpv-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tg-mpv 0.2.0
File Interpreter ABI Platform Actions
tg_mpv-0.2.0-py3-none-any.whl Python 3 none any Details for tg_mpv-0.2.0-py3-none-any.whl

Total release size: 152.0 kB

Release files / tg_mpv-0.2.0.tar.gz

Download URL tg_mpv-0.2.0.tar.gz
Size 84.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4dd9a6a6157fe6d84e6746dce683c5b43cdafeba1ed58769a0d5c28475661be0
BLAKE2b-256 checksum
How to use checksums
e194f3761a082cd20f61f7476d2539fda45ba4ed6b4b47aa9aeb29a8f5cec09d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / tg_mpv-0.2.0-py3-none-any.whl

Download URL tg_mpv-0.2.0-py3-none-any.whl
Size 67.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee002a28cf413a80ff06ba98e3ec2884ff878159eb4916044f6a519e9506f95d
BLAKE2b-256 checksum
How to use checksums
c48863d1292b13490bfe29a1324781340abcfe29cd52c518e27fb32215978728
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for tg-mpv 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tg_mpv-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page