kljohann / mpv.el

control mpv for easy note taking
GNU General Public License v3.0
145 stars 15 forks source link

Expose a function to call mpv commands directly, similar to mpv--enque #3

Closed mihaiolteanu closed 4 years ago

mihaiolteanu commented 4 years ago

Expose a function similar to mpv--enque.

Something generic, so that calls like (mpv-command "get_property" "playback-time") can be possible. The user can then call most of the mpv commands.

Or something more specific, so that calls like these and others, can be made possible: (mpv-get-property "playback-time") (mpv-set-property "percent-pos" 15)

kljohann commented 4 years ago

That's a good idea. The easiest way to implement this would be to stick to an asynchronous interface, where you would have to provide a callback to process the response. However a blocking call might be more convenient.

mihaiolteanu commented 4 years ago

I'm not saying this is the solution, but it might work,

(shell-command-to-string
  "echo '{ \"command\": [\"get_property\", \"playback-time\"] }' | socat - /tmp/mpv-fbxEpt")

This is a synchronous call. It passes the mpv command get-property to socat, which uses the socket opened when the mpv-player was started.

You can then pass the output from this command to a json function,

(require 'json)
(json-read-from-string output-from-prev-command)

If you implement a generic function where "get_property" and "playback-time" can be passed as parameters, you can implement the kind of functions I was talking about in the initial post with just one line of code each.

By the way, I've noticed that you don't clean up the tmp socket files after you close them. I have like 40 files already in my /tmp folder. But that might be the topic of another issue.

kljohann commented 4 years ago

I just gave it a go. Can you have a look at the "next" branch and see if it does what you want? There are no specific {get,set}-property commands yet, but you should be able to call

(mpv-run-command "get_property" "playback-time")

(The issue with the stale files should also be fixed on that branch.)

mihaiolteanu commented 4 years ago

I've tried these,

(mpv-run-command "get_property" "playback-time")
(mpv-run-command "set_property" "playback-time" "0")

They both work as I would expect them to work. Nice! :)

Let me know if you need more input from my side. I think he easiest interfaces for the users would be something like mpv-playback-time, mpv-replay, mpv-rewind, mpv-length, etc. for the most used/useful mpv commands. For the most esoteric ones, you can provide this generic interface and the user can study the mpv manual and write his own. At least you're not locking him out if you don't provide the functionality yourself.

kljohann commented 4 years ago

These functions are now available on master. Thanks again for the feature request. I'm not quite sure what you meant by mpv-replay and mpv-rewind, though. Do (mpv-cycle-property "loop") and (mpv-seek 0.0) come close?

kljohann commented 4 years ago

Also, if you feel that any particular (interactive) function is missing, I will gladly add it.

mihaiolteanu commented 4 years ago

Yes, (mpv-seek 0.0) would be my (mpv-replay) and (mpv-seek-backward 5) would be my (mpv-rewind 5).

I've tried your changes and it works great, thank you very much.