MPV的进阶使用:JSON IPC

发布时间 2023-11-22 17:01:59作者: eiSouthBoy

一、背景介绍

在一个项目中需要用到mpv播放器,通过ipc控制mpv播放器,实现如下功能:

  • 暂停/播放
  • 音量调节
  • 切换视频文件
  • 跳转播放进度

在浏览器上找了一段时间,发现没有分享过需求的案例。只能通过mpv官网手册的介绍,一字一句的浏览和查询,找到相关的信息。

Using mpv from other programs or scripts (从其他程序或脚本中使用mpv),这不就是我要找的嘛

Using mpv from other programs or scripts
There are three choices for using mpv from other programs or scripts:

1. Calling it as UNIX process. If you do this, do not parse terminal output. The terminal output is intended for humans, and may change any time. In addition, terminal behavior itself may change any time. Compatibility cannot be guaranteed.

Your code should work even if you pass --no-terminal. Do not attempt to simulate user input by sending terminal control codes to mpv's stdin. If you need interactive control, using --input-ipc-server is recommended. This gives you access to the JSON IPC over unix domain sockets (or named pipes on Windows).

Depending on what you do, passing --no-config or --config-dir may be a good idea to avoid conflicts with the normal mpv user configuration intended for CLI playback.

Using --input-ipc-server is also suitable for purposes like remote control (however, the IPC protocol itself is not "secure" and not intended to be so).

2. Using libmpv. This is generally recommended when mpv is used as playback backend for a completely different application. The provided C API is very close to CLI mechanisms and the scripting API.

Note that even though libmpv has different defaults, it can be configured to work exactly like the CLI player (except command line parsing is unavailable).

See EMBEDDING INTO OTHER PROGRAMS (LIBMPV).

3. As a user script (LUA SCRIPTING, JAVASCRIPT, C PLUGINS). This is recommended when the goal is to "enhance" the CLI player. Scripts get access to the entire client API of mpv.

This is the standard way to create third-party extensions for the player.



All these access the client API, which is the sum of the various mechanisms provided by the player core, as documented here: OPTIONS, List of Input Commands, Properties, List of events (also see C API), Hooks.

大概意思:这有三种方式可供选择

  • --input-ipc-server :该参数允许你通过unix域套接字(或Windows上的有名管道)访问JSON IPC

  • libmpv库:该库提供C API 接口,可以在代码中直接调用

  • user script:脚本文件有:lua scripting 、javascript 、c plugins,脚本可以允许访问mpv客户端api

这里通过 --input-ipc-server 来实现需求。

二、JSON IPC

关于JSON IPC的介绍:

mpv can be controlled by external programs using the JSON-based IPC protocol. It can be enabled by specifying the path to a unix socket or a named pipe using the option --input-ipc-server. Clients can connect to this socket and send commands to the player or receive events from it.

释义:基于JSON的IPC协议的外部程序可以控制mpv。使用--input-ipc-server 选项 指定一个路径给unix套接字或有名管道而被启动。客户端能连接这个套接字并发送命令给播放器或从播放器接收事件。

在windows的JSON IPC中的缺陷:

Unfortunately, it's not as easy to test the IPC protocol on Windows, since Windows ports of socat (in Cygwin and MSYS2) don't understand named pipes. In the absence of a simple tool to send and receive from bidirectional pipes, the echo command can be used to send commands, but not receive replies from the command prompt.

释义:很不幸,在Windows上测试 这个IPC协议不太容易,因为Windows的socat端口不能理解有名管道。缺少一个简单的工具从双向管道发送和接收,echo 命令仅能被用作发送命令行,而不能从命令行提示接收应答。

注意事项1:命令行使用uft8编码格式

To avoid getting conflicts, encode all text characters including and above codepoint U+0020 as UTF-8.

注意事项2:命令行必须用换行符\n结尾,且该字符不能出现在命令行的里面任何位置

Every message must be terminated with \n. Additionally, \n must not appear anywhere inside the message

JSON命令的一般格式:{ "command": ["command_name", "param1", "param2", ...] }

2.1 暂停/播放

mpv的暂停/播放通过参数: pause (布尔值)设置

mpv播放视频文件:mpv "D:\temp\qingshe.mp4" --input-ipc-server=\\.\pipe\mpvsocket

另外启动一个cmd窗口暂停视频:echo { "command": ["set_property", "pause", true] } > \\.\pipe\mpvsocket

2.2 音量调节

mpv的音量调节通过参数:volume (整型) 设置

设置音量为80:echo { "command": ["set_property", "volume", 80] } > \\.\pipe\mpvsocket

? 一般发送命令后会有一个返回值信息。但是在Windows上仅能发送命令,却不能接收返回值信息。

2.3 切换视频文件

更换正在播放的视频:echo { "command": [ "loadfile", "d:\\temp\\blue.mkv"] } > \\.\pipe\mpvsocket

3.4 跳转播放进度

跳转到指定位置:echo { "command": [ "seek", "00:01:00" , "absolute" ] } > \\.\pipe\mpvsocket

3.5 添加视频文件到播放列表

添加视频文件命令:echo { "command": [ "loadfile", "D:\\temp\\qingshe.mp4", "append" ] } > \\.\pipe\mpvsocket

其他属性

设置全屏:echo { "command": ["set_property", "fullscreen", true] } > \\.\pipe\mpvsocket

设置窗口Title:echo { "command": ["set_property", "title", "blue.mkv"] } > \\.\pipe\mpvsocket

视频截图:echo { "command": [ "screenshot" ] } > \\.\pipe\mpvsocket

倍速播放:echo { "command": [ "set_property", "speed", 1 ] } > \\.\pipe\mpvsocket
(speed取值范围:0.01~100)

设置静音:echo { "command": [ "set_property", "mute", false ] } > \\.\pipe\mpvsocket