Get full path of currently playing file in mpv
Asked Answered
I

3

6

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.

Inhabit answered 25/6, 2020 at 19:5 Comment(0)
I
3

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

Inhabit answered 25/6, 2020 at 19:5 Comment(1)
been playing with this a bunch more since I left this question; left my random scripts up here incase anyone wants more examples.Inhabit
A
1

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.
Ally answered 11/4, 2021 at 10:9 Comment(0)
E
0

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

osd-playing-msg=${path}
Episodic answered 7/6, 2021 at 5:54 Comment(1)
Guess its related, but wasn't exactly my question (as my answer described) -- I wanted to get the path from a terminal or another program, not from within mpv itself. Have updated the question to make that more obviousInhabit

© 2022 - 2024 — McMap. All rights reserved.