Skip to content
Advertisement

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.

Advertisement

Answer

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/%sn" "$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

Advertisement