获取mpv中当前播放文件的完整路径

Get full path of currently playing file in mpv

有没有办法在 mpv 启动后从 mpv 获取当前播放文件的完整路径?

我看到了 问题,但它没有显示如何获取属性,只显示了如何发送命令。

编辑:'get the full path',我的意思是通过编程;来自另一个程序或终端,而不是在 mpv 应用程序本身上使用 mpv commands/keybindings。

为此,您必须使用 --input-ipc-server 选项启动 mpv,或将其放入 mpv.conf 文件中。看起来像:

--input-ipc-server=/tmp/mpvsocket

mpv.conf 文件中没有破折号:

input-ipc-server=/tmp/mpvsocket

套接字连接到使用相同 input-ipc-server 启动的最新 mpv 实例。

然后,您可以使用如下命令:

echo '{ "command": ["get_property", "<some property>"] }' | socat - /tmp/mpvsocket

例如:

$ echo '{ "command": ["get_property", "path"] }' | socat - /tmp/mpvsocket
{"data":"01 - Don't Know Why.mp3","request_id":0,"error":"success"}

您可以通过 mpv --list-properties

获取属性列表

要获得完整路径,请组合 working-directorypath 属性。可以使用 jq 解析响应,因此对于所需的输出:

#!/bin/sh

SOCKET='/tmp/mpvsocket'

# pass the property as the first argument
mpv_communicate() {
  printf '{ "command": ["get_property", "%s"] }\n' "" | socat - "${SOCKET}" | jq -r ".data"
}

WORKING_DIR="$(mpv_communicate "working-directory")"
FILEPATH="$(mpv_communicate "path")"

printf "%s/%s\n" "$WORKING_DIR" "$FILEPATH"

编辑:我在上面的脚本中添加了额外的错误处理; mpv-currently-playing。除非您确定它正在播放本地文件,否则不应总是尝试计算绝对路径。如果它是 URL,那最终可能会搞乱 scheme/location

试试这个:

echo '{ "command": ["get_property", "playlist"] }' | socat - /tmp/mpvsocket |jq '.data[].filename'

"/mnt/d6/media/vid.mp4"

# -- 1
echo '{ "command": ["get_property", "playlist"] }' | socat - /tmp/mpvsocket # |jq . '.data[].filename'

{"data":[{"filename":"/mnt/d6/media/vid.mp4","current":true,"playing":true}],"request_id":0,"error":"success"}


# -- 2
echo '{ "command": ["get_property", "playlist"] }' | socat - /tmp/mpvsocket |jq . # '.data[].filename'

{
  "data": [
    {
      "filename": "/mnt/d6/media/vid.mp4",
      "current": true,
      "playing": true
    }
  ],
  "request_id": 0,
  "error": "success"
}


# -- 3
echo '{ "command": ["get_property", "playlist"] }' | socat - /tmp/mpvsocket |jq '.data[].filename'

"/mnt/d6/media/vid.mp4"


# -- 4
enjoy ;)


# -- jq
https://www.baeldung.com/linux/jq-command-json
https://stedolan.github.io/jq/tutorial/

jq is like sed for JSON data :

you can use it to slice and filter and map and transform structured data
with the same ease that sed, awk, grep and 
friends let you play with text.

将其放入您的 mpv 配置文件中以在打开时显示完整路径

osd-playing-msg=${path}