Skip to content

Instantly share code, notes, and snippets.

@olejorgenb
Last active July 2, 2023 02:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olejorgenb/a5194d9bc183dbe0bfb02aac18fe37f9 to your computer and use it in GitHub Desktop.
Save olejorgenb/a5194d9bc183dbe0bfb02aac18fe37f9 to your computer and use it in GitHub Desktop.
mpv user script: copy a command that will resume video at current position
@Justsoos
Copy link

Justsoos commented Jan 16, 2020

-- for MacOS

function copyPermalink()
  local pos = mp.get_property_number("time-pos")
  local uri = mp.get_property("path")
  local bookmark = string.format("mpv --start=%s %q", pos, uri)
  local pipe = io.popen("pbcopy", "w")
  pipe:write(bookmark)
  pipe:close()
  mp.osd_message("Link to position copied to clipboard")
end

mp.add_key_binding("ctrl+SPACE", "copy-permalink", copyPermalink)

@sigboe
Copy link

sigboe commented Jan 23, 2020

Hei Ole, if you want to add a sharable link function to your script, I did it like below.
This adds a shift+space key binding that copies a youtube link with the time stamp in the link.

-- Author: Ole Jørgen Brønner (olejorgenb@yahoo.no)
-- Requirement: xclip
-- Installation: 
-- 'mkdir -p ~/.config/mpv/scripts && cp -i copy-permalink.lua ~/.config/mpv/scripts'

function copyPermalink()
  local pos = mp.get_property_number("time-pos")
  local uri = mp.get_property("path")
  -- %q might not be fully robust
  local bookmark = string.format("mpv --start=%s %q", pos, uri)
  local pipe = io.popen("xclip -silent -in -selection clipboard", "w")
  pipe:write(bookmark)
  pipe:close()
  mp.osd_message("Link to position copied to clipboard")
end

function sharePermalink()
  local pos = mp.get_property_number("time-pos")
  local uri = mp.get_property("path")
  -- %q might not be fully robust
  -- local bookmark = string.format("mpv --start=%s %q", pos, uri)
  local bookmark = string.format("%s&t=%i", uri, pos)
  local pipe = io.popen("xclip -silent -in -selection clipboard", "w")
  pipe:write(bookmark)
  pipe:close()
  mp.osd_message("Shareable link with position copied to clipboard")
end

-- mp.register_script_message("copy-permalink", copyPermalink)
mp.add_key_binding("ctrl+SPACE", "copy-permalink", copyPermalink)
mp.add_key_binding("shift+SPACE", "share-permalink", sharePermalink)

@phanirithvij
Copy link

xclip alternative on Windows anyone?

@sigboe
Copy link

sigboe commented Mar 10, 2020

@phanirithvij

xclip alternative on Windows anyone?

https://stackoverflow.com/a/11546439/1701567
first result in google

@elig0n
Copy link

elig0n commented Mar 28, 2020

xclip alternative on Windows anyone?

Here's a Powershell alternative off copyTime.lua:

require 'mp'

local function set_clipboard(text)
    mp.commandv("run", "powershell", "set-clipboard", text);
end

@sergeevabc
Copy link

Full working script is provided for Linux by @olejorgenb and for Mac OS by @Justsoos, whereas Windows version is still in DIY state. We need a hero here.

@elig0n
Copy link

elig0n commented Jun 18, 2020

@sergeevabc please take a look at my script , my addition from above is working!

@sergeevabc
Copy link

@elig0n, it does not work for me on Windows 7. Placed your copy-permalink.lua to mpv/scripts/, then opened a video from Youtube, pressed ctrl+space, got on-screen message “… copied to clipboard", but there clipboard is still empty, alas.

@elig0n
Copy link

elig0n commented Jun 18, 2020

@sergeevabc could you please try again now ?

@sergeevabc
Copy link

@elig0n, still does not work.

@NothingSpecialOne
Copy link

We need a hero here.

Here i am, @sergeevabc
You have to add just a single line into input.conf file, to make the magic happen
Shift+c run "cmd.exe" "/d" "/c" "echo mpv --start=${time-pos} ${path}|clip"
That's it, no scripts have been used
And the cherry on the cake - youtube timestamp link
Ctrl+c run "cmd.exe" "/d" "/c" "echo ${path}^^^&t=${=time-pos}|clip"
but it sux, because of milliseconds at the timestamp (416.280000 for example), and link would not work properly until you delete them (416 is what we need, in this case) manually, also link ends with new line (\n) symbol
I have no idea - is this even possible to get seconds only, from current playback time, or needs a feature request, probably
Anyway, the trick is - NO scripts
Windows only solution

@sergeevabc
Copy link

@NothingSpecialOne, thank you! It works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment