first commit

This commit is contained in:
2026-03-26 03:26:37 -07:00
commit 38d50a814f
38 changed files with 7755 additions and 0 deletions

155
scripts/README.md Normal file
View File

@@ -0,0 +1,155 @@
[English][readme-en] | [简体中文][readme-zh-hans] | [繁体中文][readme-zh-hant]
[readme-en]: https://github.com/akiirui/mpv-handler/blob/main/README.md
[readme-zh-hans]: https://github.com/akiirui/mpv-handler/blob/main/README.zh-Hans.md
[readme-zh-hant]: https://github.com/akiirui/mpv-handler/blob/main/README.zh-Hant.md
# mpv handler
A protocol handler for **mpv**, written by Rust.
Use **mpv** and **yt-dlp** to play video and music from the websites.
Please use it with userscript:
[![play-with-mpv][badges-play-with-mpv]][greasyfork-play-with-mpv]
## Breaking changes
### [v0.4.0][v0.4.0]
To avoid conflicts with the `mpv://` protocol provided by mpv.
> mpv://...
>
> mpv protocol. This is used for starting mpv from URL handler. The protocol is stripped and the rest is passed to the player as a normal open argument. Only safe network protocols are allowed to be opened this way.
Scheme `mpv://` and `mpv-debug://` are deprecated, use `mpv-handler://` and `mpv-handler-debug://`.
**Require manual intervention**
#### Windows
Run `handler-uninstall.bat` to uninstall deprecated protocol, and run `handler-install.bat` to install new procotol.
#### Linux
If you installed manually, please repeat the manual installation process.
## Protocol
![](share/proto.png)
### Scheme
- `mpv-handler`: Run mpv-handler without console window
- `mpv-handler-debug`: Run mpv-handler with console window to view outputs and errors
### Plugins
- `play`: Use mpv player to play video
### Encoded Data
Use [URL-safe base64][rfc-base64-url] to encode the URL or TITLE.
Replace `/` to `_`, `+` to `-` and remove padding `=`.
Example (JavaScript):
```javascript
let data = btoa("https://www.youtube.com/watch?v=Ggkn2f5e-IU");
let safe = data.replace(/\//g, "_").replace(/\+/g, "-").replace(/\=/g, "");
```
### Parameters (Optional)
```
cookies = [ www.domain.com.txt ]
profile = [ default, low-latency, etc... ]
quality = [ 2160p, 1440p, 1080p, 720p, 480p, 360p ]
v_codec = [ av01, vp9, h265, h264 ]
v_title = [ Encoded Title ]
subfile = [ Encoded URL ]
startat = [ Seconds (float) ]
referrer = [ Encoded URL ]
```
## Installation
### Linux
#### Arch Linux
[![mpv-handler][badges-aur]][download-aur]
[![mpv-handler-git][badges-aur-git]][download-aur-git]
#### Manual installation
1. Download [latest Linux release][download-linux]
2. Unzip the archive
3. Copy `mpv-handler` to `$HOME/.local/bin`
4. Copy `mpv-handler.desktop` to `$HOME/.local/share/applications/`
5. Copy `mpv-handler-debug.desktop` to `$HOME/.local/share/applications/`
6. Set executable permission for binary
- ```
$ chmod +x $HOME/.local/bin/mpv-handler
```
7. Register xdg-mime (thanks for the [linuxuprising][linuxuprising] reminder)
- ```
$ xdg-mime default mpv-handler.desktop x-scheme-handler/mpv-handler
$ xdg-mime default mpv-handler-debug.desktop x-scheme-handler/mpv-handler-debug
```
8. Add `$HOME/.local/bin` to your environment variable `PATH`
9. **Optional**: _Copy `config.toml` to `$HOME/.config/mpv-handler/config.toml` and configure_
### Windows
Windows users need to install manually.
#### Manual installation
1. Download [latest Windows release][download-windows]
2. Unzip the archive to the directory you want
3. Run `handler-install.bat` to register protocol handler
4. Edit `config.toml` and set `mpv` and `ytdl` path
## Configuration
```toml
mpv = "/usr/bin/mpv"
# Optional, Type: String
# The path of mpv executable binary
# Default value:
# - Linux: mpv
# - Windows: mpv.com
ytdl = "/usr/bin/yt-dlp"
# Optional, Type: String
# The path of yt-dlp executable binary
proxy = "http://example.com:8080"
# Optional, Type: String
# HTTP(S) proxy server address
# For Windows users:
# - The path can be "C:\\folder\\some.exe" or "C:/folder/some.exe"
# - The path target is an executable binary file, not a directory
```
[v0.4.0]: https://github.com/akiirui/mpv-handler/releases/tag/v0.4.0
[rfc-base64-url]: https://datatracker.ietf.org/doc/html/rfc4648#section-5
[badges-aur-git]: https://img.shields.io/aur/version/mpv-handler-git?style=for-the-badge&logo=archlinux&label=mpv-handler-git
[badges-aur]: https://img.shields.io/aur/version/mpv-handler?style=for-the-badge&logo=archlinux&label=mpv-handler
[badges-play-with-mpv]: https://img.shields.io/greasyfork/v/416271?style=for-the-badge&logo=greasyfork&label=play-with-mpv
[download-aur-git]: https://aur.archlinux.org/packages/mpv-handler-git/
[download-aur]: https://aur.archlinux.org/packages/mpv-handler/
[download-linux]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-linux-amd64.zip
[download-macos]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-macos-amd64.zip
[download-windows]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-windows-amd64.zip
[greasyfork-play-with-mpv]: https://greasyfork.org/scripts/416271-play-with-mpv
[linuxuprising]: https://www.linuxuprising.com/2021/07/open-youtube-and-more-videos-from-your.html

155
scripts/README.zh-Hans.md Normal file
View File

@@ -0,0 +1,155 @@
[English][readme-en] | [简体中文][readme-zh-hans] | [繁体中文][readme-zh-hant]
[readme-en]: https://github.com/akiirui/mpv-handler/blob/main/README.md
[readme-zh-hans]: https://github.com/akiirui/mpv-handler/blob/main/README.zh-Hans.md
[readme-zh-hant]: https://github.com/akiirui/mpv-handler/blob/main/README.zh-Hant.md
# mpv handler
一个 **mpv** 的协议处理程序,使用 Rust 编写。
使用 **mpv****yt-dlp** 播放网站上的视频与音乐。
请配合用户脚本使用:
[![play-with-mpv][badges-play-with-mpv]][greasyfork-play-with-mpv]
## 重大变更
### [v0.4.0][v0.4.0]
为了避免与 mpv 所提供的 `mpv://` 协议冲突。
> mpv://...
>
> mpv protocol. This is used for starting mpv from URL handler. The protocol is stripped and the rest is passed to the player as a normal open argument. Only safe network protocols are allowed to be opened this way.
协议 `mpv://``mpv-debug://` 已弃用, 请使用 `mpv-handler://``mpv-handler-debug://`.
**需要手动干预**
#### Windows
运行 `handler-uninstall.bat` 卸载已弃用的协议, 然后运行 `handler-install.bat` 安装新的协议.
#### Linux
如果你是手动安装的,请重新执行一遍手动安装流程。
## 协议
![](share/proto.png)
### 协议名
- `mpv-handler`: 在没有命令行窗口的情况下运行 mpv-handler
- `mpv-handler-debug`: 在有命令行窗口的情况下运行 mpv-handler 以便于查看输出和错误
### 插件 / Plugins
- `play`: 使用 mpv 播放视频
### 编码数据 / Encoded Data
使用 [URL 安全的 base64][rfc-base64-url] 编码网址或标题。
替换 `/``_`, `+``-` 并且删除填充的 `=`
示例 (JavaScript):
```javascript
let data = btoa("https://www.youtube.com/watch?v=Ggkn2f5e-IU");
let safe = data.replace(/\//g, "_").replace(/\+/g, "-").replace(/\=/g, "");
```
### 参数 / Parameters (可选)
```
cookies = [ www.domain.com.txt ]
profile = [ default, low-latency, etc... ]
quality = [ 2160p, 1440p, 1080p, 720p, 480p, 360p ]
v_codec = [ av01, vp9, h265, h264 ]
v_title = [ Encoded Title ]
subfile = [ Encoded URL ]
startat = [ Seconds (float) ]
referrer = [ Encoded URL ]
```
## 安装
### Linux
#### Arch Linux
[![mpv-handler][badges-aur]][download-aur]
[![mpv-handler-git][badges-aur-git]][download-aur-git]
#### 手动安装
1. 下载 [最新的 Linux 压缩包][download-linux]
2. 解压缩压缩包
3. 复制 `mpv-handler``$HOME/.local/bin`
4. 复制 `mpv-handler.desktop``$HOME/.local/share/applications/`
5. 复制 `mpv-handler-debug.desktop``$HOME/.local/share/applications/`
6. 为二进制文件设置可执行权限
- ```
$ chmod +x $HOME/.local/bin/mpv-handler
```
7. 注册 xdg-mime感谢 [linuxuprising][linuxuprising] 的提醒)
- ```
$ xdg-mime default mpv-handler.desktop x-scheme-handler/mpv-handler
$ xdg-mime default mpv-handler-debug.desktop x-scheme-handler/mpv-handler-debug
```
8. 添加 `$HOME/.local/bin` 到环境变量 `PATH`
9. **可选**: _复制 `config.toml` 至 `$HOME/.config/mpv-handler/config.toml` 并配置_
### Windows
Windows 用户目前只能手动安装。
#### 手动安装
1. 下载 [最新的 Windows 压缩包][download-windows]
2. 解压缩档案到你想要的位置
3. 运行 `handler-install.bat` 注册协议处理程序
4. 编辑 `config.toml` 设置 `mpv` 和 `ytdl` 的路径
## 配置
```toml
mpv = "/usr/bin/mpv"
# 可选,类型:字符串
# mpv 可执行文件的路径
# 默认值:
# - Linux: mpv
# - Windows: mpv.com
ytdl = "/usr/bin/yt-dlp"
# 可选,类型:字符串
# yt-dlp 可执行文件的路径
proxy = "http://example.com:8080"
# 可选,类型:字符串
# HTTP(S) 代理服务器的地址
# 对于 Windows 用户:
# - 路径格式可以是 "C:\\folder\\some.exe",也可以是 "C:/folder/some.exe"
# - 路径的目标是可执行二进制文件,而不是目录
```
[v0.4.0]: https://github.com/akiirui/mpv-handler/releases/tag/v0.4.0
[rfc-base64-url]: https://datatracker.ietf.org/doc/html/rfc4648#section-5
[badges-aur-git]: https://img.shields.io/aur/version/mpv-handler-git?style=for-the-badge&logo=archlinux&label=mpv-handler-git
[badges-aur]: https://img.shields.io/aur/version/mpv-handler?style=for-the-badge&logo=archlinux&label=mpv-handler
[badges-play-with-mpv]: https://img.shields.io/greasyfork/v/416271?style=for-the-badge&logo=greasyfork&label=play-with-mpv
[download-aur-git]: https://aur.archlinux.org/packages/mpv-handler-git/
[download-aur]: https://aur.archlinux.org/packages/mpv-handler/
[download-linux]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-linux-amd64.zip
[download-macos]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-macos-amd64.zip
[download-windows]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-windows-amd64.zip
[greasyfork-play-with-mpv]: https://greasyfork.org/scripts/416271-play-with-mpv
[linuxuprising]: https://www.linuxuprising.com/2021/07/open-youtube-and-more-videos-from-your.html

155
scripts/README.zh-Hant.md Normal file
View File

@@ -0,0 +1,155 @@
[English][readme-en] | [簡體中文][readme-zh-hans] | [繁體中文][readme-zh-hant]
[readme-en]: https://github.com/akiirui/mpv-handler/blob/main/README.md
[readme-zh-hans]: https://github.com/akiirui/mpv-handler/blob/main/README.zh-Hans.md
[readme-zh-hant]: https://github.com/akiirui/mpv-handler/blob/main/README.zh-Hant.md
# mpv handler
一個 **mpv** 的協議處理程序,使用 Rust 編寫。
使用 **mpv****yt-dlp** 播放網站上的視頻與音樂。
請配合用戶腳本使用:
[![play-with-mpv][badges-play-with-mpv]][greasyfork-play-with-mpv]
## 重大變更
### [v0.4.0][v0.4.0]
爲了避免與 mpv 所提供的 `mpv://` 協議衝突。
> mpv://...
>
> mpv protocol. This is used for starting mpv from URL handler. The protocol is stripped and the rest is passed to the player as a normal open argument. Only safe network protocols are allowed to be opened this way.
協議 `mpv://``mpv-debug://` 已棄用, 請使用 `mpv-handler://``mpv-handler-debug://`.
**需要手動干預**
#### Windows
運行 `handler-uninstall.bat` 卸載已棄用的協議, 然後運行 `handler-install.bat` 安裝新的協議.
#### Linux
如果你是手動安裝的,請重新執行一遍手動安裝流程。
## 協議
![](share/proto.png)
### 協議名
- `mpv-handler`: 在沒有命令行窗口的情況下運行 mpv-handler
- `mpv-handler-debug`: 在有命令行窗口的情況下運行 mpv-handler 以便於查看輸出和錯誤
### 插件 / Plugins
- `play`: 使用 mpv 播放視頻
### 編碼數據 / Encoded Data
使用 [URL 安全的 base64][rfc-base64-url] 編碼網址或標題。
替換 `/``_`, `+``-` 並且刪除填充的 `=`
示例 (JavaScript):
```javascript
let data = btoa("https://www.youtube.com/watch?v=Ggkn2f5e-IU");
let safe = data.replace(/\//g, "_").replace(/\+/g, "-").replace(/\=/g, "");
```
### 參數 / Parameters (可選)
```
cookies = [ www.domain.com.txt ]
profile = [ default, low-latency, etc... ]
quality = [ 2160p, 1440p, 1080p, 720p, 480p, 360p ]
v_codec = [ av01, vp9, h265, h264 ]
v_title = [ Encoded Title ]
subfile = [ Encoded URL ]
startat = [ Seconds (float) ]
referrer = [ Encoded URL ]
```
## 安裝
### Linux
#### Arch Linux
[![mpv-handler][badges-aur]][download-aur]
[![mpv-handler-git][badges-aur-git]][download-aur-git]
#### 手動安裝
1. 下載 [最新的 Linux 壓縮包][download-linux]
2. 解壓縮壓縮包
3. 複製 `mpv-handler``$HOME/.local/bin`
4. 複製 `mpv-handler.desktop``$HOME/.local/share/applications/`
5. 複製 `mpv-handler-debug.desktop``$HOME/.local/share/applications/`
6. 爲二進制文件設置可執行權限
- ```
$ chmod +x $HOME/.local/bin/mpv-handler
```
7. 註冊 xdg-mime感謝 [linuxuprising][linuxuprising] 的提醒)
- ```
$ xdg-mime default mpv-handler.desktop x-scheme-handler/mpv-handler
$ xdg-mime default mpv-handler-debug.desktop x-scheme-handler/mpv-handler-debug
```
8. 添加 `$HOME/.local/bin` 到環境變量 `PATH`
9. **可選**: _複製 `config.toml` 至 `$HOME/.config/mpv-handler/config.toml` 並配置_
### Windows
Windows 用戶目前只能手動安裝。
#### 手動安裝
1. 下載 [最新的 Windows 壓縮包][download-windows]
2. 解壓縮檔案到你想要的位置
3. 運行 `handler-install.bat` 註冊協議處理程序
4. 編輯 `config.toml` 設置 `mpv` 和 `ytdl` 的路徑
## 配置
```toml
mpv = "/usr/bin/mpv"
# 可選,類型:字符串
# mpv 可執行文件的路徑
# 默認值:
# - Linux: mpv
# - Windows: mpv.com
ytdl = "/usr/bin/yt-dlp"
# 可選,類型:字符串
# yt-dlp 可執行文件的路徑
proxy = "http://example.com:8080"
# 可選,類型:字符串
# HTTP(S) 代理服務器的地址
# 對於 Windows 用戶:
# - 路徑格式可以是 "C:\\folder\\some.exe",也可以是 "C:/folder/some.exe"
# - 路徑的目標是可執行二進制文件,而不是目錄
```
[v0.4.0]: https://github.com/akiirui/mpv-handler/releases/tag/v0.4.0
[rfc-base64-url]: https://datatracker.ietf.org/doc/html/rfc4648#section-5
[badges-aur-git]: https://img.shields.io/aur/version/mpv-handler-git?style=for-the-badge&logo=archlinux&label=mpv-handler-git
[badges-aur]: https://img.shields.io/aur/version/mpv-handler?style=for-the-badge&logo=archlinux&label=mpv-handler
[badges-play-with-mpv]: https://img.shields.io/greasyfork/v/416271?style=for-the-badge&logo=greasyfork&label=play-with-mpv
[download-aur-git]: https://aur.archlinux.org/packages/mpv-handler-git/
[download-aur]: https://aur.archlinux.org/packages/mpv-handler/
[download-linux]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-linux-amd64.zip
[download-macos]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-macos-amd64.zip
[download-windows]: https://github.com/akiirui/mpv-handler/releases/latest/download/mpv-handler-windows-amd64.zip
[greasyfork-play-with-mpv]: https://greasyfork.org/scripts/416271-play-with-mpv
[linuxuprising]: https://www.linuxuprising.com/2021/07/open-youtube-and-more-videos-from-your.html

18
scripts/config.toml Normal file
View File

@@ -0,0 +1,18 @@
#mpv = "C:\\path\\of\\mpv.com"
# Optional, Type: String
# The path of mpv executable binary
# Default value:
# - Linux: mpv
# - Windows: mpv.com
#ytdl = "C:\\path\\of\\yt-dlp.exe"
# Optional, Type: String
# The path of yt-dlp executable binary
#proxy = "http://example.com:8080"
# Optional, Type: String
# HTTP(S) proxy server address
# For Windows users:
# - The path can be "C:\\folder\\some.exe" or "C:/folder/some.exe"
# - The path is an executable binary file, not a directory

View File

@@ -0,0 +1,82 @@
@echo OFF
:: Unattended install flag. When set, the script will not require user input.
set unattended=no
if "%1"=="/u" set unattended=yes
:: Make sure this is Windows Vista or later
call :ensure_vista
:: Make sure the script is running as admin
call :ensure_admin
:: Get mpv.exe location
call :check_binary
:: Add registry
call :add_verbs
:die
if not [%1] == [] echo %~1
if [%unattended%] == [yes] exit 1
pause
exit 1
:ensure_admin
:: 'openfiles' is just a commmand that is present on all supported Windows
:: versions, requires admin privileges and has no side effects, see:
:: https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
openfiles >nul 2>&1
if errorlevel 1 (
echo This batch script requires administrator privileges.
echo Right-click on handler-install.bat and select "Run as administrator".
call :die
)
goto :EOF
:ensure_vista
ver | find "XP" >nul
if not errorlevel 1 (
echo This batch script only works on Windows Vista and later. To create file
echo associations on Windows XP, right click on a video file and use "Open with...".
call :die
)
goto :EOF
:check_binary
cd /D %~dp0
set mpv_handler_conf=%cd%\config.toml
set mpv_handler_path=%cd%\mpv-handler.exe
set mpv_handler_debug_path=%cd%\mpv-handler-debug.exe
if not exist "%mpv_handler_conf%" call :die "Not found config.toml"
if not exist "%mpv_handler_path%" call :die "Not found mpv-handler.exe"
if not exist "%mpv_handler_debug_path%" call :die "Not found mpv-handler-debug.exe"
goto :EOF
:reg
:: Wrap the reg command to check for errors
>nul reg %*
if errorlevel 1 set error=yes
if [%error%] == [yes] echo Error in command: reg %*
if [%error%] == [yes] call :die
goto :EOF
:add_verbs
:: Add the mpv protocol to the registry
call :reg add "HKCR\mpv-handler" /d "URL:MPV Handler" /f
call :reg add "HKCR\mpv-handler" /v "Content Type" /d "application/x-mpv-handler" /f
call :reg add "HKCR\mpv-handler" /v "URL Protocol" /f
call :reg add "HKCR\mpv-handler\DefaultIcon" /d "\"%mpv_exe_path%\",1" /f
call :reg add "HKCR\mpv-handler\shell\open\command" /d "\"%mpv_handler_path%\" \"%%%%1\"" /f
:: Add the mpv protocol to the registry
call :reg add "HKCR\mpv-handler-debug" /d "URL:MPV Handler Debug" /f
call :reg add "HKCR\mpv-handler-debug" /v "Content Type" /d "application/x-mpv-handler-debug" /f
call :reg add "HKCR\mpv-handler-debug" /v "URL Protocol" /f
call :reg add "HKCR\mpv-handler-debug\DefaultIcon" /d "\"%mpv_exe_path%\",1" /f
call :reg add "HKCR\mpv-handler-debug\shell\open\command" /d "\"%mpv_handler_debug_path%\" \"%%%%1\"" /f
echo Successfully installed mpv-handler
echo Enjoy!
goto :EOF

View File

@@ -0,0 +1,64 @@
@echo OFF
:: Unattended install flag. When set, the script will not require user input.
set unattended=no
if "%1"=="/u" set unattended=yes
:: Make sure this is Windows Vista or later
call :ensure_vista
:: Make sure the script is running as admin
call :ensure_admin
:: Delete registry
call :del_verbs
:die
if not [%1] == [] echo %~1
if [%unattended%] == [yes] exit 1
pause
exit 1
:ensure_admin
:: 'openfiles' is just a commmand that is present on all supported Windows
:: versions, requires admin privileges and has no side effects, see:
:: https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
openfiles >nul 2>&1
if errorlevel 1 (
echo This batch script requires administrator privileges.
echo Right-click on handler-uninstall.bat and select "Run as administrator".
call :die
)
goto :EOF
:ensure_vista
ver | find "XP" >nul
if not errorlevel 1 (
echo This batch script only works on Windows Vista and later. To create file
echo associations on Windows XP, right click on a video file and use "Open with...".
call :die
)
goto :EOF
:reg
:: Wrap the reg command to check for errors
>nul reg %*
if errorlevel 1 set error=yes
if [%error%] == [yes] echo Error in command: reg %*
if [%error%] == [yes] call :die
goto :EOF
:del_verbs
:: Delete deprecated mpv and mpv-debug protocol
call :reg delete "HKCR\mpv" /f
call :reg delete "HKCR\mpv-debug" /f
:: Delete protocol
call :reg delete "HKCR\mpv-handler" /f
call :reg delete "HKCR\mpv-handler-debug" /f
echo Successfully uninstalled mpv-handler
goto :EOF

View File

@@ -0,0 +1,229 @@
#Requires -Version 5.1
#Requires -RunAsAdministrator
[CmdletBinding()]
param(
[string]$InstallRoot,
[string]$IconPath,
[switch]$KeepExistingProtocolKeys
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Resolve-ExistingFile {
param(
[Parameter(Mandatory = $true)]
[string]$BasePath,
[Parameter(Mandatory = $true)]
[string]$FileName
)
$resolvedPath = Join-Path -Path $BasePath -ChildPath $FileName
if (-not (Test-Path -LiteralPath $resolvedPath -PathType Leaf)) {
throw "Required file not found: $resolvedPath`nInstallRoot must point at the extracted mpv-handler folder that contains config.toml, mpv-handler.exe, and mpv-handler-debug.exe."
}
return (Resolve-Path -LiteralPath $resolvedPath).Path
}
function Get-UsageExample {
return @"
Example usage:
powershell -ExecutionPolicy Bypass -File .\scripts\install-mpv-handler.ps1 -InstallRoot 'C:\path\to\mpv-handler'
Or from an elevated PowerShell session:
& 'C:\Forgejo\API-MediaPlayer\scripts\install-mpv-handler.ps1' -InstallRoot 'C:\path\to\mpv-handler'
If config.toml, mpv-handler.exe, and mpv-handler-debug.exe are in the same folder as this script,
you can run it without -InstallRoot.
"@
}
function Test-InstallRootContents {
param(
[string]$CandidatePath
)
if (-not $CandidatePath) {
return $false
}
if (-not (Test-Path -LiteralPath $CandidatePath -PathType Container)) {
return $false
}
return (
(Test-Path -LiteralPath (Join-Path -Path $CandidatePath -ChildPath 'config.toml') -PathType Leaf) -and
(Test-Path -LiteralPath (Join-Path -Path $CandidatePath -ChildPath 'mpv-handler.exe') -PathType Leaf) -and
(Test-Path -LiteralPath (Join-Path -Path $CandidatePath -ChildPath 'mpv-handler-debug.exe') -PathType Leaf)
)
}
function Get-DefaultInstallRoot {
if (Test-InstallRootContents -CandidatePath $PSScriptRoot) {
return (Resolve-Path -LiteralPath $PSScriptRoot).Path
}
return $null
}
function Assert-InstallRoot {
param(
[string]$CandidatePath
)
if (-not $CandidatePath) {
$defaultInstallRoot = Get-DefaultInstallRoot
if ($defaultInstallRoot) {
return $defaultInstallRoot
}
throw "InstallRoot is required.`n$(Get-UsageExample)"
}
if (-not (Test-Path -LiteralPath $CandidatePath -PathType Container)) {
throw "InstallRoot does not exist or is not a folder: $CandidatePath`n$(Get-UsageExample)"
}
$resolved = (Resolve-Path -LiteralPath $CandidatePath).Path
if (Test-Path -LiteralPath (Join-Path -Path $resolved -ChildPath 'package.json') -PathType Leaf) {
throw "InstallRoot appears to be this repo, not the extracted mpv-handler folder: $resolved`n$(Get-UsageExample)"
}
return $resolved
}
function Get-MpvPathFromConfig {
param(
[Parameter(Mandatory = $true)]
[string]$ConfigPath
)
if (-not (Test-Path -LiteralPath $ConfigPath -PathType Leaf)) {
return $null
}
$raw = Get-Content -LiteralPath $ConfigPath -Raw
$match = [regex]::Match($raw, '(?m)^\s*mpv\s*=\s*"(?<path>[^"]+)"\s*$')
if (-not $match.Success) {
return $null
}
$candidate = $match.Groups['path'].Value.Trim()
if (-not $candidate) {
return $null
}
if ([System.IO.Path]::IsPathRooted($candidate) -and (Test-Path -LiteralPath $candidate -PathType Leaf)) {
return (Resolve-Path -LiteralPath $candidate).Path
}
try {
$command = Get-Command -Name $candidate -ErrorAction Stop
return $command.Source
} catch {
return $null
}
}
function Remove-ProtocolKey {
param(
[Parameter(Mandatory = $true)]
[string]$SchemeName
)
$classesRoot = [Microsoft.Win32.Registry]::ClassesRoot
try {
$classesRoot.DeleteSubKeyTree($SchemeName, $false)
} catch {
}
}
function Register-Protocol {
param(
[Parameter(Mandatory = $true)]
[string]$SchemeName,
[Parameter(Mandatory = $true)]
[string]$Description,
[Parameter(Mandatory = $true)]
[string]$ContentType,
[Parameter(Mandatory = $true)]
[string]$HandlerExecutable,
[Parameter(Mandatory = $true)]
[string]$EffectiveIconPath
)
$classesRoot = [Microsoft.Win32.Registry]::ClassesRoot
$schemeKey = $classesRoot.CreateSubKey($SchemeName)
if (-not $schemeKey) {
throw "Failed to create registry key for $SchemeName"
}
try {
$schemeKey.SetValue('', $Description, [Microsoft.Win32.RegistryValueKind]::String)
$schemeKey.SetValue('Content Type', $ContentType, [Microsoft.Win32.RegistryValueKind]::String)
$schemeKey.SetValue('URL Protocol', '', [Microsoft.Win32.RegistryValueKind]::String)
$defaultIconKey = $schemeKey.CreateSubKey('DefaultIcon')
try {
$defaultIconKey.SetValue('', ('"{0}",1' -f $EffectiveIconPath), [Microsoft.Win32.RegistryValueKind]::String)
} finally {
$defaultIconKey.Dispose()
}
$commandKey = $schemeKey.CreateSubKey('shell\open\command')
try {
$commandKey.SetValue('', ('"{0}" "%1"' -f $HandlerExecutable), [Microsoft.Win32.RegistryValueKind]::String)
} finally {
$commandKey.Dispose()
}
} finally {
$schemeKey.Dispose()
}
}
if ([System.Environment]::OSVersion.Platform -ne [System.PlatformID]::Win32NT) {
throw 'This installer is only for Windows.'
}
$installRootPath = Assert-InstallRoot -CandidatePath $InstallRoot
$configPath = Resolve-ExistingFile -BasePath $installRootPath -FileName 'config.toml'
$handlerPath = Resolve-ExistingFile -BasePath $installRootPath -FileName 'mpv-handler.exe'
$handlerDebugPath = Resolve-ExistingFile -BasePath $installRootPath -FileName 'mpv-handler-debug.exe'
$effectiveIconPath = if ($IconPath) {
if (-not (Test-Path -LiteralPath $IconPath -PathType Leaf)) {
throw "IconPath does not exist: $IconPath"
}
(Resolve-Path -LiteralPath $IconPath).Path
} else {
Get-MpvPathFromConfig -ConfigPath $configPath
}
if (-not $effectiveIconPath) {
$effectiveIconPath = $handlerPath
}
if (-not $KeepExistingProtocolKeys) {
Remove-ProtocolKey -SchemeName 'mpv'
Remove-ProtocolKey -SchemeName 'mpv-debug'
Remove-ProtocolKey -SchemeName 'mpv-handler'
Remove-ProtocolKey -SchemeName 'mpv-handler-debug'
}
Register-Protocol -SchemeName 'mpv-handler' -Description 'URL:MPV Handler' -ContentType 'application/x-mpv-handler' -HandlerExecutable $handlerPath -EffectiveIconPath $effectiveIconPath
Register-Protocol -SchemeName 'mpv-handler-debug' -Description 'URL:MPV Handler Debug' -ContentType 'application/x-mpv-handler-debug' -HandlerExecutable $handlerDebugPath -EffectiveIconPath $effectiveIconPath
Write-Host 'Successfully installed mpv-handler protocol registration.' -ForegroundColor Green
Write-Host ('InstallRoot: {0}' -f $installRootPath)
Write-Host ('Handler: {0}' -f $handlerPath)
Write-Host ('Debug Handler: {0}' -f $handlerDebugPath)
Write-Host ('Icon: {0}' -f $effectiveIconPath)
Write-Host 'You can now test an mpv-handler:// URL from the browser.'

Binary file not shown.

BIN
scripts/mpv-handler.exe Normal file

Binary file not shown.