mpv2oboeru

mpv helpers to create flashcards from movies and TV shows
git clone anongit@rnpnr.xyz:mpv2oboeru.git
Log | Files | Refs | Feed | README | LICENSE

helpers.lua (1721B)


      1 local mp = require('mp')
      2 local msg = require('mp.msg')
      3 
      4 local unpack = unpack and unpack or table.unpack
      5 
      6 local function is_empty(var)
      7   return var == nil or var == '' or (type(var) == 'table' and next(var) == nil)
      8 end
      9 
     10 local function get_episode_number(filename)
     11   -- Reverses the filename to start the search from the end as the media title might contain similar numbers.
     12   local filename_reversed = filename:reverse()
     13 
     14   local ep_num_patterns = {
     15       "[%s_](%d?%d?%d)[pP]?[eE]", -- Starting with E or EP (case-insensitive). "Example Series S01E01 [94Z295D1]"
     16       "^(%d?%d?%d)[pP]?[eE]", -- Starting with E or EP (case-insensitive) at the end of filename. "Example Series S01E01"
     17       "%)(%d?%d?%d)%(", -- Surrounded by parentheses. "Example Series (12)"
     18       "%](%d?%d?%d)%[", -- Surrounded by brackets. "Example Series [01]"
     19       "%s(%d?%d?%d)%s", -- Surrounded by whitespace. "Example Series 124 [1080p 10-bit]"
     20       "_(%d?%d?%d)_", -- Surrounded by underscores. "Example_Series_04_1080p"
     21       "^(%d?%d?%d)[%s_]", -- Ending to the episode number. "Example Series 124"
     22       "(%d?%d?%d)%-edosipE", -- Prepended by "Episode-". "Example Episode-165"
     23   }
     24 
     25   local s, e, episode_num
     26   for _, pattern in pairs(ep_num_patterns) do
     27       s, e, episode_num = string.find(filename_reversed, pattern)
     28       if not is_empty(episode_num) then
     29           return #filename - e, #filename - s, episode_num:reverse()
     30       end
     31   end
     32 end
     33 
     34 local function notify(message, level, duration)
     35     level = level or 'info'
     36     duration = duration or 1
     37     msg[level](message)
     38     mp.osd_message(message, duration)
     39 end
     40 
     41 return {
     42   is_empty = is_empty,
     43   get_episode_number = get_episode_number,
     44   notify = notify,
     45   unpack = unpack,
     46 }