m1gin 66

#linux #ubuntu #mpv #cli #socket

Starting MPV to be controlled by JSON IPC:

mpv --input-ipc-server='/tmp/mpvsocket' video.mp4


#https://mpv.io/manual/master/#json-ipc
#commands: https://mpv.io/manual/master/#list-of-input-commands
#properties: https://mpv.io/manual/master/#properties
#events: https://mpv.io/manual/master/#list-of-events

#nc can also be used instead of socat
echo '{ "command": ["get_property", "sub-text"] }' | nc -q 0 -U /tmp/mpvsocket

#read json
echo '{"data":421319855,"error":"success"}' | jq ".data"


get filename

echo '{ "command": ["get_property", "filename"] }' | socat - /tmp/mpvsocket
#{"data":"video.mp4","error":"success"}

get filename without extension

echo '{ "command": ["get_property", "filename/no-ext"] }' | socat - /tmp/mpvsocket
#{"data":"video","error":"success"}


get path

echo '{ "command": ["get_property", "path"] }' | socat - /tmp/mpvsocket
#{"data":"/home/user/test/video.mp4","error":"success"}

get working directory

echo '{ "command": ["get_property", "working-directory"] }' | socat - /tmp/mpvsocket
#{"data":"/home/user/test","error":"success"}


get file size

echo '{ "command": ["get_property", "file-size"] }' | socat - /tmp/mpvsocket
#{"data":421319855,"error":"success"}


#get total length in seconds
echo '{ "command": ["get_property", "duration"] }' | socat - /tmp/mpvsocket
#{"data":2466.884000,"error":"success"}


get current position..

echo '{ "command": ["get_property", "playback-time"] }' | socat - /tmp/mpvsocket
#{"data":159.076000,"error":"success"}


#get remaining time, calculated by speed rate

echo '{ "command": ["get_property", "playtime-remaining"] }' | socat - /tmp/mpvsocket
#{"data":2319.236000,"error":"success"}

#show filename on screen

echo 'show-text "file: ${filename}"' | socat - /tmp/mpvsocket

#show subtitle line for 5 seconds

echo 'show-text "${sub-text}" "5000" ' | socat - /tmp/mpvsocket

#show time on video screen

echo 'show-text ${playback-time}' | socat - /tmp/mpvsocket

#show current time as seconds

echo 'show-text "time: ${=time-pos}"' | socat - /tmp/mpvsocket


#add id to the request...

echo '{ "command": ["get_property", "time-pos"], "request_id": 100 }' | socat - /tmp/mpvsocket
#{"data":487.905000,"request_id":100,"error":"success"}

#get volume

echo '{ "command": ["get_property", "volume"] }' | socat - /tmp/mpvsocket
#{"data":100.000000,"error":"success"}

#get speed

echo '{ "command": ["get_property", "speed"] }' | socat - /tmp/mpvsocket
#{"data":1.000000,"error":"success"}

#set speed

echo '{ "command": ["set_property", "speed", 1.4] }' | socat - /tmp/mpvsocket
#{"error":"success"}


#get position as microseconds

echo '{ "command": ["get_time_us"] }' | socat - /tmp/mpvsocket
#{"data":1010179736,"error":"success"}

#get current subtitle line

echo '{ "command": ["get_property", "sub-text"] }' | socat - /tmp/mpvsocket
#{"data":"- What's wrong with your mom?\u000a- She's a diabetic.","error":"success"}

#pause

{ "command": ["set_property", "pause", true] }


#jump 10 seconds forward

echo '{ "command": ["seek", 10] }' | socat - /tmp/mpvsocket
#{"data":null,"error":"success"}

#jump 5 seconds backward
echo '{ "command": ["seek", -5] }' | socat - /tmp/mpvsocket
#{"data":null,"error":"success"}

#jump to specific seconds
echo '{ "command": ["seek", 100, "absolute"] }' | socat - /tmp/mpvsocket
#{"data":null,"error":"success"}


#sid alias sub

#get active subtitle id
echo '{ "command": ["get_property", "sid"] }' | socat - /tmp/mpvsocket
#{"data":1,"error":"success"}

#remove subtitle
echo '{ "command": ["sub-remove", "1"] }' | socat - /tmp/mpvsocket

#disable subtitle
echo '{ "command": ["set_property", "sid", "no"] }' | socat - /tmp/mpvsocket
#{"error":"success"}

#set subtitle
echo '{ "command": ["set_property", "sid", "1"] }' | socat - /tmp/mpvsocket

echo '{ "command": ["set_property", "sub", "auto"] }' | socat - /tmp/mpvsocket
{"error":"success"}


#append a file
echo '{ "command": ["loadfile", "https://test.com/v.mp4", "append"] }' | socat - /tmp/mpvsocket

#loop a file...
--loop-file=<N|inf|no>, --loop=<N|inf|no>

#observe a property... need to read socket for output..
echo '{ "command": ["observe_property", 222, "volume"] }' | socat - /tmp/mpvsocket

#stop observing a property
{ "command": ["unobserve_property", 222] }


#get metadata
echo '{ "command": ["get_property", "metadata"] }' | socat - /tmp/mpvsocket
#{"data":{"title":"Enlem_ve_Boylam_111_Kasim_201","artist":"M. Birgin","album":"www.mbirgin.com","date":"2017","comment":"www.mbirgin.com","genre":"Blues"},"error":"success"}


#get audio info
echo '{ "command": ["get_property", "audio-params"] }' | socat - /tmp/mpvsocket
#{"data":{"samplerate":44100,"channel-count":2,"channels":"stereo","hr-channels":"stereo","format":"floatp"},"error":"success"}

#get bitrate
echo '{ "command": ["get_property", "audio-bitrate"] }' | socat - /tmp/mpvsocket
#{"data":192003,"error":"success"}

#get mpv version
echo '{ "command": ["get_property", "mpv-version"] }' | socat - /tmp/mpvsocket


#https://mpv.io/manual/master/#command-interface-idle-active

Add to: