Skip to content

Instantly share code, notes, and snippets.

@cobrce
Created April 26, 2020 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cobrce/7d6251783f9a0673102ee743c6a33364 to your computer and use it in GitHub Desktop.
Save cobrce/7d6251783f9a0673102ee743c6a33364 to your computer and use it in GitHub Desktop.
script for mpv player to skip automatically parts of a media file
[[--
this is a script for mpv player, it's purpose is to skip automatically parts of a media file
how does it work:
- install this script in mpv scripts directory
- let's say you wan't to skip from position 300s to 310s and from 5001 to 5020 in a file called movie.mp4,
- in it's directory create a file called movie.txt that contains the following lines
300,310
5001,5020
- open movie.mp4 in mpv
- enjoy
hint : this script also provides an easy way to get current time in seconds, press "c" and it show top left of the screen
--]]
positions = {}
local function load_table()
timer:resume()
positions = {}
local path = mp.get_property("path"):match("(.+)%..+")..".txt"
local file =io.open(path,"r")
if file then
io.close(file)
for line in io.lines(path) do
b,e = line:match("(.+)%,(.+)")
table.insert(positions,{tonumber(b),tonumber(e)})
end
end
end
local function get_time()
return tonumber(mp.get_property("time-pos"))
end
local function check()
now = get_time()
for i,pair in ipairs(positions) do
if now >= pair[1] and now < pair[2] then
mp.set_property("time-pos",pair[2])
end
end
end
local function pause_timer()
timer:stop()
end
local function show_time()
mp.osd_message(string.format("%d",get_time()))
end
timer = mp.add_periodic_timer(1, check)
mp.register_event("seek",check)
mp.register_event("file-loaded", load_table)
mp.register_event("end-file",pause_timer)
mp.add_key_binding("c", "show_time", show_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment