mpv2oboeru

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

config.lua (3146B)


      1 local mpopt = require('mp.options')
      2 local helpers = require('helpers')
      3 local initial_config = {}
      4 local default_profile_filename = 'subs2srs'
      5 local profiles_filename = 'subs2srs_profiles'
      6 
      7 local config, profiles
      8 
      9 local function set_audio_format()
     10     if config.audio_format == 'opus' then
     11         config.audio_codec = 'libopus'
     12         config.audio_extension = '.opus'
     13     else
     14         config.audio_codec = 'libmp3lame'
     15         config.audio_extension = '.mp3'
     16     end
     17 end
     18 
     19 local function set_video_format()
     20     if config.snapshot_format == 'webp' then
     21         config.snapshot_extension = '.webp'
     22         config.snapshot_codec = 'libwebp'
     23     else
     24         config.snapshot_extension = '.jpg'
     25         config.snapshot_codec = 'mjpeg'
     26     end
     27 end
     28 
     29 local function ensure_in_range(dimension)
     30     config[dimension] = config[dimension] < 42 and -2 or config[dimension]
     31     config[dimension] = config[dimension] > 640 and 640 or config[dimension]
     32 end
     33 
     34 local function conditionally_set_defaults(width, height, quality)
     35     if config[width] < 1 and config[height] < 1 then
     36         config[width] = -2
     37         config[height] = 200
     38     end
     39     if config[quality] < 0 or config[quality] > 100 then
     40         config[quality] = 15
     41     end
     42 end
     43 
     44 local function check_image_settings()
     45     ensure_in_range('snapshot_width')
     46     ensure_in_range('snapshot_height')
     47     conditionally_set_defaults('snapshot_width', 'snapshot_height', 'snapshot_quality')
     48 end
     49 
     50 local function validate_config()
     51     set_audio_format()
     52     set_video_format()
     53     check_image_settings()
     54 end
     55 
     56 local function load_profile(profile_name)
     57     if helpers.is_empty(profile_name) then
     58         profile_name = profiles.active
     59         if helpers.is_empty(profile_name) then
     60             profile_name = default_profile_filename
     61         end
     62     end
     63     mpopt.read_options(config, profile_name)
     64 end
     65 
     66 local function save_initial_config()
     67     for key, value in pairs(config) do
     68         initial_config[key] = value
     69     end
     70 end
     71 
     72 local function restore_initial_config()
     73     for key, value in pairs(initial_config) do
     74         config[key] = value
     75     end
     76 end
     77 
     78 local function next_profile()
     79     local first, next, new
     80     for profile in string.gmatch(profiles.profiles, '[^,]+') do
     81         if not first then
     82             first = profile
     83         end
     84         if profile == profiles.active then
     85             next = true
     86         elseif next then
     87             next = false
     88             new = profile
     89         end
     90     end
     91     if next == true or not new then
     92         new = first
     93     end
     94     profiles.active = new
     95     restore_initial_config()
     96     load_profile(profiles.active)
     97     validate_config()
     98 end
     99 
    100 local function init(config_table, profiles_table)
    101     config, profiles = config_table, profiles_table
    102     -- 'subs2srs' is the main profile, it is always loaded.
    103     -- 'active profile' overrides it afterwards.
    104     mpopt.read_options(profiles, profiles_filename)
    105     load_profile(default_profile_filename)
    106     save_initial_config(config)
    107     if profiles.active ~= default_profile_filename then
    108         load_profile(profiles.active)
    109     end
    110     validate_config()
    111 end
    112 
    113 return {
    114     init = init,
    115     next_profile = next_profile,
    116 }