<|start_filename|>speed.lua<|end_filename|>
speed_regexs_fn = '~/.config/mpv/scripts/speed.conf'

function string.interpr_hex(str)
	return (str:gsub('\\x..', function (c)
		return string.char(c:gsub('\\x', '0x')):sub(1, 1)
	end))
end

function trim(s)
	return s:gsub("^%s+", ""):gsub("%s+$", "")
end

function started()
	pth = mp.get_property("path"):lower()

	for _, speed_rgx in pairs(speed_regexs) do
		if pth:find(speed_rgx[2]) then
			mp.set_property("speed", speed_rgx[1])
			break
		end
	end
end

speed_regexs_fn = speed_regexs_fn:gsub('~', os.getenv('HOME'))
speed_regexs = {}

for line in io.lines(speed_regexs_fn) do
	rgx = line:match("^\t(.+)")

	if speed and rgx then
		rgx = trim(rgx):interpr_hex():lower()
		if rgx:len() > 0 then
			table.insert(speed_regexs, {speed, rgx})
		end
	else
		speed_tmp, rgx = line:match("^([%d%.]+)(.*)")

		if speed_tmp and rgx then
			speed = speed_tmp
			rgx = trim(rgx):interpr_hex():lower()
			if rgx:len() > 0 then
				table.insert(speed_regexs, {speed, rgx})
			end	
		end
	end
end

mp.register_event("file-loaded", started)


<|start_filename|>speed_osd3.lua<|end_filename|>
-- recalculates osd-msg3 timecodes with playback-speed != 1

-- show current speed at given paths at top right corner
SHOW_SPEED_IN_PATHS = {
    "^edl://",
    "/med/p/podcasts",
    "/abooks/"
}
----------------------------------------

local assdraw = require('mp.assdraw')

function disp_time(time)
    local hours = math.floor(time/3600)
    local minutes = math.floor((time % 3600)/60)
    local seconds = math.floor(time % 60)
    
    return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

past_value = -1
function osd3(name, value)
    if value ~= nil and math.floor(value) ~= math.floor(past_value) then
        local speed = mp.get_property_number("speed")
        local dur = mp.get_property_number("duration")
        local pp = mp.get_property_number("percent-pos")

        if speed ~= nil and dur ~= nil and pp ~= nil then
            past_value = value
            mp.set_property("osd-msg3", 
                string.format("%s / %s (%i%%)",
                    disp_time(value / speed),
                    disp_time(dur / speed),
                    pp
                )
            )
        end
    end
end

function speed_change(name, value)
    if value ~= 1.0 then
        mp.observe_property("time-pos", "number", osd3)
    else
        mp.unobserve_property(osd3)
        mp.set_property("osd-msg3", "")
    end
    
    local tp = mp.get_property_number("time-pos")
    local speed = mp.get_property_number("speed")
    local dur = mp.get_property_number("duration")
    local pp = mp.get_property_number("percent-pos")

    if tp ~= nil and tp > 0.5 and speed ~= nil and dur ~= nil and pp ~= nil then
        mp.command(
            string.format('show-text "%s / %s (%i%%)\nx%.2f"',
                disp_time(tp / speed),
                disp_time(dur / speed),
                pp,
                speed
            )
        )
    end

    local pth = mp.get_property("path")
    if pth ~= nil then
        for i, work_in_path in pairs(SHOW_SPEED_IN_PATHS) do
            if pth:find(work_in_path) then
                local osd_w, osd_h, aspect = mp.get_osd_size()
                local ass = assdraw:ass_new()
                ass:new_event()
                ass:an(9)
                ass:append(string.format('x%.2f', speed))
                mp.set_osd_ass(osd_w, osd_h, ass.text)

                break
            end
        end
    end
end

function started()
    local speed = mp.get_property_number("speed")
    local dur = mp.get_property_number("duration")

    if speed ~= nil and dur ~= nil then
        mp.set_property("osd-playing-msg", 
            string.format("${filename} \n %s \n ${playlist-pos-1}/${playlist-count}",
                disp_time(dur / speed)
            )
        )
    else
        mp.set_property("osd-playing-msg", "")
    end
end

mp.register_event("file-loaded", started)
mp.observe_property("speed", "number", speed_change)


<|start_filename|>autosubs_via_subliminal.lua<|end_filename|>
-- original script:
-- https://gist.github.com/selsta/ce3fb37e775dbd15c698
--
-- subliminal:
-- https://github.com/Diaoul/subliminal
------------------------------------------------------
-- Seeks subtitles with subliminal at different servers separately and downloads all available.
--
-- keybinding: shift+v - for current file
-- keybinding: shift+b - for all files in playlist
-- 
-- language code(s) for one or more languages to seek
sub_langs = {'de', 'he'}

-- sites where to search
sites = {'addic7ed', 'legendastv', 'opensubtitles', 'podnapisi', 'shooter', 'subscenter', 'thesubdb', 'tvsubtitles'}


local utils = require 'mp.utils'

function display_error()
	mp.msg.warn("Subtitle download failed")
	mp.osd_message("Subtitle download failed")
end

function auto_load_subs_all()
	all_files = 1
	mp.register_event("start-file", load_sub_fn)
	load_sub_fn()
end

function load_sub_fn()
	if all_files == 1 then
		os.execute('sleep .3')
		mp.set_property("pause", 'yes')
	end

	path = mp.get_property("path")

	for k1, language in pairs(sub_langs) do
		for k2, site_name in pairs(sites) do
			path2 = string.gsub(path, "%.%w+$", '.' .. language .. "." .. site_name .. ".srt")

			srt_path = string.gsub(path2, "%.%w+$", ".srt")
			t = { args = { "subliminal", "download", "--provider", site_name, "-s", "-l", language, path2 } }

			mp.osd_message("Searching subtitles...")
			res = utils.subprocess(t)
			if res.error == nil then
				if mp.commandv("sub_add", srt_path) then
					mp.msg.warn("Subtitle download succeeded.")
					mp.osd_message("Subtitle '" .. srt_path .. "' download succeeded")
				else
					display_error()
				end
			else
				display_error()
			end
		end
	end

	if all_files == 1 then
		mp.command("playlist_next force")
	end
end

mp.add_key_binding("V", "auto_load_subs", load_sub_fn)
mp.add_key_binding("B", "auto_load_subs_all", auto_load_subs_all)
