Skip to content

Instantly share code, notes, and snippets.

@guixxx
Last active December 1, 2019 22:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guixxx/a349d27d271ca100685c3ce5fb1d5c9f to your computer and use it in GitHub Desktop.
Save guixxx/a349d27d271ca100685c3ce5fb1d5c9f to your computer and use it in GitHub Desktop.
(DEPRECATED) Automatically loads lyric (.lrc) files to mpv if they're found
-- NOTE: This script is no longer necessary as of mpv 0.30.0!
loaded = false
function search_and_load_lrc()
local lrc_path = ext2lrc(mp.get_property("path"))
local file = io.open(lrc_path, "r")
if file ~= nil then
io.close(file)
mp.set_property("sub-files", lrc_path)
loaded = true
end
end
function unload_lrc()
if loaded == true then
mp.set_property("sub-files", "")
loaded = false
end
end
function ext2lrc(path)
-- strip the old extension with an empty string, then add the ".lrc" later,
-- otherwise this will fail on files with no extension
local name = path:gsub("(%..+)$", "")
return name .. ".lrc"
end
mp.register_event("start-file", search_and_load_lrc)
mp.register_event("end-file", unload_lrc)
@wis
Copy link

wis commented Apr 4, 2019

damn it's sub-files, sub-file and sub-files-append didn't work..
here's my code:

-- auto load lrc files

function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

function GetLrcPath(name)
  return name:gsub("(%..+)$", ".lrc")
end

function open_handler()
    lrcPath = GetLrcPath(mp.get_property("path"))
    if file_exists(lrcPath) then
        mp.set_property("options/sub-files", lrcPath)
    end
end
mp.register_event("start-file", open_handler)

why are is necessary to set the property to nothing when mpv ends streaming the file?

@guixxx
Copy link
Author

guixxx commented Aug 5, 2019

Sorry for the late response, I didn't get any notifications about comments on this.

why are is necessary to set the property to nothing when mpv ends streaming the file?

Let's say you open multiple files (or a directory) with mpv, if one of those files has .lrc counterpart, this subtitle will be added in all subsequent files. However, my solution is more of a workaround, though. Because while setting the sub-files option to an empty string will make the subtitles disappear, mpv will actually try to load a subtitle file named "" (an empty string), but will fail to do so, displaying an error in the terminal. I don't know what is the correct approach to unload the subtitles, but so far it works.

Thanks for sharing your script!

@guidocella
Copy link

In case you haven't noticed, mpv 0.30 loads .lrc files correctly, so this script is no longer necessary. :)

@guixxx
Copy link
Author

guixxx commented Dec 1, 2019

Thanks for the heads up! I've changed the script title and I've added a note in the script too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment