半肾
精华
|
战斗力 鹅
|
回帖 0
注册时间 2020-7-9
|
vibe coding了一下,终于差不多实现了yazi默认打开图像、视频和音频的时候调用mpv能读取当前yazi的实时文件排序。。(发现mpv现在看hdr视频和图像都完全没有问题,体验很不错)
首先创建yazi的插件目录和脚本文件 ~/.config/yazi/plugins/mpv-sort.yazi/main.lua,内容如下
- --- @sync entry
- local function detect_mode(path)
- local ext = path:match("%.([^%.]+)$")
- if not ext then
- return "unknown"
- end
- ext = ext:lower()
- local audio = {
- mp3 = true,
- flac = true,
- wav = true,
- m4a = true,
- ogg = true,
- opus = true,
- }
- local image = {
- jpg = true,
- jpeg = true,
- png = true,
- webp = true,
- bmp = true,
- }
- local video = {
- mp4 = true,
- mkv = true,
- webm = true,
- avi = true,
- mov = true,
- m4v = true,
- wmv = true,
- flv = true,
- ts = true,
- m2ts = true,
- }
- if audio[ext] then
- return "audio"
- end
- if image[ext] then
- return "image"
- end
- if video[ext] then
- return "video"
- end
- return "unknown"
- end
- local function entry()
- local hovered = cx.active.current.hovered
- if not hovered then
- return
- end
- local current = tostring(hovered.url)
- local files = cx.active.current.files
- local mode = detect_mode(current)
- if mode == "unknown" then
- return
- end
- local list = {}
- local start = 0
- for _, f in ipairs(files) do
- local path = tostring(f.url)
- if detect_mode(path) == mode then
- if path == current then
- start = #list
- end
- table.insert(list, path)
- end
- end
- local playlist = "/tmp/yazi-mpv.m3u"
- local fp = io.open(playlist, "w")
- if not fp then
- return
- end
- for _, p in ipairs(list) do
- fp:write(p .. "\n")
- end
- fp:close()
- local cmd = Command("mpv")
- if mode == "audio" then
- cmd:arg("--profile=music")
- elseif mode == "image" then
- cmd:arg("--profile=image")
- end
- cmd:arg("--playlist=" .. playlist)
- cmd:arg("--playlist-start=" .. tostring(start))
- cmd:spawn()
- end
- return { entry = entry }
复制代码
这里我对打开图片和音频有不同的mpv profile配置(视频为默认profile),如要使用这个代码需要自行进行相应修改;另外在yazi.toml里面添加/修改对应的opener和open规则
- [opener]
- mpv-sort = [
- { run = "ya emit plugin mpv-sort", orphan = true, desc = "Play with mpv" },
- ]
复制代码- [open]
- rules = [
- # Image
- { mime = "image/*", use = "mpv-sort" },
- # Video
- { mime = "video/*", use = "mpv-sort" },
- # Audio
- { mime = "audio/*", use = "mpv-sort" },
- ]
复制代码 |
|