# btdl

`btdl` is a pure command-line client for Tidal, built on the
[binary-tidal-py](https://git.binarycollective.de/binary/binary-tidal-py)
library. It is the command-line surface of the whole library:
search the catalog, resolve links, check what a download would do,
download collections with resume and per-item failure isolation,
stream tracks to stdout or files, and maintain the local library
(transcode, probe, purge, diagnose).

No TUI, no global state, no surprise I/O:

- **results** go to **stdout** — an aligned table by default, or the
  exact JSON payload with `--json` on every command;
- **progress and errors** go to **stderr**, so a pipe or a `jq`
  consumer never sees anything but the payload;
- **exit codes** are a stable contract (`0` ok, `1` a Tidal error,
  `130` cancelled, `2` usage/internal — see the
  [output contract](https://btdl.natz.club/output.html.md));
- the library’s **resume**, **skip-existing**, **quality-downgrade
  chain**, and **retry** behavior comes along for the ride, because
  `btdl` is a thin surface over it.

* [Logging in](https://btdl.natz.club/login.html.md)
  * [Device login (recommended)](https://btdl.natz.club/login.html.md#device-login-recommended)
  * [Token login](https://btdl.natz.club/login.html.md#token-login)
  * [Checking and leaving](https://btdl.natz.club/login.html.md#checking-and-leaving)
  * [What you never see](https://btdl.natz.club/login.html.md#what-you-never-see)
  * [When something goes wrong](https://btdl.natz.club/login.html.md#when-something-goes-wrong)
* [Commands](https://btdl.natz.club/commands.html.md)
  * [Search](https://btdl.natz.club/commands.html.md#search)
  * [Catalog lookups](https://btdl.natz.club/commands.html.md#catalog-lookups)
  * [Artist & album groups](https://btdl.natz.club/commands.html.md#artist-album-groups)
  * [Track](https://btdl.natz.club/commands.html.md#track)
  * [Download family](https://btdl.natz.club/commands.html.md#download-family)
  * [Stream](https://btdl.natz.club/commands.html.md#stream)
  * [User](https://btdl.natz.club/commands.html.md#user)
  * [Ids](https://btdl.natz.club/commands.html.md#ids)
  * [Auth](https://btdl.natz.club/commands.html.md#auth)
  * [Maintain](https://btdl.natz.club/commands.html.md#maintain)
  * [Offline reference](https://btdl.natz.club/commands.html.md#offline-reference)
* [Recipes](https://btdl.natz.club/recipes.html.md)
  * [Download an artist’s whole discography](https://btdl.natz.club/recipes.html.md#download-an-artist-s-whole-discography)
  * [What will this album actually play as?](https://btdl.natz.club/recipes.html.md#what-will-this-album-actually-play-as)
  * [Stream a track into the player (no file on disk)](https://btdl.natz.club/recipes.html.md#stream-a-track-into-the-player-no-file-on-disk)
  * [Pre-flight a download without touching disk](https://btdl.natz.club/recipes.html.md#pre-flight-a-download-without-touching-disk)
  * [Retry only the items that failed](https://btdl.natz.club/recipes.html.md#retry-only-the-items-that-failed)
  * [Check one local file against a link](https://btdl.natz.club/recipes.html.md#check-one-local-file-against-a-link)
  * [Two accounts, one terminal](https://btdl.natz.club/recipes.html.md#two-accounts-one-terminal)
  * [Batch: every album from a genre, in the background](https://btdl.natz.club/recipes.html.md#batch-every-album-from-a-genre-in-the-background)
  * [Transcode a finished download for the CDJ](https://btdl.natz.club/recipes.html.md#transcode-a-finished-download-for-the-cdj)
* [Output contract](https://btdl.natz.club/output.html.md)
  * [stdout: the payload, and nothing else](https://btdl.natz.club/output.html.md#stdout-the-payload-and-nothing-else)
  * [stderr: everything a person should see](https://btdl.natz.club/output.html.md#stderr-everything-a-person-should-see)
  * [Exit codes](https://btdl.natz.club/output.html.md#exit-codes)
  * [Streams to stdout](https://btdl.natz.club/output.html.md#streams-to-stdout)
  * [Secrets](https://btdl.natz.club/output.html.md#secrets)
* [Development](https://btdl.natz.club/development.html.md)
  * [Layout](https://btdl.natz.club/development.html.md#layout)
  * [The gate](https://btdl.natz.club/development.html.md#the-gate)
  * [The test surface](https://btdl.natz.club/development.html.md#the-test-surface)
  * [CI](https://btdl.natz.club/development.html.md#ci)
  * [Docs](https://btdl.natz.club/development.html.md#docs)
  * [Releases](https://btdl.natz.club/development.html.md#releases)
  * [Commits](https://btdl.natz.club/development.html.md#commits)

## Install

`btdl` is published to the binarycollective Forgejo package registry
under the name `binary-tidal-cli`.

```console
$ uv tool install \
    --index-url https://git.binarycollective.de/api/packages/binary/pypi/simple \
    binary-tidal-cli
$ btdl --version
```

`pip` works the same way
(`pip install binary-tidal-cli --index-url https://git.binarycollective.de/api/packages/binary/pypi/simple`),
as does a source checkout with `uv sync --group dev`.

System requirement: **ffmpeg**/`ffprobe` on `PATH` for
`maintain transcode` and `maintain probe`; the download pipeline
uses them only for the optional `--save-as-flac` remux and the
`--verify-media` ffprobe pass (turn both off and downloads need no
ffmpeg at all). Everything else works without it.

## Quick start

Log in once (see [login](https://btdl.natz.club/login.html.md) for the options):

```console
$ btdl auth device-login
device login
  open:  https://link.tidal.com/activate
  enter: ABCD-EFGH
  ...
logged in as user 1234567
```

Then browse and grab:

```console
$ btdl search "daft punk" --type albums
$ btdl tracks <album-url>
$ btdl plan <album-url>            # what a download would do
$ btdl download <album-url>        # resume, per-item isolation
$ btdl stream <track-url> -o track.flac
```

Every one of those also takes `--json`:

```console
$ btdl search "daft punk" --type albums --limit 5 --json | jq '.items[0].title'
```

## Global options

Every command accepts the same global options, settable per call or
via environment variables. They map one-to-one onto the library’s
`Config` — the full surface is available, so `btdl` is scriptable the
way the library is.

### Session

| Option                 | Env            | Default         | Meaning                                                                                                                                             |
|------------------------|----------------|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| `--token, -t`          | `BTDL_TOKEN`   | library default | the saved-login (token) file                                                                                                                        |
| `--output, -o`         | `BTDL_OUTPUT`  | `~/Music/btdl`  | the download output root                                                                                                                            |
| `--quality, -q`        | `BTDL_QUALITY` | `LOSSLESS`      | `LOW`, `HIGH`, `LOSSLESS`, `HI_RES_LOSSLESS`, or a label like `master` (`btdl qualities` lists the labels and ladders); an unknown label fails fast |
| `--client`             | `BTDL_CLIENT`  | library default | OAuth client id (`btdl auth clients`); an unknown id fails fast (zero traffic)                                                                      |
| `--proxy`              | `BTDL_PROXY`   | none            | proxy URL for the transport                                                                                                                         |
| `--timeout-connect`    | —              | `10.0`          | connect timeout, seconds                                                                                                                            |
| `--timeout-read`       | —              | `60.0`          | read timeout, seconds                                                                                                                               |
| `--verbose, -v`        | —              | off             | `DEBUG` logging on stderr (secret-redacted via the library’s `RedactionFilter`)                                                                     |
| `--version`            | —              | —               | print the version and exit                                                                                                                          |
| `--install-completion` | —              | —               | install tab completion for the current shell                                                                                                        |
| `--show-completion`    | —              | —               | print the completion script (copy/customize)                                                                                                        |

### Naming and collection behaviour

| Option                             | Env                     | Default                                 | Meaning                                                                                                                                                                                                                                                        |
|------------------------------------|-------------------------|-----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--track-format`                   | `BTDL_TRACK_FORMAT`     | `{number} {artist} - {title}{explicit}` | the track filename path template                                                                                                                                                                                                                               |
| `--album-format`                   | `BTDL_ALBUM_FORMAT`     | `{artist} - {album} {year}`             | the album directory path template                                                                                                                                                                                                                              |
| `--playlist-format`                | `BTDL_PLAYLIST_FORMAT`  | `Playlist - {title}`                    | the playlist/mix directory path template                                                                                                                                                                                                                       |
| `--include-ep / --no-include-ep`   | `BTDL_INCLUDE_EP`       | on                                      | include EPs and singles in artist downloads                                                                                                                                                                                                                    |
| `--quality-priority`               | `BTDL_QUALITY_PRIORITY` | none                                    | comma-separated custom playback ladder — it replaces the built-in downgrade chain, the profile’s own rung included (a rung the order omits is never asked, e.g. `HIGH,LOSSLESS`); a rung the track’s catalog ceiling cannot serve is dropped from the attempts |
| `--skip-broken / --no-skip-broken` | `BTDL_SKIP_BROKEN`      | on                                      | keep going past broken items (a per-run `--fail-fast` overrides it)                                                                                                                                                                                            |

### Network

| Option                                                                | Env                                                                               | Default   | Meaning                                                                                     |
|-----------------------------------------------------------------------|-----------------------------------------------------------------------------------|-----------|---------------------------------------------------------------------------------------------|
| `--max-retries`                                                       | `BTDL_MAX_RETRIES`                                                                | `3`       | retries per transient request failure                                                       |
| `--retry-backoff`                                                     | `BTDL_RETRY_BACKOFF`                                                              | `1.0`     | retry backoff base, seconds                                                                 |
| `--rate-limit-max-wait`                                               | `BTDL_RATE_LIMIT_MAX_WAIT`                                                        | `90.0`    | 429 wait budget per logical request, seconds                                                |
| `--rate-limit-max-pause`                                              | `BTDL_RATE_LIMIT_MAX_PAUSE`                                                       | `21600.0` | cumulative cap for long server-requested pauses, seconds                                    |
| `--request-interval`                                                  | `BTDL_REQUEST_INTERVAL`                                                           | `0.0`     | proactive pacing between playback resolutions (0 = adaptive-only)                           |
| `--asset-not-ready-max-waits`                                         | `BTDL_ASSET_NOT_READY_MAX_WAITS`                                                  | `6`       | polls to wait for an asset still being prepared                                             |
| `--user-agent`                                                        | `BTDL_USER_AGENT`                                                                 | none      | a custom User-Agent for the transport                                                       |
| `--no-verify-tls`                                                     | `BTDL_NO_VERIFY_TLS`                                                              | off       | disable TLS certificate verification (dangerous)                                            |
| `--ca-bundle`                                                         | `BTDL_CA_BUNDLE`                                                                  | none      | a custom CA bundle path (corporate MITM proxies); mutually exclusive with `--no-verify-tls` |
| `--api-base` / `--legacy-api-base` / `--openapi-base` / `--auth-base` | `BTDL_API_BASE` / `BTDL_LEGACY_API_BASE` / `BTDL_OPENAPI_BASE` / `BTDL_AUTH_BASE` | none      | base-URL overrides (replay proxies, testing)                                                |

### Downloads

| Option                     | Env                           | Default   | Meaning                                                                                          |
|----------------------------|-------------------------------|-----------|--------------------------------------------------------------------------------------------------|
| `--download-workers`       | `BTDL_DOWNLOAD_WORKERS`       | `8`       | byte-range fetch workers **per track** (distinct from the per-run `--workers` track parallelism) |
| `--part-size`              | `BTDL_PART_SIZE`              | `1 MiB`   | part size for parallel range fetches, bytes (64 KiB–32 MiB)                                      |
| `--pacing-bps`             | `BTDL_PACING_BPS`             | none      | throttle download throughput, bits/s                                                             |
| `--no-validate-media-urls` | `BTDL_NO_VALIDATE_MEDIA_URLS` | off       | disable the SSRF guard on manifest-supplied media URLs (replay proxies only)                     |
| `--output-lock-timeout`    | `BTDL_OUTPUT_LOCK_TIMEOUT`    | `300.0`   | seconds to wait for the output directory lock when another `btdl`/host writes the same tree      |

### Post-processing

| Option                                   | Env                    | Default   | Meaning                                                                                         |
|------------------------------------------|------------------------|-----------|-------------------------------------------------------------------------------------------------|
| `--write-tags / --no-write-tags`         | `BTDL_WRITE_TAGS`      | on        | write audio tags (ID3v2.4 / MP4 / FLAC) to finished files                                       |
| `--write-lyrics`                         | `BTDL_WRITE_LYRICS`    | off       | fetch per-track lyrics and embed them (one extra API call per item)                             |
| `--lyric-file`                           | `BTDL_LYRIC_FILE`      | off       | write a lyric sidecar (`.lrc` when timed, `.txt` otherwise) next to each file                   |
| `--download-cover / --no-download-cover` | `BTDL_DOWNLOAD_COVER`  | on        | embed collection artwork and write a `cover.jpg` sidecar                                        |
| `--save-album-info`                      | `BTDL_SAVE_ALBUM_INFO` | off       | write an `AlbumInfo.txt` after each album download                                              |
| `--save-as-flac / --no-save-as-flac`     | `BTDL_SAVE_AS_FLAC`    | on        | remux FLAC-in-MP4 to true FLAC when ffmpeg is available                                         |
| `--verify-media / --no-verify-media`     | `BTDL_VERIFY_MEDIA`    | on        | ffprobe-verify the delivered bytes against the manifest facts                                   |
| `--save-receipts`                        | `BTDL_SAVE_RECEIPTS`   | off       | write completion receipts; skip only receipt-proven files (the quality-aware skip/upgrade path) |
