[Linux] Paste media timestamp from external app

Premise

Since the embedded video player is not enough for my needs, I decided to use my favorite video player, VLC, to play videos while I annotate them in Logseq, but I needed a way to copy the timestamp from VLC and paste it into Logseq without switching application.

The Bash script below copy the timestamp from VLC (in microseconds), convert it in hh:mm:ss format and copy the following

 {{renderer :media-timestamp, hh:mm:ss}}: 

to the clipboard, even if VLC is not the focused window.

This script uses MPRIS protocol to read from VLC the timestamp and can be adapted to any video/audio player (even web browsers) that supports MPRIS.

It uses the Wayland clipboard but porting this to X11 is trivial.

I used Plasma’s System Settings to assign Meta + C as a global shortcut to this script. So in Logseq I type Meta+C, Ctrl+V and get this:


Script

#!/bin/bash

sec=`qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Position | rev | cut -c7- | rev`

timestamp=`date -d@$sec -u +%H:%M:%S`

wl-copy "{{renderer :media-timestamp, $timestamp}}: "