Hi all,

I’m writing an article about a movie, and I needed to get some
screenshots as image links inside Org. I know some package for those
things, like org-media-note, a nice library but for me it has two
drawbacks: it has, for what I need, too many bells and whistles; and
uses the mpv.el package in the background. I use the mpv player, but
through EMMS. In fact, I have all my multimedia setup in Emacs around
EMMS, and I'm too lazy to use something else. What I am looking for is,
therefore, something much simpler, EMMS-centric and "homemade". If
someone is in the same situation as me and also uses EMMS with mpv, I’m
sharing my solution here, in case it’s useful (I’ve taken some ideas
from org-media-note and mpv.el):

Socat needs to be installed first, to communicate with the mpv process
via the command line. In Arch it is in the official repositories:

┌────
│ sudo pacman -S socat
└────

My EMMS configuration:

┌────
│ (require 'emms-setup)
│ (emms-all)
│ (emms-default-players)
│ (setq emms-player-list '(emms-player-mpv))
└────

And these two variables are for socat communication:

┌────
│ (setq emms-player-mpv-ipc-method 'ipc-server)
│ (setq emms-player-mpv-ipc-socket "~/.emacs.d/emms/mpv-ipc.sock")
└────

The directory to save the screenshots:

┌────
│ (defvar my-screenshot-dir "/path/to/screenshot/dir/")
└────

This function returns the formatted timestamp and path of the current
mpv process:

┌────
│ (require 'org-timer)
│ (defun my-mpv-format-timestamp-and-path ()
│   (let* ((timestamp-command (shell-command-to-string
│         "echo '{ \"command\": [\"get_property\", \"playback-time\"] }' | 
socat - ~/.emacs.d/emms/mpv-ipc.sock"))
│        (path-command (shell-command-to-string
│       "echo '{ \"command\": [\"get_property\", \"path\"] }' | socat - 
~/.emacs.d/emms/mpv-ipc.sock"))
│        (timestamp (org-timer-secs-to-hms (round
│                                           (cdr
│                                            (car
│                                             (json-read-from-string 
timestamp-command))))))
│        (path (cdr
│               (car
│                (json-read-from-string path-command)))))
│     (format "%s --- %s" path timestamp)))
└────

And, finally, the function that inserts the screenshot at point as an
org image link:

┌────
│ (defun my-mpv-put-screenshot-on-org ()
│   (let* ((time (format-time-string "%d-%m-%y-%H-%M-%S"))
│        (screenshot-file-name (format "fotograma-%s.png" time))
│        (screenshot-final (expand-file-name screenshot-file-name 
my-screenshot-dir)))
│     (start-process-shell-command 
│      "screenshot" nil
│      (format
│       "echo \"screenshot-to-file %s\" | socat - 
\"~/.emacs.d/emms/mpv-ipc.sock\""
│       screenshot-final))
│     (insert (format "#+media: %s\n" (my-mpv-format-timestamp-and-path)))
│     (insert (format "[[file:%s]]" screenshot-final))))
└────

A short demo video: https://cloud.disroot.org/s/6zrGYxkKT67kFGx

Best regards,

Juan Manuel

Reply via email to