Skip to content

Block seek when jump amount inferior to remaining time (avoid EOF when playing) #756

Description

@Rochkiller

Expected behavior of the wanted feature

Hi,

I don't know if my request would be clear, but I'll try anyway.
I'm not a dev, I wrote somes scripts from scratch but never success to implement some features I want (ex: scale with geometry to keep window in screen instead of buildin scale that offscreen a portion of the window)
Then I finally lost to IA a few days ago and build all the ideas I had with a few prompts.

FYI, I those values for seek : jump_amount = 5 / jump_more_amount = 30 / idle=no / keep-open=no
I wanted to add a safe seek function to avoid an EOF when remaining playing time is inferior to jump_amount / jump_more_amount :

  • 10s → seek 5s → 5s
  • 5s → seek 5s → 0s (file still open when pause / file closed when playing)
  • 4s → seek 5s → BLOCKED
  • 1s → seek 5s → BLOCKED

Same with jump_more_amount :

  • 60s → seek 30s → 30s
  • 59s → seek 30s → 29s
  • 31s → seek 30s → 1s
  • 30s → seek 30s → 0s (same here)
  • 29s → seek 30s → BLOCKED
  • 5s → seek 30s → BLOCKED

So here the code I got :

local function safe_seek(offset, limit)
	local pos = mp.get_property_number("time-pos")
	local duration = mp.get_property_number("duration")

	if not pos or not duration then
		return
	end

	local remaining = duration - pos
	local target = pos + offset

	-- Blocks if close to end
	if offset > 0 and remaining <= limit then
		mp.osd_message("Seek bloqué (EOF protection)")
		return
	end

	-- Backward protection
	if target < 0 then
		target = 0
	end

	mp.commandv("seek", target, "absolute", "exact")
end

--jump_backward
ne = new_element("jump_backward", "button")
ne.softrepeat = user_opts.jump_softrepeat
ne.content = jump_icon[1]
ne.eventresponder["mbtn_left_down"] = function () safe_seek(-jump_amount, 5) end
ne.eventresponder["mbtn_right_down"] = function () safe_seek(-jump_more_amount, 30) end
ne.eventresponder["shift+mbtn_left_down"] = function () mp.commandv("frame-back-step") end

--jump_forward
ne = new_element("jump_forward", "button")
ne.softrepeat = user_opts.jump_softrepeat
ne.content = jump_icon[2]
ne.eventresponder["mbtn_left_down"] = function () safe_seek(jump_amount, 5) end
ne.eventresponder["mbtn_right_down"] = function () safe_seek(jump_more_amount, 30) end
ne.eventresponder["shift+mbtn_left_down"] = function () mp.commandv("frame-step") end

This works fine on local files.
Is it possible to add a similar feature with a better code which support network streams ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions