about summary refs log tree commit diff
path: root/users/Profpatsch
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2024-10-01T00·44+0200
committerProfpatsch <mail@profpatsch.de>2024-10-05T13·49+0000
commitc014e39dfd35915fc19ba28e4c170774d1aad5c0 (patch)
tree3a018d37e91a7ed8860c2993ea05fd68c4efe861 /users/Profpatsch
parent686b141767ebd7d4dcb817766b058e5ba01cb7e1 (diff)
feat(users/Profpatsch/lyric): add .lrc header for new files r/8764
Insert the length and stuff into the .lrc file headers.

Change-Id: Id2565c95c516208f1e46b79d5b8da50f3d6bee62
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12552
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch')
-rw-r--r--users/Profpatsch/lyric/lyric-timing-mpv-script.lua29
1 files changed, 27 insertions, 2 deletions
diff --git a/users/Profpatsch/lyric/lyric-timing-mpv-script.lua b/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
index 6558422f1092..db28c0a24f95 100644
--- a/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
+++ b/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
@@ -16,18 +16,43 @@ function write_timestamp_to_lrc()
         return
     end
 
-    -- Extract metadata for artist and title
+    -- Extract metadata for artist, title, and album
     local artist = mp.get_property("metadata/by-key/ARTIST", "Unknown Artist")
     local title = mp.get_property("metadata/by-key/TITLE", "Unknown Title")
+    local album = mp.get_property("metadata/by-key/ALBUM", "Unknown Album")
 
     -- Construct the lrc dir
     local dir = cache_dir .. "/lyric/timed"
-    local lrc_filename = string.format("%s/%s - %s.lrc", dir, artist, title)
+    local lrc_filename = string.format("%s/%s - %s - %s.lrc", dir, artist, album, title)
 
     -- Get current playback time
     local current_time = mp.get_property_number("time-pos", 0)
     local formatted_time = format_timestamp(current_time)
 
+    -- If the file does not exist, or is empty, create it and add metadata in the following format:
+    -- [ar: Chubby Checker oppure  Beatles, The]
+    -- [al: Hits Of The 60's - Vol. 2 – Oldies]
+    -- [ti: Let's Twist Again]
+    -- [au: Written by Kal Mann / Dave Appell, 1961]
+    -- [length: 2:23]
+    local file = io.open(lrc_filename, "r+")
+    if file then
+        -- read the file and check whether it only contains whitespace
+        local content = file:read("*all")
+        if content:match("^%s*$") then
+            file:write("[ar: " .. artist .. "]\n")
+            file:write("[al: " .. album .. "]\n")
+            file:write("[ti: " .. title .. "]\n")
+            local duration = mp.get_property_number("duration", 0)
+            local formatted_duration = string.format("%02d:%02d", math.floor(duration / 60), duration % 60)
+            file:write("[length: " .. formatted_duration .. "]\n")
+            file:write("\n")
+        end
+        file:close()
+    else
+        mp.msg.error("Failed to open " .. lrc_filename)
+    end
+
     -- Append the timestamp to the LRC file
     local file = io.open(lrc_filename, "a")
     if file then