Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mp = require 'mp'
- local msg = require 'mp.msg'
- -- path to csv file
- local csv_filename = 'sheet.csv'
- local csv_entry_separator = '\n' -- change it to ',' if you want all the entries in a single line
- local allow_repeats = false
- local ignore_seeked = false
- -- this function returns csv entry that will be written to file
- -- edit this function to your liking
- --
- -- i wrapped it in a function, so that any additional logic will not fuck up anything else
- --
- -- reference:
- -- https://mpv.io/manual/master/#command-interface-filename
- -- https://mpv.io/manual/master/#command-interface-path
- function get_csv_entry()
- -- reference: https://mpv.io/manual/master/#properties
- local filename = mp.get_property('filename')
- local filesize = mp.get_property('file-size')
- filename = filename:gsub('%,', '.') -- replace all ',' with '.' in filename to avoid csv conflicts
- csv_entry = filename .. ',' .. filesize
- -- could be useful for debugging
- -- mp.osd_message('csv_entry: ' .. tostring(csv_entry))
- return csv_entry
- end
- -- this function determines if the entry should be written to the csv file
- -- it takes get_csv_entry result as an argument, feel free to use (or completely ignore) it
- function check_file(csv_entry)
- if ignore_seeked and seek_flag then
- return false
- end
- local percent_pos = mp.get_property('percent-pos')
- if not percent_pos then
- return false
- end
- while percent_pos and tonumber(percent_pos) < 80 do
- sleep(5)
- percent_pos = mp.get_property('percent-pos')
- -- mp.osd_message('loop')
- end
- -- mp.osd_message('exit loop')
- if not percent_pos then
- return false
- end
- return true
- end
- -- ------------------------------------------------------------------------------------------------------------
- local seek_flag = false -- don't touch this one
- function detect_platform()
- local o = {}
- -- Kind of a dumb way of detecting the platform but whatever
- if mp.get_property_native('options/vo-mmcss-profile', o) ~= o then
- return 'windows'
- elseif mp.get_property_native('options/input-app-events', o) ~= o then
- return 'macos'
- end
- return 'linux'
- end
- function sleep(seconds)
- local platform = detect_platform()
- if(platform == 'windows') then
- os.execute("ping -n " .. tonumber(seconds+1) .. " localhost > NUL")
- else
- os.execute("sleep " .. tonumber(seconds))
- end
- end
- function on_file_loaded()
- seek_flag = false
- local csv_entry = get_csv_entry()
- if not csv_entry or not check_file(csv_entry) then
- -- could be useful for debugging
- -- mp.osd_message('File didn\'t pass verification')
- return -- do nothing if the file verification didn't pass
- end
- -- uncomment for debugging
- -- mp.osd_message('File verification passed')
- local file_contents = {}
- local is_repeat = false
- -- read current file contents if file exists
- local csv_file = io.open(csv_filename, 'r')
- if csv_file then
- for line in csv_file:lines() do
- if not is_repeat and line == csv_entry then
- is_repeat = true
- end
- table.insert(file_contents, line)
- end
- csv_file:close()
- end
- -- append new entry at the end of the file (unless it's a repeat and repeats are disallowed)
- if allow_repeats or not is_repeat then
- file_contents[#file_contents + 1] = csv_entry
- end
- -- sort file contents; you can pass your own comparator if you want to
- -- reference: http://lua-users.org/wiki/TableLibraryTutorial
- table.sort(file_contents)
- -- open file for writing
- csv_file = io.open(csv_filename, 'w')
- if not csv_file then
- mp.osd_message('Could not open ' .. tostring(csv_filename) .. ' file for writing')
- return
- end
- -- write file entry by entry
- for index, line in pairs(file_contents) do
- if csv_entry_separator == '\n' then
- csv_file:write(line, csv_entry_separator)
- elseif index > 1 then
- csv_file:write(csv_entry_separator, line)
- else
- csv_file:write(line)
- end
- end
- csv_file:close()
- end
- function on_seek()
- seek_flag = true
- end
- mp.register_event('file-loaded', on_file_loaded)
- mp.register_event('seek', on_seek)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement