about summary refs log tree commit diff
path: root/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2024-09-27T23·30+0200
committerProfpatsch <mail@profpatsch.de>2024-10-05T13·49+0000
commit9bec21ea1cb9137d8c5dae6ee2cb78aac1b5601a (patch)
treeec649db1716bc7b0a7a5d42b8adb4f0c81410659 /users/Profpatsch/lyric/lyric-timing-mpv-script.lua
parent970dcaa04f3c4bda473674f7e7bb2d2d87ab13e8 (diff)
feat(users/Profpatsch/lyric): add vscode extension & helpers r/8759
* tap-bpm: simple CLI program that accepts key inputs and averages a
BPM value

* lyric-timing-mpv-script: If you press Ctrl+l, mpv attaches the
  current timestamp to a .lrc file named after the song.
  This is for manually timing missing songs for uploading them to
  https://lrclib.net/

* extension: vscode extension for `.lrc` files, currently with the
  following features:

    1. A “jump to LRC position” command which reads an .lrc timestamp
    from the current line and expects mpv to listen on
    `~/tmp/mpv-socket` (via `--input-ipc-server`), and will seek to
    the exact timestamp (down to the ms) in the currently playing
    song.

    2. Some initial linting warnings

      - A lint that warns if the difference to the next timestamp is
      more than 10s (which usually means there’s an instrumental and
      the previous line is stuck)

      - A lint that checks that timestamps are monotonically
      increasing

Change-Id: I32a4ac0e2c5bbe3d94e45ffcf647f81bc7c08aa0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12537
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch/lyric/lyric-timing-mpv-script.lua')
-rw-r--r--users/Profpatsch/lyric/lyric-timing-mpv-script.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/users/Profpatsch/lyric/lyric-timing-mpv-script.lua b/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
new file mode 100644
index 000000000000..6558422f1092
--- /dev/null
+++ b/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
@@ -0,0 +1,43 @@
+-- This function formats the current timestamp in the [mm:ss.ms] format
+function format_timestamp(seconds)
+    local minutes = math.floor(seconds / 60)
+    local seconds = seconds % 60
+    return string.format("[%02d:%05.2f]", minutes, seconds)
+end
+
+-- Get the user’s cache directory
+local cache_dir = os.getenv("XDG_CACHE_HOME") or os.getenv("HOME") .. "/.cache"
+
+-- This function writes the timestamp to the LRC file
+function write_timestamp_to_lrc()
+    local filename = mp.get_property("path")
+    if not filename then
+        mp.msg.warn("No file currently playing.")
+        return
+    end
+
+    -- Extract metadata for artist and title
+    local artist = mp.get_property("metadata/by-key/ARTIST", "Unknown Artist")
+    local title = mp.get_property("metadata/by-key/TITLE", "Unknown Title")
+
+    -- Construct the lrc dir
+    local dir = cache_dir .. "/lyric/timed"
+    local lrc_filename = string.format("%s/%s - %s.lrc", dir, artist, title)
+
+    -- Get current playback time
+    local current_time = mp.get_property_number("time-pos", 0)
+    local formatted_time = format_timestamp(current_time)
+
+    -- Append the timestamp to the LRC file
+    local file = io.open(lrc_filename, "a")
+    if file then
+        file:write(formatted_time .. "\n")
+        file:close()
+        mp.msg.info("Timestamp " .. formatted_time .. " added to " .. lrc_filename)
+    else
+        mp.msg.error("Failed to open " .. lrc_filename)
+    end
+end
+
+-- Bind Ctrl+l to the function that writes the timestamp
+mp.add_key_binding("Ctrl+l", "insert_timestamp", write_timestamp_to_lrc)