po5 / mpv_sponsorblock

mpv script to skip sponsored segments of YouTube videos
GNU General Public License v3.0
530 stars 29 forks source link

Legacy script tries to require script from current directory - mpv 0.32 #12

Closed marty-oehme closed 4 years ago

marty-oehme commented 4 years ago

When opening mpv version 0.32, the following error is generated:

sponsorblock_legacy: stack traceback:
sponsorblock_legacy:    [C]: in function 'require'
sponsorblock_legacy:    /home/marty/.config/mpv/scripts/sponsorblock_legacy.lua:2: in main chunk
sponsorblock_legacy:    [C]: in ?
sponsorblock_legacy:    [C]: in ?
sponsorblock_legacy: Lua error: /home/marty/.config/mpv/scripts/sponsorblock_legacy.lua:2: module 'sponsorblock/main' not found:
sponsorblock_legacy:    no field package.preload['sponsorblock/main']
sponsorblock_legacy:    no file '/usr/share/lua/5.2/sponsorblock/main.lua'
sponsorblock_legacy:    no file '/usr/share/lua/5.2/sponsorblock/main/init.lua'
sponsorblock_legacy:    no file '/usr/lib/lua/5.2/sponsorblock/main.lua'
sponsorblock_legacy:    no file '/usr/lib/lua/5.2/sponsorblock/main/init.lua'
sponsorblock_legacy:    no file './sponsorblock/main.lua'
sponsorblock_legacy:    no file '/usr/lib/lua/5.2/sponsorblock/main.so'
sponsorblock_legacy:    no file '/usr/lib/lua/5.2/loadall.so'
sponsorblock_legacy:    no file './sponsorblock/main.so'

This looks like ./sponsorblock/main.lua could not be found, thus not required correctly. The folder structure in scripts looks as follows

. ~/.config/mpv/scripts/
├── sponsorblock_legacy.lua
├── sponsorblock/
│  ├── main.lua
│  └── shared
│     ├── sponsorblock.py
│     └── ...

As far as I know, this should be the correct directory structure. I am running mpv on linux. It appears that the root folder for lua scripts is set to whatever directory mpv is called from, which means it always attempts to source ./sponsorblock/main.lua from wherever it is called.

Feel free to ignore this report as it only affects the legacy workaround anyways, and for the time being either:

  1. aliasing mpv to be called from the scripts directory or
  2. putting the sponsorblock script into one of the absolute paths looked for by lua work.

But I struggled with this for a little while and thought it worth highlighting. Anyways, thank you so much for the work of integrating sponsorblock with mpv -- when using one of the workarounds, it works like a charm!

Edit:

Actually, just a quick edit -- changing the legacy script to substitute its package path to the one that the sponsorblock_legacy.lua file resides in also works (though it's a bit hacky, and probably only works with unix directories):

if mp.get_script_directory == nil then
    local scriptpath = debug.getinfo(1).short_src:match('.*/')
    local originalpath = package.path
    package.path = scriptpath .. "?.lua"

    require 'sponsorblock/main'

    package.path = originalpath
end
po5 commented 4 years ago

Thanks for bringing this to my attention, I've implemented a cleaner and hopefully more robust solution.

marty-oehme commented 4 years ago

Works perfectly, thank you!