Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. local mp = require 'mp'
  2.  
  3. local default_visibility = 'auto'
  4. local visibility_by_extension = default_visibility
  5. local is_paused = false
  6. local music_mode = false
  7. local music_mode_timer = mp.add_periodic_timer(1, function ()
  8.         local chapters = mp.get_property_number('chapters', 0)
  9.         local playlist_count = 0
  10.         if chapters == 0 then
  11.             playlist_count = mp.get_property_number('playlist-count', 0)
  12.             if playlist_count > 1 then
  13.                 mp.command('script-message osc-playlist 1 no-osd')
  14.                 return
  15.             end
  16.         end
  17.  
  18.         mp.command('script-message osc-chapterlist 1 no-osd')
  19.     end)
  20. music_mode_timer:stop()
  21.  
  22. -- extensions for which osc will be always hidden
  23. local no_osc_extensions = { 'png', 'jpg', 'jpeg', 'bmp' }
  24. -- helper function ran on file load that determines whether osc should be hidden or not
  25. function determine_visibility_by_file_extension()
  26.     local path = mp.get_property('path')
  27.     if not path then
  28.         visibility_by_extension = default_visibility
  29.         return
  30.     end
  31.    
  32.     local last_dot_pos = (path:reverse()):find('%.')
  33.     if not last_dot_pos then
  34.         visibility_by_extension = default_visibility
  35.         return
  36.     end
  37.    
  38.     local extension = (path:sub(-last_dot_pos+1))
  39.     if not extension then
  40.         visibility_by_extension = default_visibility
  41.         return
  42.     end
  43.  
  44.     for index, value in ipairs(no_osc_extensions) do
  45.         if value == extension then
  46.             visibility_by_extension = 'never'
  47.             -- disable music mode automatically
  48.             if music_mode then
  49.                 toggle_music_mode()
  50.             end
  51.             return
  52.         end
  53.     end
  54.  
  55.     visibility_by_extension = default_visibility
  56. end
  57.  
  58. -- function that updates osc visibility based on file extension, pause state and music mode state
  59. function my_osc_visibility()
  60.     local visibility = visibility_by_extension
  61.     if (is_paused or music_mode) and visibility_by_extension ~= 'never' then
  62.         visibility = 'always'
  63.     end
  64.  
  65.     mp.command('script-message osc-visibility ' .. visibility .. ' no-osd')
  66. end
  67.  
  68. function determine_and_update()
  69.     determine_visibility_by_file_extension()
  70.     my_osc_visibility()
  71. end
  72.  
  73. -- update paused state and osc visibility on "paused" property change
  74. function on_pause(name, value)
  75.     is_paused = value
  76.     my_osc_visibility()    
  77. end
  78.  
  79. -- music mode function
  80. -- keeps chapterlist and osc always visible
  81. function toggle_music_mode()
  82.     if music_mode then
  83.         music_mode_timer:stop()
  84.         music_mode = false
  85.         mp.osd_message('Music mode: off')
  86.         my_osc_visibility()
  87.  
  88.     -- don't apply music mode when in no-osc mode
  89.     -- elseif visibility_by_extension ~= 'never' then
  90.     else
  91.         -- call once before the timer proceeds
  92.         -- mp.command('script-message osc-chapterlist 1 no-osd')
  93.         mp.osd_message('Music mode: on')
  94.         music_mode_timer:resume()
  95.         music_mode = true
  96.         my_osc_visibility()    
  97.     -- else
  98.     --     mp.osd_message('Music mode unavailable for this file type', 5)
  99.     end
  100. end
  101.  
  102. mp.register_script_message('my_osc_visibility', my_osc_visibility)
  103. mp.register_script_message('toggle_music_mode', toggle_music_mode)
  104.  
  105. mp.add_hook('on_preloaded', 50, determine_and_update)
  106. mp.observe_property('pause', 'bool', on_pause)
  107.  
  108. -- always show osc when opening a new file
  109. -- useful for network streams - user can see what is happening in the background
  110. mp.add_hook('on_load', 50, function()
  111.         mp.command('script-message osc-visibility always no-osd')
  112.     end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement