diff options
| author | Robert Günzler <r@gnzler.io> | 2024-11-25 13:30:08 +0100 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2024-11-25 13:58:25 +0100 |
| commit | 18e3848110da0a59843058927ea04b496f5db2a5 (patch) | |
| tree | d54e7e1ab025d61507a0b1c3523c08abf400575d /.config/mpv/mvi/scripts/detect-image.lua | |
| parent | 8f29be993dad465c1bb108462dc1bd63706649a5 (diff) | |
update mpv config
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.config/mpv/mvi/scripts/detect-image.lua')
| -rw-r--r-- | .config/mpv/mvi/scripts/detect-image.lua | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/.config/mpv/mvi/scripts/detect-image.lua b/.config/mpv/mvi/scripts/detect-image.lua new file mode 100644 index 0000000..bfe52c2 --- /dev/null +++ b/.config/mpv/mvi/scripts/detect-image.lua @@ -0,0 +1,73 @@ +local opts = { + command_on_first_image_loaded="", + command_on_image_loaded="", + command_on_non_image_loaded="", +} +local options = require 'mp.options' +local msg = require 'mp.msg' + +options.read_options(opts, nil, function() end) + +function run_maybe(str) + if str ~= "" then + mp.command(str) + end +end + +local was_image = false + +function set_image(is_image) + if is_image and not was_image then + msg.info("First image detected") + run_maybe(opts.command_on_first_image_loaded) + end + if is_image then + msg.info("Image detected") + run_maybe(opts.command_on_image_loaded) + end + if not is_image and was_image then + msg.info("Non-image detected") + run_maybe(opts.command_on_non_image_loaded) + end + was_image = is_image +end + +local properties = {} + +function properties_changed() + local dwidth = properties["dwidth"] + local tracks = properties["track-list"] + local path = properties["path"] + local framecount = properties["estimated-frame-count"] + + if not path or path == "" then return end + if not tracks or #tracks == 0 then return end + local audio_tracks = 0 + for _, track in ipairs(tracks) do + if track.type == "audio" then + audio_tracks = audio_tracks + 1 + end + end + + -- only do things when state is consistent + if not framecount and audio_tracks > 0 then + set_image(false) + elseif framecount and dwidth and dwidth > 0 then + -- png have 0 frames, jpg 1 ¯\_(ツ)_/¯ + set_image((framecount == 0 or framecount == 1) and audio_tracks == 0) + end +end + +function observe(propname) + mp.observe_property(propname, "native", function(_, val) + if val ~= properties[propname] then + properties[propname] = val + msg.verbose("Property " .. propname .. " changed") + properties_changed() + end + end) +end +observe("estimated-frame-count") +observe("track-list") +observe("dwidth") +observe("path") |