emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: [Tip] Screenshots as org links with EMMS and socat
Date: Sat, 18 Jun 2022 14:35:44 +0000	[thread overview]
Message-ID: <871qvmt4xr.fsf@posteo.net> (raw)

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	other threads:[~2022-06-18 14:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871qvmt4xr.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


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).