emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Tip] Screenshots as org links with EMMS and socat
@ 2022-06-18 14:35 Juan Manuel Macías
  0 siblings, 0 replies; only message in thread
From: Juan Manuel Macías @ 2022-06-18 14:35 UTC (permalink / raw)
  To: orgmode

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-18 14:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-18 14:35 [Tip] Screenshots as org links with EMMS and socat Juan Manuel Macías

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).