summaryrefslogtreecommitdiff
path: root/lua/bookmarker.lua
blob: f95a4c0e45e5f7fd97ddbe9fa5a6fd1cb2d5513e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
-- DEBUGGING
--
-- Debug messages will be printed to stdout with mpv command line option
-- `--msg-level='bookmarker=debug'`

local utils = require('mp.utils')
local msg = require('mp.msg')

local latest_loaded_bookmark = -1
local latest_saved_bookmark = -1
local latest_saved_bookmark_data_before = nil

--// seconds to hh:mm:ss
function displayTime(time)
  local hours = math.floor(time / 3600)
  local minutes = math.floor((time % 3600) / 60)
  local seconds = math.floor((time % 60))
  return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

--// Extract filename/immediate-dir from url
function GetFileName(url)
  return url:match("^.+/(.+)$")
end
function GetImmediateDirectoryName(url)
  return url:match("^.*/([^/]+)/[^/]+$")
end
function GetDirectory(url)
  return url:match("^(.*)/[^/]+$")
end
function GetHostName(url)
  return url:match("^http[s]?://([^/]+).*$")
end
function GetUrlPath(url)
  return url:match("^http[s]?://[^/]+[/]?(.*)$")
end

--// Save/Load string serializer function
function exportstring(s)
  return string.format("%q", s)
end

--// Save a table as json to a file
function saveTable(t, path)
  -- a simple machanism to make it transactional
  local contents = utils.format_json(t)
  local file = io.open(path .. ".tmp", "wb")
  file:write(contents)
  io.close(file)
  os.remove(path)
  os.rename(path .. ".tmp", path)
  msg.debug("[persistence]", "bookmark file successfully saved.")
  return true
end

--// Load a table from a json-file
function loadTable(path)
  function tableLength(T)
    local count = 0
    for _ in pairs(T) do count = count + 1 end
    return count
  end
  local myTable = {}
  local file = io.open(path, "r")
  if file then
    local contents = file:read("*a")
    io.close(file)
    local length = string.len(contents)
    msg.debug("[persistence]", "bookmark file successfully loaded. length: " .. length)
    if length == 0 then
      contents = "{}"
    end
    myTable = utils.parse_json(contents);
    if not myTable then
      error("Corrupt bookmark file '" .. path .. "', please remove it! bookmarker will automatically create a new file.")
    end
    msg.debug("[persistence]", tableLength(myTable) .. " slots found.")
    return myTable
  end
  msg.warn("[persistence]", "could not load bookmark file!")
  return nil
end

--// check if it's a url/stream
function is_url(path)
  if path ~= nil and string.sub(path, 1, 4) == "http" then
    msg.debug("[path/url]", "detected as stream: '" .. path .. "'")
    return true
  else
    return false
  end
end

--// check whether a file exists or not
function file_exists(path)
  if is_url(path) then
    return true
  else
    local f = io.open(path, "r")
    if f ~= nil then
      io.close(f)
      return true
    else
      msg.debug("[path/url]", "'" .. path .. "' did not exist.")
      return false
    end
  end
end

--// check if macos
function is_macos()
  local homedir = os.getenv("HOME")
  if homedir ~= nil and string.sub(homedir, 1, 6) == "/Users" then
    msg.debug("[os/detector]", "macOS detected.")
    return true
  else
    return false
  end
end

--// check if windows
function is_windows()
  local windir = os.getenv("windir")
  if windir ~= nil then
    msg.debug("[os/detector]", "windows detected.")
    return true
  else
    return false
  end
end

function platform_independent(filepath)
  return filepath -- // see "shared-bookmarks-different-os.md" to see utility of this function
end

--// default file to save/load bookmarks to/from
function getConfigFile()
  local path = ''
  if is_windows() then
    path = os.getenv("APPDATA"):gsub("\\", "/") .. "/mpv/bookmarks.json"
  else
    path = os.getenv("HOME") .. "/.config/mpv/bookmarks.json"
  end
  msg.debug("[persistence]", "config file is set to '" .. path .. "'.")
  return path
end

--// print current bookmark object
function printBookmarkInfo(bookmark)
  if bookmark ~= nil then
    local path = bookmark["filepath"] or "UNDEFINED PATH"
    local dirname = "UNDEFINED DIRECTORY"
    local name = "UNDEFINED FILENAME"
    if is_url(path) then
      dirname = GetHostName(path) or "INVALID URL"
      name = GetUrlPath(path) or "INVALID URL"
    else
      dirname = GetImmediateDirectoryName(path) or "INVALID DIRECTORY"
      name = GetFileName(path) or "INVALID FILENAME"
      name = name:gsub("_", " ")
    end
    local existance = (file_exists(path) and "") or "[!!] "
    local pos = bookmark["pos"] or "0"
    local title = bookmark["title"]
    if title ~= nil and title ~= "" and title ~= name then
      title = "\n" .. title
    else
      title = ""
    end
    return existance .. dirname .. "\n" .. existance .. name .. title .. "\n" .. displayTime(tonumber(pos))
  else
    return "Undefined"
  end
end

function fetchBookmark(slot)
  local bookmarks = loadTable(getConfigFile())
  if bookmarks == nil then
    mp.osd_message("Error loading bookmarks.json")
    return
  end
  local bookmark = bookmarks[slot]
  if bookmark == nil then
    msg.debug("[persistence]", "slot[" .. slot .. "] is empty!")
    return
  end
  bookmark["pos"] = math.max(bookmark["pos"] or 0, 0)
  bookmark["filepath"] = platform_independent(bookmark["filepath"])
  msg.debug("[persistence]", "slot[" .. slot .. "] loaded: " .. utils.format_json(bookmark))
  return bookmark
end

--// save current file/pos to a bookmark object
function currentPositionAsBookmark()
  local bookmark = {}
  local isLiveStream = mp.get_property("duration") == 0
  if isLiveStream then
    bookmark["pos"] = nil
  else
    bookmark["pos"] = mp.get_property_number("time-pos")
  end
  bookmark["filepath"] = mp.get_property("path")
  bookmark["filename"] = mp.get_property("filename")
  bookmark["title"] = mp.get_property("media-title")
  msg.debug("[interface]", "bookmark to be saved: { " .. utils.format_json(bookmark) .. " }")
  return bookmark
end

--// play to a bookmark
function bookmarkToCurrentPosition(bookmark, firstStep)
  if firstStep then
    msg.debug("[interface]", "bookmark to be loaded: { " .. utils.format_json(bookmark) .. " }")
  end
  if mp.get_property("path") == bookmark["filepath"] then
    if firstStep then
      msg.debug("[interface]", "file is already loaded.")
    end
    msg.debug("[interface]", "setting position to: " .. bookmark["pos"])
    -- if current media is the same as bookmark media
    mp.set_property_number("time-pos", bookmark["pos"])
    return
  elseif firstStep == true then
    msg.debug("[interface]", "setting path to: '" .. bookmark["filepath"] .. "'")
    mp.commandv("loadfile", bookmark["filepath"], "replace")
    if bookmark["pos"] ~= nil then
      local seekerFunc = {}
      seekerFunc.fn = function()
        mp.unregister_event(seekerFunc.fn);
        bookmarkToCurrentPosition(bookmark, false)
      end
      mp.register_event("playback-restart", seekerFunc.fn)
      msg.debug("[interface]", "waiting for file/url to load.")
    end
  else
    msg.debug("[interface]", "setting the position is cancelled as the path is not loaded.")
  end
end

--// get latest bookmark if it relates to current file (if they point to files that are in the same directory)
function find_current_bookmark_slot()
  if latest_loaded_bookmark ~= -1 then
    local bookmark = fetchBookmark(latest_loaded_bookmark)
    current_file = mp.get_property("path")
    if bookmark ~= nil and current_file ~= nil then
      if GetDirectory(platform_independent(bookmark["filepath"])) == GetDirectory(platform_independent(current_file)) then
        msg.debug("[interface]", "Current bookmark slot detected as: " .. latest_loaded_bookmark)
        return latest_loaded_bookmark
      end
      msg.debug("[interface]", "Lastest loaded bookmark slot was " .. latest_loaded_bookmark .. " but the path has been changed.")
    end
  end
  msg.debug("[interface]", "No bookmark has been loaded yet.")
  return nil
end

--// handle "bookmark-set" function triggered by a key in "input.conf"
function bookmark_save(slot)
  msg.debug("[interface]", "received 'bookmark-set(" .. slot .. ")' script message.")
  local bookmarks = loadTable(getConfigFile())
  if bookmarks == nil then
    bookmarks = {}
  end
  latest_saved_bookmark_data_before = bookmarks[slot]
  bookmarks[slot] = currentPositionAsBookmark()
  local result = saveTable(bookmarks, getConfigFile())
  if result ~= true then
    mp.osd_message("Error saving: " .. result)
  end
  latest_loaded_bookmark = slot
  mp.osd_message("Bookmark#" .. slot .. " saved.")
end
mp.register_script_message("bookmark-set", bookmark_save)

--// Save a table as json to a file
function bookmark_save_undo()
  msg.debug("[interface]", "received 'bookmark-set-undo' script message.")
  if latest_saved_bookmark ~= -1 and latest_saved_bookmark_data_before ~= nil then
    local bookmarks = loadTable(getConfigFile())
    bookmarks[latest_saved_bookmark] = latest_saved_bookmark_data_before
    local result = saveTable(bookmarks, getConfigFile())
    if result ~= true then
      mp.osd_message("Error undoing: " .. result)
    end
    mp.osd_message("Bookmark#" .. latest_saved_bookmark .. " set back to: \n" .. printBookmarkInfo(latest_saved_bookmark_data_before))
    latest_saved_bookmark = -1
    latest_saved_bookmark_data_before = nil
  end
end
mp.register_script_message("bookmark-set-undo", bookmark_save_undo)

--// handle "bookmark-update" function triggered by a key in "input.conf" | basically updates latest saved/loaded bookmark if current file is with in the same directory
function last_bookmark_update()
  msg.debug("[interface]", "received 'bookmark-update' script message.")
  slot_to_be_saved = find_current_bookmark_slot()
  if slot_to_be_saved ~= nil then
    bookmark_save(slot_to_be_saved)
  end
end
mp.register_script_message("bookmark-update", last_bookmark_update)

--// handle "bookmark-load" function triggered by a key in "input.conf"
function bookmark_load(slot)
  msg.debug("[interface]", "received bookmark-load(" .. slot .. ") script message.")
  local bookmark = fetchBookmark(slot)
  if bookmark == nil then
    mp.osd_message("Bookmark#" .. slot .. " is not set.")
    return
  end
  if file_exists(bookmark["filepath"]) == false then
    mp.osd_message("File " .. bookmark["filepath"] .. " not found!")
    return
  end
  bookmarkToCurrentPosition(bookmark, true)
  latest_loaded_bookmark = slot
  mp.osd_message("Bookmark#" .. slot .. " loaded\n" .. printBookmarkInfo(bookmark))
end
mp.register_script_message("bookmark-load", bookmark_load)

--// handle "bookmark-peek" function triggered by a key in "input.conf"
function bookmark_peek(slot)
  msg.debug("[interface]", "received 'bookmark-peek(" .. slot .. ")' script message.")
  local bookmark = fetchBookmark(slot)
  if bookmark == nil then
    mp.osd_message("Bookmark#" .. slot .. " is not set.")
    return
  end
  mp.osd_message("Bookmark#" .. slot .. " :\n" .. printBookmarkInfo(bookmark))

end
mp.register_script_message("bookmark-peek", bookmark_peek)

--// handle "bookmark-peek-current" function triggered by a key in "input.conf" | basically peeks at latest saved/loaded bookmark if current file is with in the same directory
function current_bookmark_peek()
  msg.debug("[interface]", "received 'bookmark-peek-current' script message.")
  slot_to_be_saved = find_current_bookmark_slot()
  if slot_to_be_saved ~= nil then
    bookmark_peek(slot_to_be_saved)
  end
end
mp.register_script_message("bookmark-peek-current", current_bookmark_peek)

--// save current file/pos to a bookmark object without displaying any message
function auto_save_bookmark(slot)
  msg.debug("[autosave]", "autosaving to slot " .. slot)
  local bookmarks = loadTable(getConfigFile())
  if bookmarks == nil then
    bookmarks = {}
  end
  bookmarks[slot] = currentPositionAsBookmark()
  local result = saveTable(bookmarks, getConfigFile())
  if result ~= true then
    msg.warn("Error autosaving: " .. result)
  end
  latest_loaded_bookmark = slot
end

-- (existing code)

--// handle "bookmark-update" function triggered by a key in "input.conf" | basically updates latest saved/loaded bookmark if current file is with in the same directory
function last_bookmark_update()
  msg.debug("[interface]", "received 'bookmark-update' script message.")
  slot_to_be_saved = find_current_bookmark_slot()
  if slot_to_be_saved ~= nil then
    bookmark_save(slot_to_be_saved)
  end
end
mp.register_script_message("bookmark-update", last_bookmark_update)

-- (existing code)

-- Set up a timer to call the auto_save_bookmark function every 5 seconds
local autosave_timer = mp.add_periodic_timer(5, function()
  -- Check if the video is playing
  local is_playing = mp.get_property("core-idle") == "no"
  if is_playing then
    local slot_to_be_autosaved = find_current_bookmark_slot()
    if slot_to_be_autosaved ~= nil then
      auto_save_bookmark(slot_to_be_autosaved)
    end
  end
end)
-- Get the value of bookmarker-start-bookmark option
local options = require("mp.options")
local script_opts = {
    start_bookmark = -1
}
options.read_options(script_opts, "bookmarker")

-- If a valid bookmark number is provided, load it when the script starts
if script_opts.start_bookmark and tonumber(script_opts.start_bookmark) > 0 then
    local initial_bookmark = tonumber(script_opts.start_bookmark)
    mp.add_timeout(0.5, function()
        mp.commandv("script_message", "bookmark-load", tostring(initial_bookmark))
    end)
end