feat(users/Profpatsch/lyrics): integrate into an mpv extension

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
This commit is contained in:
Profpatsch 2024-09-13 19:12:05 +02:00
parent 7fedfe1cde
commit ffdcb3bb5e
4 changed files with 149 additions and 58 deletions

View file

@ -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
''
]

View file

@ -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;
}

View file

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

View file

@ -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 [