mpv-player / mpv-player/mpv

New command line option: skip to time on opening file only

Open
#16,532 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

meta:feature-request
Dominant language
C
Stars
37k
Forks
3.5k
Avg merge
1d 10h
Merged PRs (30d)
22

Description

Expected behavior of the wanted feature

When watching a bunch of videos in a directory, I would like to be able to open a new instance of mpv, so it plays the current file, at the current time.

Using existing command line arguments, it is possible to skip to a time in that new instance of the player, but then subsequently all other files that I skip to in that new instance will also start at the provided time, which is not desirable.

I have tried putting together a script using pipes, but it is unreliable, as mpv does not immediately respond to pipe input, and ignores some of the initial commands, until some random time that is dependent on system load and uptime. Additionally, it pops up a terminal window to scan for pipes, and sometimes that terminal window doesn't close on its own.

Alternative behavior of the wanted feature

No response

Log File

No response

Sample Files

This script only works sporadically:

-- Reopens the same media file in a new player, at the same timestamp

-- To use this script, first put it in your mpv config directory, in the scripts subdirectory.
-- Then, put this in input.conf to use it:
-- Ctrl+x script-message reopen-at-timestamp
-- You can use other key bindings of course

-- Requires SysInternals PipeList to be installed in:
-- C:\Programs\PipeList\pipelist.exe
-- Get it at:
-- https://learn.microsoft.com/en-us/sysinternals/downloads/pipelist


local dbg = false

local function dbgprint(s)
  if dbg then
    print(s)
  end
end


local function file_exists(name)
  local f=io.open(name,"r")
  if f~=nil then
    io.close(f)
    return true
  else
    return false
  end
end


function string:contains(sub)
  return self:find(sub, 1, true) ~= nil
end

local function sleep(a) 
  local sec = tonumber(os.clock() + a); 
  while (os.clock() < sec) do 
  end 
end

local function do_ipc(pipe, command)
  dbgprint("Doing IPC...")
  local ipc = io.open(pipe, "w")
  ipc:write(command)
  ipc:flush()
  ipc:close()
end

local function reopen_at_timestamp()
  local pos = mp.get_property_native("time-pos")
  local rnd = math.random(1, 1000000000)
  local path = mp.get_property("path")
  dbgprint(path)
  local pipename = string.format("mpvpipe_%d", rnd)
  local pipe = string.format("\\\\.\\pipe\\%s", pipename) -- backslashes need to be escaped.
  local ipcarg = string.format("--input-ipc-server=%s", pipe)
  dbgprint(ipcarg)
  mp.commandv("run", "mpv", ipcarg, path)
  
  -- Wait for socket to start existing
  local timeout = 3 -- max time to wait in seconds
  local deadline = tonumber(os.clock() + timeout)
  local found = false
  while (os.clock() < deadline) do
    dbgprint(string.format("deadline and os clock: %f %f", deadline, os.clock()))
    if found then
      break
    end
    -- The below is commented out because it doesn't work.
    -- As it turns out, the pipe file existing does not ensure that mpv is receiving commands.
    -- if file_exists(pipe) then
    --   dbgprint("FOUND!!!")
    --   dbgprint("pipe:")
    --   dbgprint(pipe)
    --   found = true
    --   break
    -- end
    -- This seems to work more often:
    for dir in io.popen('C:\\Programs\\PipeList\\pipelist.exe -h'):lines() do
      if dir:contains(pipename) then
        dbgprint(dir)
        found = true
        break
      end
    end
    sleep(0.01)
  end
  

  if found then
    local command = string.format('{ "command": [ "seek", %d, "absolute" ] }\n', pos)
    do_ipc(pipe, command)
  end
end

mp.register_script_message("reopen-at-timestamp", reopen_at_timestamp)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing mpv's existing command-line time-seeking behavior and the script entry point reopen_at_timestamp shown in the issue. Determine where an opening-only seek could be represented, then verify that the initial file starts at the requested time while subsequently opened files are unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, lua
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.