Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. local mp = require 'mp'
  2. local msg = require 'mp.msg'
  3.  
  4.  
  5. -- path to csv file
  6. local csv_filename = 'sheet.csv'
  7. local csv_entry_separator = '\n' -- change it to ',' if you want all the entries in a single line
  8. local allow_repeats = false
  9. local ignore_seeked = false
  10.  
  11. -- this function returns csv entry that will be written to file
  12. -- edit this function to your liking
  13. --
  14. -- i wrapped it in a function, so that any additional logic will not fuck up anything else
  15. --
  16. -- reference:
  17. -- https://mpv.io/manual/master/#command-interface-filename
  18. -- https://mpv.io/manual/master/#command-interface-path
  19. function get_csv_entry()
  20.     -- reference: https://mpv.io/manual/master/#properties
  21.     local filename = mp.get_property('filename')
  22.     local filesize = mp.get_property('file-size')
  23.    
  24.     filename = filename:gsub('%,', '.') -- replace all ',' with '.' in filename to avoid csv conflicts
  25.    
  26.     csv_entry = filename .. ',' .. filesize
  27.    
  28.     -- could be useful for debugging
  29.     -- mp.osd_message('csv_entry: ' .. tostring(csv_entry))
  30.     return csv_entry
  31. end
  32.  
  33. -- this function determines if the entry should be written to the csv file
  34. -- it takes get_csv_entry result as an argument, feel free to use (or completely ignore) it
  35. function check_file(csv_entry)
  36.     if ignore_seeked and seek_flag then
  37.         return false
  38.     end
  39.  
  40.     local percent_pos = mp.get_property('percent-pos')
  41.     if not percent_pos then
  42.         return false
  43.     end
  44.    
  45.     while percent_pos and tonumber(percent_pos) < 80 do
  46.         sleep(5)
  47.         percent_pos = mp.get_property('percent-pos')
  48.         -- mp.osd_message('loop')
  49.     end
  50.  
  51.     -- mp.osd_message('exit loop')
  52.    
  53.     if not percent_pos then
  54.         return false
  55.     end
  56.  
  57.     return true
  58. end
  59.  
  60. -- ------------------------------------------------------------------------------------------------------------
  61. local seek_flag = false -- don't touch this one
  62.  
  63. function detect_platform()
  64.     local o = {}
  65.     -- Kind of a dumb way of detecting the platform but whatever
  66.     if mp.get_property_native('options/vo-mmcss-profile', o) ~= o then
  67.         return 'windows'
  68.     elseif mp.get_property_native('options/input-app-events', o) ~= o then
  69.         return 'macos'
  70.     end
  71.     return 'linux'
  72. end
  73.  
  74. function sleep(seconds)
  75.     local platform = detect_platform()
  76.     if(platform == 'windows') then
  77.         os.execute("ping -n " .. tonumber(seconds+1) .. " localhost > NUL")
  78.     else
  79.         os.execute("sleep " .. tonumber(seconds))
  80.     end
  81. end
  82.  
  83. function on_file_loaded()
  84.     seek_flag = false
  85.     local csv_entry = get_csv_entry()
  86.     if not csv_entry or not check_file(csv_entry) then
  87.         -- could be useful for debugging
  88.         -- mp.osd_message('File didn\'t pass verification')
  89.         return -- do nothing if the file verification didn't pass
  90.     end
  91.    
  92.     -- uncomment for debugging
  93.     -- mp.osd_message('File verification passed')
  94.    
  95.     local file_contents = {}
  96.     local is_repeat = false
  97.    
  98.     -- read current file contents if file exists
  99.     local csv_file = io.open(csv_filename, 'r')
  100.     if csv_file then
  101.         for line in csv_file:lines() do
  102.             if not is_repeat and line == csv_entry then
  103.                 is_repeat = true
  104.             end
  105.            
  106.             table.insert(file_contents, line)
  107.         end
  108.        
  109.         csv_file:close()
  110.     end
  111.    
  112.     -- append new entry at the end of the file (unless it's a repeat and repeats are disallowed)
  113.     if allow_repeats or not is_repeat then
  114.         file_contents[#file_contents + 1] = csv_entry
  115.     end
  116.    
  117.     -- sort file contents; you can pass your own comparator if you want to
  118.     -- reference: http://lua-users.org/wiki/TableLibraryTutorial
  119.     table.sort(file_contents)
  120.    
  121.     -- open file for writing
  122.     csv_file = io.open(csv_filename, 'w')
  123.     if not csv_file then
  124.         mp.osd_message('Could not open ' .. tostring(csv_filename) .. ' file for writing')
  125.         return
  126.     end
  127.    
  128.     -- write file entry by entry
  129.     for index, line in pairs(file_contents) do
  130.         if csv_entry_separator == '\n' then
  131.             csv_file:write(line,  csv_entry_separator)
  132.         elseif index > 1 then
  133.             csv_file:write(csv_entry_separator, line)
  134.         else
  135.             csv_file:write(line)
  136.         end
  137.     end
  138.  
  139.     csv_file:close()
  140. end
  141.  
  142. function on_seek()
  143.     seek_flag = true
  144. end
  145.  
  146. mp.register_event('file-loaded', on_file_loaded)
  147. mp.register_event('seek', on_seek)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement