purarue
purarue

Reputation: 2164

Get full path of currently playing file in mpv

Is there a way to get the full path of the currently playing file from mpv, after mpv has been launched?

I saw this question but it doesn't show how to get properties, just how send commands.

Edit: by 'get the full path', I mean from programatically; from another program or a terminal, not by using mpv commands/keybindings on the mpv application itself.

Upvotes: 6

Views: 6601

Answers (3)

Alex Zubkov
Alex Zubkov

Reputation: 127

Put this in your mpv config file to show full path upon opening

osd-playing-msg=${path}

Upvotes: 0

purarue
purarue

Reputation: 2164

To do this, you have to start mpv with the --input-ipc-server option, or put that in your mpv.conf file. That would look like:

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

or without the dashes in the mpv.conf file:

input-ipc-server=/tmp/mpvsocket

The socket is connected to the most recent mpv instance launched with the same input-ipc-server.

Then, you can use a command like:

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

For example:

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

You can get a list of properties by doing mpv --list-properties

To get the full path, combine the working-directory and path properties. The response can be parsed with jq, so for the desired output:

#!/bin/sh

SOCKET='/tmp/mpvsocket'

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

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

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

Edit: I've since added additional error handling to what the above script became; mpv-currently-playing. Shouldn't always try to compute an absolute path unless you're sure its playing a local file. If its a URL, that could end up messing up the scheme/location

Upvotes: 3

hong tong
hong tong

Reputation: 21

try this :

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

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

ie

# -- 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.

Upvotes: 1

Related Questions