添加自动调整屏幕大小功能

This commit is contained in:
nnwang
2026-02-22 07:11:56 +08:00
parent f7acd8ce9a
commit 06f9013090

View File

@@ -1,11 +1,72 @@
-- 简化版视频播放器 -- 简化版视频播放器
local gpu = peripheral.wrap("tm_gpu_0") local gpu = peripheral.wrap("tm_gpu_0")
local gpu_Size = 64 local gpu_Size_Auto = true --自动分辨率设置
local gpu_Size = 64 --默认屏幕分辨率
if not gpu then gpu = peripheral.find("tm_gpu") end if not gpu then gpu = peripheral.find("tm_gpu") end
gpu.refreshSize() gpu.refreshSize()
gpu.setSize(gpu_Size) gpu.setSize(gpu_Size)
local w, h = gpu.getSize() local w, h, w_black, h_black, Size = gpu.getSize()
-- 分批下载(每批 10 帧)
local BATCH_SIZE = 10
local function computeOptimalSize(w_black, h_black)
local w_target, h_target = 640, 360
local max_size = 64
-- 避免除零
if w_black <= 0 or h_black <= 0 then
return 1
end
-- 计算理想缩放比例(不能让任一边超过目标)
local size_by_w = w_target / w_black
local size_by_h = h_target / h_black
local ideal_size = math.min(size_by_w, size_by_h)
-- 如果理想值 >= 64直接用 64
if ideal_size >= max_size then
return max_size
end
-- 否则,在 floor 和 ceil 中选更优的(但不能超过 64
local size_floor = math.floor(ideal_size)
local size_ceil = math.ceil(ideal_size)
-- 确保不越界
if size_floor < 1 then size_floor = 1 end
if size_ceil > max_size then size_ceil = max_size end
-- 计算两个候选的误差(欧氏距离或曼哈顿距离均可,这里用平方和)
local function error(size)
local w = w_black * size
local h = h_black * size
return (w - w_target)^2 + (h - h_target)^2
end
local err_floor = error(size_floor)
local err_ceil = error(size_ceil)
if err_floor <= err_ceil then
return size_floor
else
return size_ceil
end
end
if gpu_Size_Auto then
local optimal_Size = computeOptimalSize(w_black, h_black)
print("[I] gpu_Size: "..optimal_Size)
gpu.refreshSize()
gpu.setSize(optimal_Size)
end
local w, h, w_black, h_black, Size = gpu.getSize()
local mypath = "/"..fs.getDir(shell.getRunningProgram()) local mypath = "/"..fs.getDir(shell.getRunningProgram())
if not fs.exists(mypath.."/speakerlib.lua") then shell.run("wget https://git.liulikeji.cn/xingluo/computer_craft_video_play/raw/branch/main/speakerlib.lua") end if not fs.exists(mypath.."/speakerlib.lua") then shell.run("wget https://git.liulikeji.cn/xingluo/computer_craft_video_play/raw/branch/main/speakerlib.lua") end
@@ -188,8 +249,6 @@ local function unpackFramePack(data)
return frames return frames
end end
-- 分批下载(每批 50 帧)
local BATCH_SIZE = 20
-- local totalFrames = #videoInfo.frame_urls -- local totalFrames = #videoInfo.frame_urls
local totalFrames = videoInfo.fps * 20 -- 仅下载前20秒以节省时间 local totalFrames = videoInfo.fps * 20 -- 仅下载前20秒以节省时间
local allFrameData = {} local allFrameData = {}
@@ -219,7 +278,7 @@ for _, batch in ipairs(initBatches) do
url = server_url .. "/api/framepack?" .. batch.urls[1], url = server_url .. "/api/framepack?" .. batch.urls[1],
headers = { ["Content-Type"] = "application/json" }, headers = { ["Content-Type"] = "application/json" },
body = textutils.serializeJSON({ urls = batch.urls }), body = textutils.serializeJSON({ urls = batch.urls }),
timeout = 3, timeout = 2,
binary = true binary = true
}) })
@@ -265,7 +324,7 @@ until _G.audio_ready
local starttime1 = os.clock() local starttime1 = os.clock()
-- 播放前已缓存 20 秒
local totalFramesToPlay = #videoInfo.frame_urls local totalFramesToPlay = #videoInfo.frame_urls
-- 标志:是否还在播放 -- 标志:是否还在播放
@@ -319,7 +378,7 @@ local function cacheAhead()
url = url, url = url,
headers = { ["Content-Type"] = "application/json" }, headers = { ["Content-Type"] = "application/json" },
body = body, body = body,
timeout = 3, timeout = 2,
binary = true binary = true
}) })
pendingRequests[url] = batch pendingRequests[url] = batch
@@ -455,7 +514,7 @@ local function renderVideo()
math.floor(ui_avgMs), math.floor(ui_avgMs),
math.floor(ui_lowFps), math.floor(ui_lowFps),
math.floor(ui_lowMs), math.floor(ui_lowMs),
w,h,gpu_Size w,h,Size
) )
gpu.drawText(1, 1, statusText, 0xFD452A) gpu.drawText(1, 1, statusText, 0xFD452A)
end end
@@ -484,11 +543,7 @@ local function renderVideo()
if nil_e >= 200 then if nil_e >= 200 then
error("\n[E] No available video data for 10 seconds \nDownload speed is below the minimum threshold! \nPlease try checking your network bandwidth or adjusting the resolution.") error("\n[E] No available video data for 10 seconds \nDownload speed is below the minimum threshold! \nPlease try checking your network bandwidth or adjusting the resolution.")
end end
repeat sleep(2)
local event, id = os.pullEvent("timer")
until id == timer_id
timer_id = os.startTimer(frameDelay) timer_id = os.startTimer(frameDelay)
-- === 修改点:数据为空时,不做任何操作,直接进入下方的同步逻辑 === -- === 修改点:数据为空时,不做任何操作,直接进入下方的同步逻辑 ===
-- 此时 frameEnd 依然等于 frameStart计算出的 costTime 为 0 -- 此时 frameEnd 依然等于 frameStart计算出的 costTime 为 0