summary refs log tree commit diff
path: root/.config/mpv/mvi/scripts/detect-image.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2026-04-08 13:26:13 +0900
committerRobert Günzler <r@gnzler.io>2026-04-08 13:26:13 +0900
commit4e34cbf9673aeaba252e01df554a0d204e83c5e2 (patch)
treef3eea07d0e2bf1628ce19987c5164577f63ac614 /.config/mpv/mvi/scripts/detect-image.lua
parent28db7c76e26c312918959384e02e433f8858de49 (diff)
Automated-commit-by: dots
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.lua73
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")