# 命令行和自动化

VDM can be driven without its window in three ways:

- **应用程序自带的命令行**，使用与 IDM 相同的开关：通过脚本、批处理文件或其他程序，将下载任务交给正在运行的应用程序。
- **`voltdownloadmanager-cli`**，一个独立的命令行工具：无需打开应用程序，即可在终端中下载文件、视频和种子，并显示进度。
- **The automation API**: other programs list the running app's downloads (as JSON) and pause, start, remove or delete them, over a local, token-protected channel. AI assistants use it through VDM's MCP server: see [AI assistants](https://vdm.mikaillab.com/zh-hans/help/ai-assistants.html).

To run the whole app without a window, on a home server or a NAS, see [Run VDM on a server](https://vdm.mikaillab.com/zh-hans/help/server.html) (`voltdownloadmanager --headless`); to control it from a phone, [Control VDM from your phone](https://vdm.mikaillab.com/zh-hans/help/remote-control.html).

示例使用 Windows 路径；在 macOS 和 Linux 上，程序名称相同，不带 `.exe`。

## 应用程序的命令行

```
voltdownloadmanager.exe /d URL [/p folder] [/f name] [/a] [/n] [/s]
```

| 开关 | 功能 |
|---|---|
| `/d URL` | 要下载的网址。 |
| `/p folder` | 将其保存到此文件夹（否则保存到**选项 → 保存到**中对应类别的文件夹）。 |
| `/f name` | 以此名称保存。 |
| `/a` | 将其添加到主下载队列，但不开始下载。 |
| `/n` | 静默模式：不显示**下载文件信息**窗口，立即开始下载（或使用 `/a` 将其加入队列）。 |
| `/s` | 开始主下载队列。 |
| `/exit` | 保存并关闭正在运行的应用程序（安装程序会在升级前使用此开关）。 |
| `/startup` | Start in the notification area, the window hidden: what **Launch Volt Download Manager on startup** starts VDM with when you sign in. Where no notification area shows VDM's icon (some Linux desktops), VDM starts minimized. A running VDM stays as it is. |
| *一个 `.torrent` 文件或 `magnet:` 链接* | Open it in **Add torrent** (what Windows passes when VDM opens torrents). |

Switches can start with `/` or `-`, in any case. If VDM is running, the command goes to it; if not, VDM starts and carries it out. For example:

```
voltdownloadmanager.exe /d https://example.com/files/report.pdf /p D:\Reports /n
voltdownloadmanager.exe /d https://example.com/big.iso /a
voltdownloadmanager.exe /s
```

## voltdownloadmanager-cli

It comes with every download of VDM, next to the app (the installer puts it in VDM's folder). It works on its own: the app needn't run, except for `status` and `api`.

Its downloads leave the way the app's do: through **Options → Proxy / Socks** (no proxy, **Use system settings** with its `HTTPS_PROXY` / `HTTP_PROXY` / `ALL_PROXY` variables and the system's automatic configuration script, an automatic configuration script, fetched before the command starts, or a manual proxy with its login) and its **Network adapter**, with the DNS of **Options → Connection**. That covers `get`, `resume`, the video, playlist and gallery commands (yt-dlp and gallery-dl too), the `install-*` commands, and torrents when Torrent settings' **Use the proxy** is on. For one run, `--proxy` takes the place of Options' proxy: `none` (direct, yt-dlp and gallery-dl too, whatever the system's proxy), `system`, or a proxy's address, `http://127.0.0.1:7890`, `https://…`, `socks5://…`, `socks5h://…` (the proxy looks the sites' names up), `socks4://…`, `socks4a://…`. A password is never taken on the command line, where other programs can read it: write the user name in the address (`socks5://ann@127.0.0.1:1080`) and put the password in the `VDM_PROXY_PASSWORD` environment variable. `get` and `resume` also have `--dns` and `--adapter`, and `video-get` and `gallery-get` `--adapter` (`any` for none), for the same.

| 命令 | 功能 |
|---|---|
| `get URL [-o folder\\|file] [-c connections] [-H "Header: value"]... [-r referer] [--user-agent agent] [--cookie cookie] [--checksum hash] [--dns choice] [--adapter name] [--proxy proxy]` | Download a file with up to 8 connections (`-c`, 1 to 128), showing progress. `Ctrl+C` pauses it; `resume` continues. `-r` sets the referring page address (Referer header), `--user-agent` sets the request's User-Agent (then the only one sent; without it, VDM speaks as your default browser, and, when a site's bot check refuses that and the way a browser connects too, once more under its own name), `--cookie` sets the Cookie header (a cookie on the command line ends up in the shell's history; `-` reads it from standard input instead). `--checksum` checks the finished file: hex MD5, SHA-1, SHA-256, SHA-512 or CRC32, optionally named (`sha256:…`); a file that doesn't match fails. Without it, a checksum the server sends is checked, and a file that doesn't match it is kept with a `warning:` line (see [Verify checksum](https://vdm.mikaillab.com/zh-hans/help/download-list.html#verify-checksum)). `--dns` picks the DNS instead of Options': `system`; a service and how to ask it, `cloudflare-https`, `quad9-tls`, `google-plain`, `adguard-https`, `nextdns-tls`; DNS servers' addresses, `9.9.9.9,149.112.112.112`; or a DNS-over-HTTPS address. It never falls back to the system's DNS. `--adapter` sends every connection through one network adapter instead of Options' (`any` for none), by its name (`Ethernet`, `wg0`) or one of its addresses; when it isn't connected, nothing is sent and the download fails. `--proxy` takes the place of Options' proxy for this run (see above). The app's [site rules](https://vdm.mikaillab.com/zh-hans/help/connections-and-speed.html#site-rules) for the address apply too (their headers, User-Agent, browser-like connection, number of connections and proxy), where the command line gives none of the same: `--proxy` goes before every rule's proxy. A link from a file host that one of your debrid accounts (**Options → Accounts**) supports goes through that account, as in the app, and `get` says `through Real-Debrid`; `--direct` downloads the address itself ([Debrid service accounts](https://vdm.mikaillab.com/zh-hans/help/debrid.html)). |
| `resume file.vdm.json [-c connections] [-H ...] [-r referer] [--user-agent agent] [--cookie cookie] [--checksum hash] [--dns choice] [--adapter name] [--proxy proxy] [--direct]` | 从旁边的 `.vdm.json` 文件继续暂停或中断的下载。`-r`、`--user-agent` 和 `--cookie` 设置额外的头（`-` 从标准输入读取 Cookie）。`--checksum` 验证本次运行完成的文件。`--dns`、`--adapter` 和 `--proxy` 设置连接的 DNS、网络适配器和代理，与 `get` 相同，并且应用程序的站点规则也适用于 `get`。通过 Debrid 账户，如 `get`：当旧链接过期时，服务会提供新链接；`--direct` 从地址本身继续。 |
| `install-gallery-dl` | 安装或更新用于下载图片图库的 gallery-dl：使用作者的签名密钥验证其校验和列表，再根据该列表验证程序（适用于 Windows 和 Linux x86-64；其他系统需自行安装）。 |
| `gallery-get URL [-o folder] [--limit N] [--adapter name]` | Download a gallery's pictures (a Wikimedia Commons category, a Flickr album, ...) into a folder named after it inside `-o`; `--limit` takes at most that many, the first the site lists. Started again, it fetches only the new ones. VDM's own downloader brings the pictures gallery-dl lists, 4 at a time. `--adapter` sends the connections, VDM's and gallery-dl's, through one network adapter, as for `get`. |
| `video-info URL` | 查看网页中包含的视频（YouTube、Vimeo 等）以及可选画质。 |
| `playlist-info URL [--from N] [--count N]` | 列出播放列表或频道中的视频，每行一个，按列表顺序显示其位置、时长、标题和网址。 |
| `playlist-get URL [-f selector] [-o folder] [-c pieces] [--split-chapters] [--from N] [--count N]` | 将播放列表或频道中的视频逐个下载到 `-o` 内以列表命名的文件夹中。`-f` 是所有视频的统一画质，与 `video-get` 相同（默认是每个视频的最佳画质；`bv*[height<=720]+ba/b[height<=720]` = 最高 720p；`bv[vcodec^=avc]+ba[acodec^=mp4a]/b[vcodec^=avc][acodec^=mp4a]` = 最兼容的 H.264 + AAC；`ba` = 原始音轨；`mp3:320`、`opus:`、`flac:` 与 `video-get` 相同）。`-c` 和 `--split-chapters` 与 `video-get` 相同。 |
| `video-get URL [-f selector \\| -q height\\|compatible] [-o folder] [-c pieces] [--subs langs] [--auto-subs] [--embed-subs] [--split-chapters] [--adapter name]` | Download a video. Without `-f` or `-q`, the best quality, the one the Download video window starts on: with ffmpeg the best picture with the best sound joined to it (a site whose pictures and sounds come apart, such as Bilibili, needs ffmpeg for a video), else the best file with both. `-q 1080` (or `720p`) takes that height, or the nearest below it the page offers (else the nearest above), and says so. `-q compatible` takes the most compatible quality, as the window's **Most compatible (H.264 + AAC)**: the best H.264 picture with AAC sound, in an MP4 that plays in every editor, TV and phone; a page without one says so (nothing is converted). `-f` takes a quality's selector from `video-info` (`b` = the best single file; `ba` = the sound alone as the site has it, not converted; `mp3:320`, `mp3:256`, `mp3:192` or `mp3:128` = the sound as an MP3 at that bitrate, `mp3:0` at its best variable one; `opus:` = as Opus; `flac:` = as FLAC, lossless but no better than the site's sound; conversions need ffmpeg). `-c`: VDM's own downloader brings each stream yt-dlp finds over this many connections (8 by default, 1 to 128), a stream in pieces (HLS, DASH) this many pieces at once. `--subs` saves subtitles beside it: language codes from `video-info` (`en,fr`), or `all` for every one the uploader gave; `--auto-subs` takes the site's automatic captions instead; `--embed-subs` puts them into the video (needs ffmpeg, MP4 or MKV). With ffmpeg, the video's chapters go into the file too, as Download video's **Embed chapters** does. `--split-chapters` splits the video into numbered chapter files without re-encoding, beside the main video. `--adapter` sends the downloads' connections, and yt-dlp's (and ffmpeg's), through one network adapter, as for `get`; when it isn't connected, nothing starts. When no format of the page matches, the error lists the ones it offers, as `video-info` does. |
| `update-helpers` | Check VDM's own helpers (yt-dlp, ffmpeg, gallery-dl, Node.js) for newer versions now, install them (each checked as its first install was), and print their versions. A helper a download is using switches to its new version once that download ends. Programs you chose in Helpers, and your system's, are yours to update. |
| `install-ytdlp`，`install-ffmpeg`，`install-js-runtime` | Install or update the helpers videos need: yt-dlp (checked against its signing key), ffmpeg (x64 and ARM64 Windows, a build in VDM's signed list of checked builds; on macOS and Linux VDM uses the system's, and on 32-bit Windows one you choose in Helpers) and VDM's own JavaScript runtime, which yt-dlp needs for YouTube's qualities: Node.js's newest LTS build (its checksum list checked against Node.js's releasers' keys; Windows, Linux and macOS). A runtime already on your computer, or one chosen in Helpers, is still used first, and the command says so. `install-deno`, its earlier name, does the same. |
| `torrent-info SOURCE` | 种子的名称、文件（编号）和大小；`SOURCE` 是磁力链接或 `.torrent` 文件。 |
| `torrent-get SOURCE [-o folder] [--files 0,2] [--use-existing]` | 下载种子，或仅下载其部分文件。如果其部分文件已在文件夹中，它会停止并列出这些文件；`--use-existing` 则使用这些文件（匹配的保留，其余下载）。如果“种子设置”中的**将此公共列表的追踪器添加到公共种子**已开启，公共种子也会获得列表中的追踪器，与应用程序中相同。 |
| `torrent-create PATH -o file.torrent` | 创建文件或文件夹的 `.torrent`，或按名称列出文件夹中的文件（对等节点通过 DHT 发现）。 |
| `status [--id N] [-s status] [--search text] [-c category] [-q queue] [-l limit]`（或 `list`） | 运行中应用的下载任务，以 JSON 格式显示，包含其 ID（自动化 API 必须开启）。可选标志可按 ID、状态、搜索字符串、类别或队列进行筛选，或限制数量。 |
| `pause ID...`, `start ID...` | 暂停正在运行的下载，或在运行中的应用里开始（恢复）已停止、已排队或失败的下载。 |
| `remove ID...`, `delete ID...` | 将下载从运行中的应用列表中移除：`remove` 保留文件，`delete` 也删除文件。正在进行的下载会先暂停（`pause`）。 |
| `add URL [-o folder] [-f name] [-c category] [-q queue] [--checksum hash] [-r referer] [--user-agent agent] [--cookie cookie] [--private] [-l] [-s]` | 将下载任务交给正在运行的应用。`-o`设置文件夹，`-f`设置文件名，`-c`设置类别，`-q`设置队列，`--checksum`设置所需的哈希值，`-r`设置引用页地址（Referer头），`--user-agent`设置请求的User-Agent，`--cookie`设置Cookie头（命令行中的Cookie会留在shell历史记录中；`-`则从标准输入读取），`--private`将其标记为隐私窗口下载（与普通窗口下载分开，仅当**选项 → 常规 → 将隐私窗口下载保留在列表中**开启时才保存在列表中，默认开启），`-l`将其加入队列而不开始，`-s`跳过“下载文件信息”对话框。成功时打印`{"id": N}`。 |
| `start-queue [name] [-q name]`, `stop-queue [name] [-q name]` | 在运行中的应用中启动或停止下载队列（自动化 API）。如果省略，则启动或停止主队列。 |
| `pair` | 显示一个新的配对码，用于将手机或远程浏览器与运行中应用的远程控制功能配对：包括其网址、配对码和二维码。 |
| `api on`, `api off` | 开启或关闭运行中的应用的自动化 API。 |

`voltdownloadmanager-cli help <command>`显示所有选项。如果命令失败（应用未运行，应用返回错误，下载中断），则以代码 1 退出。例如：

```
voltdownloadmanager-cli get https://example.com/big.iso -o D:\Downloads -c 16 --checksum sha256:9f86d0…
voltdownloadmanager-cli video-get "https://www.youtube.com/watch?v=aqz-KE-bpKQ" -q 720
voltdownloadmanager-cli torrent-get "magnet:?xt=urn:btih:…" --files 0
```

### 用于脚本和程序的 JSON

使用 `--json`（在命令之前或之后），每个命令都会打印 JSON 而不是文本，每行一个对象，供脚本和 AI 代理读取：

| 命令 | 打印 |
|---|---|
| `get`, `resume` | `{"event": "through", "service"}`首先，当下载通过 Debrid 账户进行时，然后每半秒显示一行 `{"event": "progress", "downloaded", "size", "speed", "connections", "segments"}`，然后是 `{"event": "done", "path", "size", "seconds"}`（如果文件与服务器发送的校验和不匹配但仍被保留，则带有 `"warning"`），或者在 `Ctrl+C` 之后显示 `{"event": "paused", "state"}`（`state` 是 `resume` 获取的文件）。 |
| `video-get` | 如果没有 `-f`，则首先显示 `{"event": "quality", "label", "format", "height", "asked", "nearest"}`：所选画质，`asked` 是 `-q` 的高度（`"compatible"` 用于 `-q compatible`，否则为 `null`），以及 `nearest` 是否未提供该高度。然后是 `{"event": "progress", "downloaded", "size", "speed"}` 行，`{"event": "note", "text"}` 用于下载日志中的内容（服务器未提供但已选择的最接近的较低画质），然后是 `{"event": "done", "path", "subtitles", "subtitle_problems"}`：保存到旁边的字幕文件，以及网站拒绝的语言（`{"code", "name", "reason", "beside"}`；`beside`：它们在第二次尝试时成功下载，保存到视频旁边而不是嵌入视频中）。 |
| `torrent-get` | `{"event": "progress", "phase", "downloaded", "size", "speed", "peers", "peers_seen"}`行，然后是 `{"event": "done", "path", "size"}`，或者 `--seconds` 的 `{"event": "stopped", "downloaded", "seconds"}`。 |
| `video-info` | `{"title", "choices": [{"label", "format", "size", "approx", "height", "fps", "codec", "range"}], "subtitles": [{"code", "name", "automatic"}], "declared"}`；`declared`是页面声称的视频地址，当查找的是该地址而不是HTML中的第一个播放器时（在yt-dlp没有自己的读取器的站点上），否则为`null`；选择的`format`是`video-get -f`所接受的（最初用于确定大小的格式，如`401+251`，然后是相同编解码器和高度最接近的，如果这些都消失了，则为任何编解码器），`approx`表示其`size`是估算值，`codec`是`AV1`、`VP9`、`H.264`…，`range`是`HDR`（或HDR选项旁边的`SDR`）；字幕的`code`是`--subs`所接受的。 |
| `torrent-info` | `{"name", "info_hash", "size", "files": [{"index", "size", "path"}]}`. |
| `torrent-create` | `{"info_hash", "path"}`. |
| `install-ytdlp`，`install-ffmpeg`，`install-js-runtime` | `{"tool", "version", "path"}`；`install-js-runtime`添加`used`，即yt-dlp使用的运行时（`{"runtime", "version", "path"}`）。 |
| `update-helpers` | 每个帮助程序一个`{"tool", "status", "version"}`；`status`是`up-to-date`、`updated`、`ready`（下载结束后使用）、`skipped`（您从最新版本回退了）、`not-ours`、`busy`或`failed`（带`error`）。 |
| `status`、`pause`、`start`、`remove`、`delete`、`start-queue`、`stop-queue`、`add`、`pair`、`api` | 应用的回答，已经是 JSON 格式（见下文）。 |

大小以字节为单位（未知时为 `null`），速度以字节每秒为单位。失败时打印 `{"error": "…"}` 并以代码 1 退出；当 `video-get` 未找到页面可下载的格式时，错误信息会附带 `"choices"`（与 `video-info` 相同）和 `"formats"`（yt-dlp 自己的：`{"id", "ext", "height", "video", "audio"}`）。例如：

```
voltdownloadmanager-cli --json get https://example.com/big.iso -o D:\Downloads
{"event":"progress","downloaded":4194304,"size":1073741824,"speed":52428800,"connections":8,"segments":8}
…
{"event":"done","path":"D:\\Downloads\\big.iso","size":1073741824,"seconds":20.4}
```

## 自动化 API

其他程序可以询问正在运行的应用正在下载什么，并暂停、开始、移除或删除下载。API **默认关闭**；使用 `voltdownloadmanager-cli api on` 开启（使用 `api off` 关闭）。

### How a program talks to VDM

1. While it runs, VDM listens on a port of `127.0.0.1` (this computer only) and writes the port and a secret token to `ipc.json` in its data folder: - Windows: `%APPDATA%\voltdownloadmanager\ipc.json` - macOS: `~/Library/Application Support/voltdownloadmanager/ipc.json` - Linux: `~/.local/share/voltdownloadmanager/ipc.json` - A [portable copy](https://vdm.mikaillab.com/zh-hans/help/portable.html): `data\ipc.json` next to the program.

The token changes at every start; only programs that can read your data folder (yours) can use it. 2. Connect to that port and send one line of JSON: the token and the command's fields, e.g. `{"token": "<token>", "query": "list"}`. 3. VDM answers with one line: `ok` for a command, or a JSON object for a question.

### 支持的操作

| 字段（除了 `token`） | 回答 |
|---|---|
| `{"query": "list"}` | 每个下载和总速度：`{"downloads": [{"id", "file", "url", "path", "status", "size", "downloaded", "speed", "added", "category", "error"}], "speed": bytes per second, "queues": [{"name", "running", "ids"}], "categories": [{"key", "name"}], "free": bytes}`。`status` 是 `Queued`、`Downloading`、`Stopped`、`Complete` 或 `Error`；`size` 在未知时是 `null`；`error`（失败原因）仅在下载处于 `Error` 状态时存在；`category` 是类别的 `key`；`free` 是下载存储的可用空间（未知时为 `null`）。仅在 API 开启时可用。 |
| `{"action": "pause", "ids": [3, 4]}` | 根据 ID 操作下载：`"pause"`、`"start"`、`"remove"`（保留文件）或 `"delete"`（也删除文件）。返回 `{"done": [3], "skipped": [{"id": 4, "why": "not running"}]}`；移除或删除正在进行的下载前，必须先暂停。仅在 API 开启时可用。 |
| `{"action": "start_queue", "queue": "Main download queue"}` | 启动（或使用 `"stop_queue"` 停止）该名称的队列（名称来自列表的 `queues`；默认：主下载队列）。回答 `{"done": [], "skipped": []}`，或 `{"error": …, "code": "unknown-queue"}`。仅当 API 开启时有效。 |
| `{"api": true}` / `{"api": false}` | 开启或关闭 API：`{"api": true}`。 |
| `{"url": "https://…", "dir": "D:\\Files", "name": "a.zip", "silent": true}` | 添加下载，如同命令行中的 `/d /p /f /n`（`"later": true` 是 `/a`）；`ok`。`"category"`（列表中的 `categories` 键）将其放入该类别的文件夹，而不是其类型选择的文件夹；`"queue"`（队列名称）是它与 `"later"` 一起等待的队列。在 API 关闭时也可用，如同命令行。 |
| `{"start_queue": true}` | 启动主下载队列；`ok`。 |

An unknown question or action is answered `{"error": "…"}`, as is a question while the API is off (then with `"code": "api-off"`, for programs to check: the error text may change). A wrong token is answered `denied`. When VDM is already handling as many requests as it takes at once, it answers `busy`: nothing was done, and the line can be sent again a moment later.

A command (answered `ok`) may carry an `"id"` of your own: letters, digits and `-`, at most 64, different for each command. If its answer doesn't come back (the line closed, or a wait ran out), send the same line again with the same `id`: VDM does it once, however many of those lines reach it, and answers `ok` to each. Without an `id`, each line is a new command. A second launch of the program (`voltdownloadmanager.exe /d …`, `/exit`) does this by itself.

每个请求和回答都按字段在 JSON Schema 中描述：[automation-api.schema.json](https://vdm.mikaillab.com/automation-api.schema.json)（也在 `https://vdm.mikaillab.com/automation-api.schema.json`）。它由应用回答的代码生成，因此保持精确；程序可以根据它检查发送和读取的内容，或从中生成其类型。

### 示例

PowerShell (Windows):

```
$ep = Get-Content "$env:APPDATA\voltdownloadmanager\ipc.json" | ConvertFrom-Json
$client = New-Object Net.Sockets.TcpClient("127.0.0.1", $ep.port)
$stream = $client.GetStream()
$writer = New-Object IO.StreamWriter($stream); $reader = New-Object IO.StreamReader($stream)
$writer.WriteLine((@{ token = $ep.token; query = "list" } | ConvertTo-Json -Compress))
$writer.Flush()
$reader.ReadLine() | ConvertFrom-Json | Select-Object -ExpandProperty downloads | Format-Table file, status, downloaded, size
$client.Close()
```

Python：

```
import json, os, socket

ipc = os.path.join(os.environ["APPDATA"], "voltdownloadmanager", "ipc.json")  # see above elsewhere
ep = json.load(open(ipc))
with socket.create_connection(("127.0.0.1", ep["port"])) as s:
    s.sendall((json.dumps({"token": ep["token"], "query": "list"}) + "\n").encode())
    answer = json.loads(s.makefile().readline())
for d in answer["downloads"]:
    print(d["file"], d["status"], d["downloaded"], d["size"])
```

若要添加下载，请发送`{"token": "…", "url": "https://example.com/a.zip", "silent": true}`。

## 相关

- [选项](https://vdm.mikaillab.com/zh-hans/help/options.html)
- [主题](https://vdm.mikaillab.com/zh-hans/help/themes.html)
- [语言](https://vdm.mikaillab.com/zh-hans/help/languages.html)
- [便携模式](https://vdm.mikaillab.com/zh-hans/help/portable.html)
- [键盘快捷方式](https://vdm.mikaillab.com/zh-hans/help/keyboard-shortcuts.html)

[在 GitHub 上编辑此页面](https://github.com/mikailhossenimran/voltdownloadmanager/edit/main/docs/command-line.md) [报告此页面的问题](https://github.com/mikailhossenimran/voltdownloadmanager/issues/new?template=website.yml&title=Website%3A%20Command%20line%20and%20automation&page=https%3A//vdm.mikaillab.com/help/command-line.html) [页面历史](https://github.com/mikailhossenimran/voltdownloadmanager/commits/main/docs/command-line.md)

免费、开源、无广告、无试用版。

[Download Volt Download Manager](https://vdm.mikaillab.com/zh-hans/download.html)
