469 lines
14 KiB
Lua
469 lines
14 KiB
Lua
-- 简化版视频播放器
|
||
local gpu = peripheral.wrap("tm_gpu_0")
|
||
gpu.refreshSize()
|
||
gpu.setSize(64)
|
||
local w, h = gpu.getSize()
|
||
|
||
if not fs.exists("speakerlib.lua") then shell.run("wget https://git.liulikeji.cn/xingluo/computer_craft_video_play/raw/branch/main/speakerlib.lua") end
|
||
|
||
server_url = "https://newgmapi.liulikeji.cn"
|
||
|
||
-- 检查命令行参数
|
||
local videoUrl = ...
|
||
if not videoUrl then
|
||
print("Usage: video_player <video URL>")
|
||
print("Example: video_player https://example.com/video.mp4")
|
||
return
|
||
end
|
||
|
||
_G.audio_ready = false
|
||
local task_id
|
||
local status_url
|
||
|
||
if videoUrl:sub(1,4) ~= "http" then
|
||
-- 如果不是 URL,则直接当作 task_id 使用
|
||
task_id = videoUrl
|
||
status_url = server_url .. "/api/task/" .. task_id
|
||
else
|
||
-- 创建新任务
|
||
print("Submitting video frame extraction task...")
|
||
local requestData = {
|
||
video_url = videoUrl,
|
||
w = w,
|
||
h = h,
|
||
force_resolution = false,
|
||
pad_to_target = true,
|
||
fps = 20
|
||
}
|
||
|
||
local response = http.post(
|
||
server_url .. "/api/video_frame/async",
|
||
textutils.serializeJSON(requestData),
|
||
{["Content-Type"] = "application/json"}
|
||
)
|
||
|
||
if not response then
|
||
error("Failed to connect to API server")
|
||
end
|
||
|
||
local respBody = response.readAll()
|
||
response.close()
|
||
|
||
local createResult = textutils.unserialiseJSON(respBody)
|
||
if not createResult or createResult.status ~= "success" then
|
||
error("Task creation failed: " .. (createResult and createResult.message or "Unknown error"))
|
||
end
|
||
|
||
task_id = createResult.task_id
|
||
status_url = createResult.status_url
|
||
|
||
|
||
end
|
||
|
||
term.clear()
|
||
term.setCursorPos(1,1)
|
||
print("task_id: " .. task_id)
|
||
|
||
-- 用于记录已打印的日志数量,避免重复打印
|
||
local total_logs_printed = 0
|
||
speakerrun = true
|
||
while true do
|
||
|
||
|
||
local response = http.get(status_url, {["Content-Type"] = "application/json"})
|
||
if not response then
|
||
error("Failed to fetch task status")
|
||
end
|
||
|
||
local respBody = response.readAll()
|
||
response.close()
|
||
|
||
local task_info = textutils.unserialiseJSON(respBody)
|
||
|
||
term.clear()
|
||
term.setCursorPos(1,1)
|
||
-- 打印基本信息
|
||
print("task_id: " .. task_id)
|
||
print("Status: " .. (task_info.status or "unknown"))
|
||
print("Progress: " .. (task_info.progress or 0) .. "%")
|
||
if task_info.message then
|
||
print("Message: " .. task_info.message)
|
||
end
|
||
|
||
-- 打印新增日志(new_logs 是数组)
|
||
if task_info.new_logs and #task_info.new_logs > 0 then
|
||
for i = 1, #task_info.new_logs do
|
||
print(task_info.new_logs[i])
|
||
end
|
||
total_logs_printed = total_logs_printed + #task_info.new_logs
|
||
end
|
||
|
||
if task_info.result.audio_urls and speakerrun then
|
||
speakerrun = false
|
||
shell.run("bg speakerlib play ".. server_url .. task_info.result.audio_urls.audio_dfpwm_url .." ".. server_url .. task_info.result.audio_urls.audio_dfpwm_left_url .." ".. server_url .. task_info.result.audio_urls.audio_dfpwm_right_url)
|
||
end
|
||
|
||
-- 检查是否完成
|
||
if task_info.result.current_frames then
|
||
if task_info.result.current_frames >= 200 then
|
||
break
|
||
end
|
||
end
|
||
|
||
sleep(1)
|
||
end
|
||
|
||
|
||
-- 获取最终结果
|
||
local response = http.get(status_url, {["Content-Type"] = "application/json"})
|
||
local finalResp = textutils.unserialiseJSON(response.readAll())
|
||
response.close()
|
||
|
||
videoInfo = finalResp.result
|
||
videoInfo.fps = 20
|
||
|
||
videoInfo.frame_urls = {}
|
||
|
||
-- /frames/20116713/frame_000001.png
|
||
for i = 1, videoInfo.total_frames - 10 do
|
||
videoInfo.frame_urls[i] = "/frames/"..task_id.."/frame_" .. string.format("%06d", i) .. ".png"
|
||
end
|
||
|
||
-- 播放音频 单通道
|
||
--shell.run("bg speaker play", server_url .. videoInfo.audio_dfpwm_url)
|
||
|
||
-- server_url .. videoInfo.audio_urls.audio_dfpwm_url -- 音频单通道文件 URL
|
||
-- server_url .. videoInfo.audio_urls.audio_dfpwm_right_url -- 音频右通道文件 URL
|
||
-- server_url .. videoInfo.audio_urls.audio_dfpwm_left_url -- 音频左通道文件 URL
|
||
|
||
-- 下载和播放函数
|
||
frames = {}
|
||
local frameCount = 0
|
||
frameData = {}
|
||
|
||
local get_task = {}
|
||
|
||
local function readU32(s, pos)
|
||
if pos + 3 > #s then return nil end
|
||
local b1, b2, b3, b4 = s:byte(pos, pos + 3)
|
||
-- 使用 bit32.lshift 和 bit32.bor
|
||
return bit32.bor(
|
||
bit32.lshift(b1, 24),
|
||
bit32.lshift(b2, 16),
|
||
bit32.lshift(b3, 8),
|
||
b4
|
||
)
|
||
end
|
||
|
||
-- 解析 FramePack 二进制数据
|
||
local function unpackFramePack(data)
|
||
local frames = {}
|
||
local pos = 1
|
||
|
||
local frameCount = readU32(data, pos)
|
||
if not frameCount then error("Invalid framepack header") end
|
||
pos = pos + 4
|
||
|
||
for i = 1, frameCount do
|
||
local size = readU32(data, pos)
|
||
if not size then error("Truncated framepack at frame " .. i) end
|
||
pos = pos + 4
|
||
if pos + size - 1 > #data then error("Frame " .. i .. " out of bounds") end
|
||
local frameData = data:sub(pos, pos + size - 1)
|
||
frames[i] = frameData
|
||
pos = pos + size
|
||
end
|
||
|
||
return frames
|
||
end
|
||
|
||
-- 分批下载(每批 50 帧)
|
||
local BATCH_SIZE = 20
|
||
-- local totalFrames = #videoInfo.frame_urls
|
||
local totalFrames = videoInfo.fps * 10 -- 仅下载前10秒以节省时间
|
||
local allFrameData = {}
|
||
|
||
-- 第一步:构建所有需要下载的批次(仅前10秒)
|
||
local initBatches = {}
|
||
for startIdx = 1, totalFrames, BATCH_SIZE do
|
||
local endIdx = math.min(startIdx + BATCH_SIZE - 1, totalFrames)
|
||
local urls = {}
|
||
for i = startIdx, endIdx do
|
||
table.insert(urls, videoInfo.frame_urls[i])
|
||
end
|
||
table.insert(initBatches, {
|
||
start = startIdx,
|
||
urls = urls
|
||
})
|
||
end
|
||
|
||
print("Pre-caching first " .. totalFrames .. " frames (" .. #initBatches .. " batches)...")
|
||
|
||
-- 第二步:并发下载所有初始化批次
|
||
local initTasks = {}
|
||
for _, batch in ipairs(initBatches) do
|
||
table.insert(initTasks, function()
|
||
while true do
|
||
local resp = http.post({
|
||
url = server_url .. "/api/framepack?" .. batch.urls[1],
|
||
headers = { ["Content-Type"] = "application/json" },
|
||
body = textutils.serializeJSON({ urls = batch.urls }),
|
||
timeout = 3,
|
||
binary = true
|
||
})
|
||
|
||
if resp then
|
||
local binData = resp.readAll()
|
||
resp.close()
|
||
|
||
local batchFrames = unpackFramePack(binData)
|
||
for idx = 1, #batchFrames do
|
||
local globalIdx = batch.start + idx - 1
|
||
allFrameData[globalIdx] = batchFrames[idx]
|
||
end
|
||
|
||
print("Cached init batch: " .. batch.start .. " - " .. (batch.start + #batchFrames - 1))
|
||
break
|
||
else
|
||
print("Retry init batch starting at " .. batch.start)
|
||
sleep(0.5)
|
||
end
|
||
end
|
||
end)
|
||
end
|
||
|
||
-- 执行并发下载
|
||
parallel.waitForAll(table.unpack(initTasks))
|
||
|
||
print("Initial caching completed.")
|
||
|
||
-- 播放循环
|
||
local frameDelay = tonumber(string.format("%.3f",1 / videoInfo.fps))
|
||
print("Starting playback (FPS: " .. videoInfo.fps .. ")")
|
||
print(frameDelay * #videoInfo.frame_urls)
|
||
|
||
repeat
|
||
sleep(0.05)
|
||
until _G.audio_ready
|
||
|
||
local starttime1 = os.clock()
|
||
|
||
|
||
-- 播放前已缓存 10 秒
|
||
local totalFramesToPlay = #videoInfo.frame_urls
|
||
local maxCachedFrames = math.min(videoInfo.fps * 20, totalFramesToPlay) -- 最多缓存 20 秒
|
||
local nextDownloadIndex = math.min(videoInfo.fps * 10 + 1, totalFramesToPlay + 1) -- 从第 10 秒后开始继续下载
|
||
|
||
-- 标志:是否还在播放
|
||
local running = true
|
||
|
||
-- 全局帧索引(由播放线程更新)
|
||
local frameIndex = 1
|
||
|
||
|
||
-- 共享状态(用于 cacheAhead 和 httpResponseHandler 通信)
|
||
local pendingRequests = {} -- url -> { batch = {...}, retry = N }
|
||
local downloadedFrames = {} -- frameIndex -> true (防止重复调度)
|
||
|
||
-- 缓存协程:边播边下(使用异步 http.request)
|
||
local function cacheAhead()
|
||
while running do
|
||
local currentStart = frameIndex
|
||
local currentEnd = math.min(frameIndex + videoInfo.fps * 20 - 1, totalFramesToPlay)
|
||
|
||
local batches = {}
|
||
local i = currentStart
|
||
while i <= currentEnd do
|
||
if not allFrameData[i] and not downloadedFrames[i] then
|
||
local batchStart = i
|
||
local batchEnd = math.min(batchStart + BATCH_SIZE - 1, currentEnd)
|
||
local urls = {}
|
||
for j = batchStart, batchEnd do
|
||
if not allFrameData[j] and not downloadedFrames[j] then
|
||
table.insert(urls, videoInfo.frame_urls[j])
|
||
downloadedFrames[j] = true
|
||
end
|
||
end
|
||
if #urls > 0 then
|
||
table.insert(batches, {
|
||
start = batchStart,
|
||
urls = urls,
|
||
retry = 0
|
||
})
|
||
end
|
||
i = batchEnd + 1
|
||
else
|
||
i = i + 1
|
||
end
|
||
end
|
||
|
||
-- 发起所有新请求(不等待!)
|
||
for _, batch in ipairs(batches) do
|
||
local url = server_url .. "/api/framepack?" .. batch.urls[1]
|
||
local body = textutils.serializeJSON({ urls = batch.urls })
|
||
http.request({
|
||
url = url,
|
||
headers = { ["Content-Type"] = "application/json" },
|
||
body = body,
|
||
timeout = 2,
|
||
binary = true
|
||
})
|
||
pendingRequests[url] = batch
|
||
end
|
||
|
||
-- 如果没有要下载的,小睡
|
||
if next(batches) == nil then
|
||
sleep(0.5)
|
||
end
|
||
end
|
||
end
|
||
|
||
-- HTTP 处理协程
|
||
local function httpResponseHandler()
|
||
while running do
|
||
local event, url, handleOrErr = os.pullEvent()
|
||
|
||
if event == "http_success" then
|
||
local batch = pendingRequests[url]
|
||
if batch then
|
||
pendingRequests[url] = nil
|
||
local binData = handleOrErr.readAll()
|
||
handleOrErr.close()
|
||
|
||
local success, batchFrames = pcall(unpackFramePack, binData)
|
||
if success then
|
||
for idx = 1, #batchFrames do
|
||
local globalIdx = batch.start + idx - 1
|
||
if not allFrameData[globalIdx] then
|
||
allFrameData[globalIdx] = batchFrames[idx]
|
||
end
|
||
end
|
||
print("[V] Cached batch: " .. batch.start .. " - " .. (batch.start + #batchFrames - 1))
|
||
else
|
||
print("[R] Unpack failed for batch " .. batch.start .. ": " .. tostring(batchFrames))
|
||
-- 标记这些帧可重试
|
||
for j = batch.start, batch.start + #batch.urls - 1 do
|
||
downloadedFrames[j] = nil
|
||
end
|
||
end
|
||
end
|
||
|
||
elseif event == "http_failure" then
|
||
local batch = pendingRequests[url]
|
||
if batch then
|
||
pendingRequests[url] = nil
|
||
batch.retry = (batch.retry or 0) + 1
|
||
if batch.retry < 3 then
|
||
print("[R] Retrying batch " .. batch.start .. " (attempt " .. (batch.retry + 1) .. ")")
|
||
-- 允许重试
|
||
for j = batch.start, batch.start + #batch.urls - 1 do
|
||
downloadedFrames[j] = nil
|
||
end
|
||
-- 重新触发 cacheAhead 下一轮会重发
|
||
else
|
||
print("[X] Giving up on batch " .. batch.start)
|
||
-- 永久失败,不再重试
|
||
for j = batch.start, batch.start + #batch.urls - 1 do
|
||
downloadedFrames[j] = true
|
||
end
|
||
end
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
||
|
||
-- 视频渲染协程
|
||
local function renderVideo()
|
||
local frameIndex2 = 0
|
||
local play_fps = videoInfo.fps
|
||
|
||
local starttime = os.clock()
|
||
os.queueEvent("audio_start")
|
||
while running and frameIndex <= #videoInfo.frame_urls do
|
||
|
||
|
||
local frame_start_time = os.clock()
|
||
|
||
local data = allFrameData[frameIndex]
|
||
local imgBin = {}
|
||
for j = 1, #data do
|
||
imgBin[#imgBin + 1] = data:byte(j)
|
||
end
|
||
|
||
for i = 1, frameIndex - 5 do
|
||
allFrameData[i] = nil
|
||
end
|
||
|
||
data = nil
|
||
local success, image = pcall(function()
|
||
return gpu.decodeImage(table.unpack(imgBin))
|
||
end)
|
||
|
||
--local image = frames[frameIndex]
|
||
|
||
|
||
-- 播放当前帧
|
||
if image then
|
||
|
||
|
||
gpu.drawImage(0, 0, image.ref())
|
||
--gpu.drawText(1,1,frameIndex .. " / "..#videoInfo.frame_urls.." fps: "..math.ceil(play_fps),0xffffff)
|
||
gpu.drawText(1,1,frameIndex .. " / "..#videoInfo.frame_urls.." fps: "..math.ceil(play_fps),0xffffff)
|
||
local event, id
|
||
if frameIndex > 1 then
|
||
repeat
|
||
event, id = os.pullEvent("timer")
|
||
until id == timer_id
|
||
end
|
||
|
||
timer_id = os.startTimer(frameDelay)
|
||
|
||
gpu.sync()
|
||
|
||
-- for i = 1, frameIndex - 4 do
|
||
-- local image = frames[i]
|
||
-- image:free()
|
||
-- end
|
||
image:free()
|
||
else
|
||
error(frameIndex.. "Frame does not exist, possibly due to full memory or insufficient performance")
|
||
end
|
||
|
||
|
||
|
||
local frame_end_time = os.clock()
|
||
|
||
local frame_time = frame_end_time - frame_start_time
|
||
|
||
play_fps = 1 / frame_time
|
||
|
||
local play_time = tonumber(string.format("%.3f",os.clock() - starttime))
|
||
|
||
frameIndex2 = math.ceil(play_time / frameDelay)
|
||
|
||
if frameIndex2 + (frameIndex - frameIndex2) * 0.5 >= 4 then
|
||
frameIndex = frameIndex2
|
||
else
|
||
frameIndex = frameIndex+1
|
||
end
|
||
|
||
|
||
end
|
||
end
|
||
|
||
|
||
|
||
|
||
-- 启动协程
|
||
-- 启动三个协程:渲染 + 缓存调度 + HTTP 响应处理
|
||
parallel.waitForAny(renderVideo, cacheAhead, httpResponseHandler)
|
||
local endtime = os.clock()
|
||
local time = endtime-starttime1
|
||
|
||
print("Playback finished")
|
||
print("Play_Time: ".. time)
|
||
local fps = #videoInfo.frame_urls / time
|
||
print("Final_FPS: "..fps)
|
||
|
||
print("Average Frame Interval: "..1/fps) |