Skip to content

Instantly share code, notes, and snippets.

@bitingsock
bitingsock / noAFK.lua
Last active April 6, 2023 23:05
send virtual keystroke (F15) during playback to prevent auto afk of some applications (requires Windows powershell)
local afkTimer = mp.add_periodic_timer(60, function()
if mp.get_property("pause") == "no" and mp.get_property("vid") ~= "no" then
mp.command_native({
name = "subprocess",
capture_stdout = false,
playback_only = false,
args = { "powershell", "(New-Object -ComObject WScript.Shell).SendKeys('{F15}')" }
})
end
end)
@bitingsock
bitingsock / rewindPlaylist.lua
Created September 6, 2021 10:35
keybind to rewind to the end of the previous playlist entry
local optStart = 0
function seekHandler(duration)
if mp.get_property_number("playlist-pos") > 0 then
optStart = mp.get_property("start")
mp.set_property("start", duration)
mp.command("playlist-prev")
end
end
mp.register_event("file-loaded", function() mp.set_property("start", optStart) end)
@bitingsock
bitingsock / stopCache.lua
Last active July 15, 2023 06:14
stops the demuxer from downloading more if it is already past --end
local maxBytes = mp.get_property("demuxer-max-bytes")
local function stopCache()
local state = mp.get_property_native("demuxer-cache-state")
if state and state["cache-end"] and tonumber(state["cache-end"]) > tonumber(mp.get_property("end")) then
mp.set_property("demuxer-max-bytes",0)
print("stop")
mp.unobserve_property(stopCache)
end
end
local function look()
@bitingsock
bitingsock / ytdl-preload.lua
Last active April 28, 2024 08:08
Precache the next entry in your playlist if it is a network source by downloading it to a temp file ahead of time. Change Line 20 to temp directory. It will delete the directory on exit.
----------------------
-- #example ytdl_preload.conf
-- # make sure lines do not have trailing whitespace
-- # ytdl_opt has no sanity check and should be formatted exactly how it would appear in yt-dlp CLI, they are split into a key/value pair on whitespace
-- # at least on Windows, do not escape '\' in temp, just us a single one for each divider
-- #temp=R:\ytdltest
-- #ytdl_opt1=-r 50k
-- #ytdl_opt2=-N 5
-- #ytdl_opt#=etc
@bitingsock
bitingsock / audio-dupe.lua
Last active April 11, 2024 20:45
Runs a child process to play a second audio stream. Synced through named pipe. Recommend using with [cycle-adevice](https://gist.github.com/bitingsock/ad58ee5da560ecb922fa4a867ac0ecfd) to change the output device of the parent process.
--New child with start with the current 'aid' and 'audio-device' of the parent
-- non-Windows environments require the use of the 'socat' package
local platform_is_windows = (package.config:sub(1, 1) == "\\")
local options = require 'mp.options'
local o = {
pipe_template = platform_is_windows and "\\\\.\\pipe\\mpvDupedAudio" or "~/mpvDupedAudio", --windows format
new_child_key = 'Ctrl+A', --Start a new child: 'Ctrl+Shift+a'
cycle_child_control_key = 'Ctrl+Alt+A', --Cycle child control: 'Ctrl+Shift+Alt+a'
cycle_child_aid_key = 'Ctrl+Alt+a', --Cycle 'aid' of child: 'Ctrl+Alt+a'
child_increase_volume_key = 'Ctrl+Alt+WHEEL_UP', --Change volume of child: 'Ctrl+Alt+wheel'
@bitingsock
bitingsock / channel mixer.lua
Created June 12, 2019 09:12
remix surround channel levels
cmCenter = 1
local defCenter = 1
cmFront = 0.707
local defFront = 0.707
cmSide = 0.707
local defSide = 0.707
cmBack = 0.707
local defBack = 0.707
cmLFE = 0
local defLFE = 0
@bitingsock
bitingsock / ytdl prefetch resolver.lua
Last active August 19, 2023 18:35
resolves ytdl for urls in playlist when the demuxer reaches near the end of the file
local utils = require 'mp.utils'
local ytdlPath = mp.find_config_file("youtube-dl.exe")
local fileDuration = 0
local function check_position()
if fileDuration==0 then
mp.unobserve_property(check_position)
return
end
local demuxEndPosition = mp.get_property("demuxer-cache-time")
if demuxEndPosition and
lookahead = 5 --if the next subtitle appears after this threshold then speedup
speedup = 5 --the value that "speed" is set to during speedup
leadin = 1 --seconds to stop short of the next subtitle --range 0-2
skipmode = false --instead of speeding up playback seek to the next known subtitle
maxskip = 5 --max seek distance (seconds) when skipmode is enabled
directskip = false --seek to next known subtitle no matter how far away
dropOnAVdesync = true --Because mpv syncs subtitles to audio it is possible that if
--audio processing lags behind video processing then normal playback may not resume
--in sync with the video. If "avsync" > leadin then this disables the audio so that
--we can ensure normal playback resumes on time.
local sense=-20
local speed = mp.get_property("speed")
local detect = false
function f(msg)
if string.find(msg.text, "silence_end") and detect then
mp.set_property("speed",speed)
endmsg=msg.text
detect = false
--print("end")
elseif string.find(msg.text, "silence_start") and detect==false then
@bitingsock
bitingsock / cycle-adevice.lua
Last active January 29, 2024 03:10
Cycle through available audio devices with key binds(shift+a,ctrl+a). Change "wasapi" on line 1 to your relevant audio api.
local api = "wasapi"
local deviceList = mp.get_property_native("audio-device-list")
local aid = 1
local function cycle_adevice(s, e, d)
mp.enable_messages("error")
while s ~= e + d do -- until the loop would cycle back to the number we started on
if string.find(mp.get_property("audio-device"), deviceList[s].name, 1, true) then
while true do
if s + d == 0 then --the device list starts at 1; 0 means we iterated to far
s = #deviceList + 1 --so lets restart at the last device