用灵犀claw做个复读机——mpv的lua扩展
wils
Lv.4 核心创作者
上学那会儿,大家或多或少都用过复读机
就是按A标记位置,按B重复播放A到B之间的内容
今天尝试用mpv开源播放器做个类似的复读机
其实就是写个lua脚本
就像wps表格可以用jsa写扩展,mpv播放器可以用lua写扩展
灵犀claw只用pro模式,一次成功
使用时只需要添加--script参数即可
mpv --script=mpv_repeater.lua media1.mp3最后贴上lua脚本
-- mpv_repeater.lua
-- 复读机:按 A 标记起点,按 B 标记终点,在 A↔B 之间循环播放
-- 按 C 取消所有标记,停止循环
--
-- 使用方法:将本文件放入 mpv 的 scripts 目录即可自动加载
local mp = require("mp")
local mark_a = nil -- 起点位置(秒)
local mark_b = nil -- 终点位置(秒)
local looping = false -- 是否处于循环模式
-- 检查当前位置是否已到达/超过标记 B,若是则跳回标记 A
local function on_time_pos_change(name, val)
if not looping or val == nil then
return
end
-- 确保两个标记都已设置,且当前位置 >= 标记 B
if mark_a ~= nil and mark_b ~= nil and val >= mark_b then
mp.set_property_number("time-pos", mark_a)
end
end
-- 注册 time-pos 属性观察,实时检测播放位置
mp.observe_property("time-pos", "number", on_time_pos_change)
-- ====== 按键绑定 ======
-- 按 A:标记起点(位置 1)
mp.add_key_binding("a", "repeater-mark-a", function()
local pos = mp.get_property_number("time-pos")
if pos == nil then
mp.osd_message("复读机:无法获取当前播放位置", 2)
return
end
mark_a = pos
if mark_b ~= nil then
looping = true
mp.osd_message(string.format("复读机:已标记 A=%.1fs B=%.1fs 开始循环", mark_a, mark_b), 3)
else
mp.osd_message(string.format("复读机:标记起点 A=%.1fs(再按 B 设置终点)", mark_a), 2)
end
end)
-- 按 B:标记终点(位置 2)
mp.add_key_binding("b", "repeater-mark-b", function()
local pos = mp.get_property_number("time-pos")
if pos == nil then
mp.osd_message("复读机:无法获取当前播放位置", 2)
return
end
mark_b = pos
if mark_a ~= nil then
looping = true
mp.osd_message(string.format("复读机:已标记 A=%.1fs B=%.1fs 开始循环", mark_a, mark_b), 3)
else
mp.osd_message(string.format("复读机:标记终点 B=%.1fs(再按 A 设置起点)", mark_b), 2)
end
end)
-- 按 C:取消所有标记,停止循环
mp.add_key_binding("c", "repeater-cancel", function()
mark_a = nil
mark_b = nil
looping = false
mp.osd_message("复读机:已取消所有标记,停止循环", 2)
end)
-- 文件停止/重新加载时自动清理标记
mp.register_event("end-file", function()
mark_a = nil
mark_b = nil
looping = false
end)
Lv.4 核心创作者
Lv.3 优质创作者
Lv.2潜力创作者
Lv.4 核心创作者