Skip to content

Commit

Permalink
添加sub-fonts-dir-auto.lua脚本
Browse files Browse the repository at this point in the history
在播放目录下自动指定`sub-fonts-dir`要使用的字体目录fonts以实现加载特定字体目录。**注意**:mpv必须以包含pr mpv-player/mpv#9856 的版本编译方可使用此脚本


Former-commit-id: 91f390a [formerly ba437d2]
Former-commit-id: 5ea8754
Former-commit-id: 00d7e94
  • Loading branch information
dyphire committed Mar 1, 2022
1 parent 3fb3244 commit 3a1bdd0
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
5 changes: 5 additions & 0 deletions manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"whitelist":"blacklist%-extensions%.lua$",
"dest":"~~/scripts"
},
{
"git":"https://github.com/fbriere/mpv-scripts",
"whitelist":"sub%-fonts%-dir%-autos%.lua$",
"dest":"~~/scripts"
},
{
"git":"https://github.com/Ashyni/mpv-scripts",
"whitelist":"dynamic%-crop%.lua$",
Expand Down
1 change: 1 addition & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
| simplebookmark.lua | 高级书签菜单(配置文件 [simplebookmark.conf](../script-opts/simplebookmark.conf));键位绑定均在同名配置文件中 |
| simplehistory.lua | 高级历史菜单,可恢复最后的播放记录并播放(配置文件 [simplehistory.conf](../script-opts/simplehistory.conf));键位绑定均在同名配置文件中 |
| smartcopypaste_II.lua | 高级剪贴菜单,智能复制粘贴视频路径及进度(配置文件 [smartcopypaste_II.conf](../script-opts/smartcopypaste_II.conf));键位绑定均在同名配置文件中 |
| sub-fonts-dir-auto.lua | 在播放目录下自动指定`sub-fonts-dir`要使用的字体目录fonts以实现加载特定字体目录。**注意**:mpv必须以包含pr [mpv-player/#9856](https://github.com/mpv-player/mpv/pull/9856) 的版本编译方可使用此脚本 |
| sub-select.lua | 指定字幕轨道优先级/黑白名单(配置文件 [sub_select.conf](../script-opts/sub_select.conf)[sub-select.json](../script-opts/sub-select.json)|
| thumbnailer*.lua | 缩略图引擎(依赖 [thumbnailer_osc.lua](../scripts/thumbnailer_osc.lua);配置文件 [thumbnailer.conf](../script-opts/thumbnailer.conf)) |
| thumbnailer_osc.lua | 缩略图引擎搭配的 OSC 界面(配置文件 [thumbnailer_osc.conf](../script-opts/thumbnailer_osc.conf)|
Expand Down
124 changes: 124 additions & 0 deletions scripts/sub-fonts-dir-auto.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
--[[
Automatically look for a fonts directory to use with `sub-fonts-dir`.
This mpv Lua script will automatically use the `sub-fonts-dir` option (to
override the default `~~/fonts` location) if it find a `Fonts` directory
alongside the currently playing file. (The name of the directory is
matched case-insensitively.)
**NOTE:** The `sub-fonts-dir` option has been submitted as part of [PR
#9856](https://github.com/mpv-player/mpv/pull/9856). Until it is merged
upstream, you will have to download and compile the [mpv
source](https://github.com/mpv-player/mpv) yourself.
USAGE:
Simply drop this script in your scripts configuration directory (usually
`~/.config/mpv/scripts/`).
REQUIREMENTS:
This script requires a version of mpv that includes the `sub-fonts-dir`
option.
NOTES:
- Any `--sub-fonts-dir` option passed on the command-line will override
this script.
- When going through a playlist, `sub-fonts-dir` will be dynamically
updated for each individual file.
- This script will output some additional information on higher verbosity
levels (`-v`). To increase the verbosity for this script only, use
`--msg-level=sub_fonts_dir_auto=v` (or `=debug` for more output).
AUTHOR:
Frédéric Brière ([email protected])
Licensed under the GNU General Public License, version 2 or later.
--]]


local utils = require 'mp.utils'
local msg = require 'mp.msg'
-- msg.trace() was added in 0.28.0 -- define it ourselves if it's missing
if msg.trace == nil then
msg.trace = function(...) return mp.log("trace", ...) end
end


-- Directory name we are looking for (case-insensitive)
local FONTS_DIR_NAME = "Fonts"
-- Option name that we want to set
local OPTION_NAME = "sub-fonts-dir"
-- Make sure this option is available in this version of mpv
do
local _, err = mp.get_property(OPTION_NAME)
if err then
msg.error(string.format("This version of mpv does not support the %s option", OPTION_NAME))
return
end
end


-- Whether a path is a directory
local function isdir(path)
return utils.readdir(path .. "/.") ~= nil
end

-- Set an option's value for this file, without overriding the command-line
local function set_option(name, value)
if not mp.get_property_bool(string.format("option-info/%s/set-from-commandline", name)) then
msg.verbose(string.format("Setting %s to %q", name, value))
mp.set_property(string.format("file-local-options/%s", name), value)
else
msg.debug(string.format("Option %s was set on command-line -- leaving it as-is", name))
end
end

-- Find a "Fonts" directory under a single path
local function find_fonts_dir(path)
local entries = utils.readdir(path, "dirs")
if entries == nil then
-- mpv will throw an error message soon enough, no need to intervene
return
end
msg.trace(string.format("Iterating over directories under %q", path))
for _, entry in ipairs(entries) do
msg.trace("Candidate directory:", entry)
if entry:lower() == FONTS_DIR_NAME:lower() then
msg.trace("Match found")
return utils.join_path(path, entry)
end
end
msg.trace("No match found")
end

-- "on_load" hook callback for when a file is about to be loaded.
local function on_load()
local path = mp.get_property("path")
if isdir(path) then
msg.debug("Playing 'file' is actually a directory -- skipping")
return
end

local path_dir = utils.split_path(path)
-- Cosmetic nitpicking: That trailing "/" just looks annoying to me
path_dir = path_dir:gsub("(.)/+$", "%1")

msg.debug(string.format("Searching %q for fonts directory", path_dir))
local fonts_dir = find_fonts_dir(path_dir)
if fonts_dir then
msg.debug("Found fonts directory:", fonts_dir)
set_option(OPTION_NAME, fonts_dir)
end
end
mp.add_hook("on_load", 50, on_load)

0 comments on commit 3a1bdd0

Please sign in to comment.