Skip to content

Instantly share code, notes, and snippets.

@thelastfantasy
Last active January 2, 2025 07:22
Show Gist options
  • Save thelastfantasy/d806805b2c465c80083738b4cee7c350 to your computer and use it in GitHub Desktop.
Save thelastfantasy/d806805b2c465c80083738b4cee7c350 to your computer and use it in GitHub Desktop.
MPV Resize window
-- resize_window.lua
local mp = require 'mp'
local msg = require 'mp.msg'
local options = {
min_height = 1080,
max_screen_ratio = 0.75,
enabled = true
}
-- 加载 mp.options 模块并读取配置
mp.options = require "mp.options"
mp.options.read_options(options, "resize_window")
local resized = false
-- 可选:定义一个函数来打印当前配置(用于调试)
local function print_options()
msg.info(string.format("Resize Window Options: min_height=%d, max_screen_ratio=%.2f, enabled=%s",
options.min_height, options.max_screen_ratio, tostring(options.enabled)))
end
if not options.enabled then
msg.info("resize_window disabled")
else
mp.msg.info("MPV window resized script running...")
end
-- 在脚本加载时打印当前配置(可选)
print_options()
mp.register_event("file-loaded", function()
if not resized then
-- 检测是否处于全屏模式,如果以全屏启动播放器则跳过该脚本执行
local is_fullscreen = mp.get_property_native("fullscreen")
if is_fullscreen then
msg.info("MPV is in fullscreen mode. Skipping window resize.")
return
end
local video_width = tonumber(mp.get_property("video-params/w", "0")) or 0
local video_height = tonumber(mp.get_property("video-params/h", "0")) or 0
local screen_width = tonumber(mp.get_property("display-width", "0")) or 0
local screen_height = tonumber(mp.get_property("display-height", "0")) or 0
if video_width > 0 and video_height > 0 then
local ratio = video_width / video_height
local max_height = math.floor(screen_height * options.max_screen_ratio)
local target_height = math.max(options.min_height, math.min(max_height, video_height))
local target_width = math.floor(ratio * target_height)
if target_width > screen_width then
target_width = screen_width
target_height = math.floor(target_width / ratio)
end
local percent_string = ""
if target_height == max_height then
local percentage = math.floor((target_height / screen_height) * 100)
percent_string = string.format(" (%d%%)", percentage)
end
mp.set_property("geometry", string.format("%dx%d", target_width, target_height))
msg.info(string.format(
"Resize successful: Adjusted video to %dx%d%s based on screen resolution (%dx%d).",
target_width, target_height, percent_string, screen_width, screen_height
))
resized = true
else
msg.warn("无法确定视频尺寸。调整窗口大小已跳过。")
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment