diff options
author | Profpatsch <mail@profpatsch.de> | 2024-09-13T17·12+0200 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2024-10-05T13·49+0000 |
commit | ffdcb3bb5e8d7689254c82260a29f5d6b2a09281 (patch) | |
tree | 270fb01f12d3e355fb041d62043d1ffa9bf0babe /users/Profpatsch | |
parent | 7fedfe1cde1d1b021c50f3cbd4f620027c85a7ce (diff) |
feat(users/Profpatsch/lyrics): integrate into an mpv extension r/8757
Slop it together! The mpv script is entirely generated by ChatGPT. Whoooooooooo Change-Id: Ic284d142c2f1fd6d407af6b3571db0e815604051 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12478 Reviewed-by: Profpatsch <mail@profpatsch.de> Tested-by: BuildkiteCI
Diffstat (limited to 'users/Profpatsch')
-rw-r--r-- | users/Profpatsch/lyric.nix | 54 | ||||
-rw-r--r-- | users/Profpatsch/lyric/default.nix | 92 | ||||
-rw-r--r-- | users/Profpatsch/lyric/lyric-mpv-script.lua | 57 | ||||
-rw-r--r-- | users/Profpatsch/nix-home/default.nix | 4 |
4 files changed, 149 insertions, 58 deletions
diff --git a/users/Profpatsch/lyric.nix b/users/Profpatsch/lyric.nix deleted file mode 100644 index b3914d195e1f..000000000000 --- a/users/Profpatsch/lyric.nix +++ /dev/null @@ -1,54 +0,0 @@ -# Display lyrics for the given search string; -# search string can contain a substring of band name, album name, song title -# -# Use the database dump from https://lrclib.net/db-dumps and place it in ~/.cache/lyric/lrclib-db-dump.sqlite3 - -{ depot, pkgs, lib, ... }: - -let - bins = depot.nix.getBins pkgs.sqlite-utils [ "sqlite-utils" ] - // depot.nix.getBins pkgs.jq [ "jq" ]; - -in -depot.nix.writeExecline "lyric" { readNArgs = 1; } [ - "backtick" - "-E" - "cache" - [ depot.users.Profpatsch.xdg-cache-home ] - "pipeline" - [ - bins.sqlite-utils - "query" - "\${cache}/lyric/lrclib-db-dump.sqlite3" - '' - select - synced_lyrics, - has_synced_lyrics, - plain_lyrics - from - tracks_fts(:searchstring) tf - join tracks t on t.rowid = tf.rowid - join lyrics l on t.rowid = l.track_id - order by - t.id - limit - 1 - '' - "--param" - "searchstring" - "$1" - ] - bins.jq - "-r" - '' - if .[0] == null - then "" - else - .[0] - | if .has_synced_lyrics == 1 - then .synced_lyrics - else .plain_lyrics - end - end - '' -] diff --git a/users/Profpatsch/lyric/default.nix b/users/Profpatsch/lyric/default.nix new file mode 100644 index 000000000000..c20f28b18742 --- /dev/null +++ b/users/Profpatsch/lyric/default.nix @@ -0,0 +1,92 @@ +{ pkgs, depot, lib, ... }: + +let + bins = depot.nix.getBins pkgs.sqlite-utils [ "sqlite-utils" ] + // depot.nix.getBins pkgs.jq [ "jq" ]; + + mpv-script = pkgs.writeTextFile { + name = "lyric.lua"; + text = + lib.replaceStrings + [ "@get_subtitles_command@" ] + [ (toString lyric-to-temp-file) ] + (builtins.readFile ./lyric-mpv-script.lua); + derivationArgs.passthru.scriptName = "lyric.lua"; + }; + + lyric-to-temp-file = depot.nix.writeExecline "lyric-to-temp-file" { readNArgs = 1; } [ + "backtick" + "-E" + "cache" + [ depot.users.Profpatsch.xdg-cache-home ] + "if" + [ "mkdir" "-p" "\${cache}/lyric/as-files" ] + "if" + [ + "redirfd" + "-w" + "1" + "\${cache}/lyric/as-files/\${1}.lrc" + lyric + "$1" + ] + "printf" + "\${cache}/lyric/as-files/\${1}.lrc" + ]; + + # Display lyrics for the given search string; + # search string can contain a substring of band name, album name, song title + # + # Use the database dump from https://lrclib.net/db-dumps and place it in ~/.cache/lyric/lrclib-db-dump.sqlite3 + lyric = + + (depot.nix.writeExecline "lyric" { readNArgs = 1; } [ + "backtick" + "-E" + "cache" + [ depot.users.Profpatsch.xdg-cache-home ] + "pipeline" + [ + bins.sqlite-utils + "query" + "\${cache}/lyric/lrclib-db-dump.sqlite3" + '' + select + synced_lyrics, + has_synced_lyrics, + plain_lyrics + from + tracks_fts(:searchstring) tf + join tracks t on t.rowid = tf.rowid + join lyrics l on t.rowid = l.track_id + order by + has_synced_lyrics desc, t.id + limit + 1 + '' + "--param" + "searchstring" + "$1" + ] + bins.jq + "-r" + '' + if .[0] == null + then "" + else + .[0] + | if .has_synced_lyrics == 1 + then .synced_lyrics + else .plain_lyrics + end + end + '' + ]); + + +in +{ + inherit + lyric + mpv-script; +} diff --git a/users/Profpatsch/lyric/lyric-mpv-script.lua b/users/Profpatsch/lyric/lyric-mpv-script.lua new file mode 100644 index 000000000000..e492bf687140 --- /dev/null +++ b/users/Profpatsch/lyric/lyric-mpv-script.lua @@ -0,0 +1,57 @@ +-- get_lrc_subtitles.lua + +-- Asynchronous callback function to handle the result of the 'get_subtitles' command +function handle_subtitle_result(success, result) + if not success or result.status ~= 0 then + mp.msg.error("Failed to get subtitles") + mp.msg.error("Exit code: " .. tostring(result.exit_code)) + if result.stderr then + mp.msg.error("Error: " .. result.stderr) + end + return + end + + -- Extract the output from the external command + local subtitle_file = result.stdout:match("^%s*(.-)%s*$") -- trim any extra whitespace + + -- If no subtitle file was returned, stop + if not subtitle_file or subtitle_file == "" then + mp.msg.warn("No subtitle file found") + return + end + + -- Load the subtitle file in MPV + mp.commandv("sub-add", subtitle_file) + mp.msg.info("Loaded subtitle file: " .. subtitle_file) +end + +-- Function to load the LRC subtitle file +function load_lrc_subtitles() + -- Retrieve metadata for the currently playing file + local artist = mp.get_property("metadata/by-key/artist") + local album = mp.get_property("metadata/by-key/album") + local title = mp.get_property("metadata/by-key/title") + + -- If any metadata is missing, print a warning and stop + if not artist or not album or not title then + mp.msg.warn("Artist, album, or title metadata is missing") + return + end + + -- Concatenate the metadata + local query = string.format("%s %s %s", artist, album, title) + + -- Create the command array + local cmd = {"@get_subtitles_command@", query} + + -- Run the external command asynchronously to get the subtitle file + mp.command_native_async({ + name = "subprocess", + args = cmd, + capture_stdout = true, + capture_stderr = true + }, handle_subtitle_result) +end + +-- Register the event that triggers when a file is loaded +mp.register_event("file-loaded", load_lrc_subtitles) diff --git a/users/Profpatsch/nix-home/default.nix b/users/Profpatsch/nix-home/default.nix index 72c77122fc9b..6d5de19aedcd 100644 --- a/users/Profpatsch/nix-home/default.nix +++ b/users/Profpatsch/nix-home/default.nix @@ -158,10 +158,6 @@ let name = "scripts/lw"; path = depot.users.Profpatsch.lorri-wait-for-eval; } - { - name = "scripts/lyric"; - path = depot.users.Profpatsch.lyric; - } ] ++ (lib.pipe depot.users.Profpatsch.aliases [ |