更新 play.lua
This commit is contained in:
22
play.lua
22
play.lua
@@ -14,13 +14,13 @@ if not videoUrl then
|
|||||||
print("Example: video_player https://example.com/video.mp4")
|
print("Example: video_player https://example.com/video.mp4")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- 假设 videoUrl, w, h 已定义
|
|
||||||
|
|
||||||
local task_id
|
local task_id
|
||||||
local status_url
|
local status_url
|
||||||
|
|
||||||
if videoUrl:sub(1,4) ~= "http" then
|
if videoUrl:sub(1,4) ~= "http" then
|
||||||
-- 如果不是 URL,则直接当作 task_id 使用(兼容模式?)
|
-- 如果不是 URL,则直接当作 task_id 使用
|
||||||
task_id = videoUrl
|
task_id = videoUrl
|
||||||
status_url = server_url .. "/api/task/" .. task_id
|
status_url = server_url .. "/api/task/" .. task_id
|
||||||
else
|
else
|
||||||
@@ -111,11 +111,9 @@ local response = http.get(status_url, {["Content-Type"] = "application/json"})
|
|||||||
local finalResp = textutils.unserialiseJSON(response.readAll())
|
local finalResp = textutils.unserialiseJSON(response.readAll())
|
||||||
response.close()
|
response.close()
|
||||||
|
|
||||||
-- 注意:最终结果在 result.result 中
|
|
||||||
videoInfo = finalResp.result.result
|
videoInfo = finalResp.result.result
|
||||||
|
|
||||||
-- 此时 videoInfo 包含 frame_urls, audio_dfpwm_url 等字段
|
-- 此时 videoInfo 包含 frame_urls, audio_dfpwm_url 等字段
|
||||||
-- 可根据需要进一步处理
|
|
||||||
print("\nTask completed! Total frames: " .. #videoInfo.frame_urls)
|
print("\nTask completed! Total frames: " .. #videoInfo.frame_urls)
|
||||||
|
|
||||||
-- 播放音频 单通道
|
-- 播放音频 单通道
|
||||||
@@ -168,7 +166,7 @@ end
|
|||||||
-- 分批下载(每批 50 帧)
|
-- 分批下载(每批 50 帧)
|
||||||
local BATCH_SIZE = 20
|
local BATCH_SIZE = 20
|
||||||
-- local totalFrames = #videoInfo.frame_urls
|
-- local totalFrames = #videoInfo.frame_urls
|
||||||
local totalFrames = videoInfo.fps * 5 -- 仅下载前5秒以节省时间
|
local totalFrames = videoInfo.fps * 10 -- 仅下载前10秒以节省时间
|
||||||
local allFrameData = {}
|
local allFrameData = {}
|
||||||
|
|
||||||
for startIdx = 1, totalFrames, BATCH_SIZE do
|
for startIdx = 1, totalFrames, BATCH_SIZE do
|
||||||
@@ -178,7 +176,7 @@ for startIdx = 1, totalFrames, BATCH_SIZE do
|
|||||||
-- 构造要下载的 URL 列表(相对路径)
|
-- 构造要下载的 URL 列表(相对路径)
|
||||||
local urls = {}
|
local urls = {}
|
||||||
for i = startIdx, endIdx do
|
for i = startIdx, endIdx do
|
||||||
-- 假设 videoInfo.frame_urls 是 "/frames/xxx/frame_xxx.png" 这样的相对路径
|
|
||||||
table.insert(urls, videoInfo.frame_urls[i])
|
table.insert(urls, videoInfo.frame_urls[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -209,8 +207,6 @@ for startIdx = 1, totalFrames, BATCH_SIZE do
|
|||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 后续播放时直接使用 allFrameData[frameIndex]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -256,7 +252,7 @@ sleep(1)
|
|||||||
-- 播放前已缓存 10 秒
|
-- 播放前已缓存 10 秒
|
||||||
local totalFramesToPlay = #videoInfo.frame_urls
|
local totalFramesToPlay = #videoInfo.frame_urls
|
||||||
local maxCachedFrames = math.min(videoInfo.fps * 20, totalFramesToPlay) -- 最多缓存 20 秒
|
local maxCachedFrames = math.min(videoInfo.fps * 20, totalFramesToPlay) -- 最多缓存 20 秒
|
||||||
local nextDownloadIndex = math.min(videoInfo.fps * 5 + 1, totalFramesToPlay + 1) -- 从第 5 秒后开始继续下载
|
local nextDownloadIndex = math.min(videoInfo.fps * 10 + 1, totalFramesToPlay + 1) -- 从第 10 秒后开始继续下载
|
||||||
|
|
||||||
-- 标志:是否还在播放
|
-- 标志:是否还在播放
|
||||||
local running = true
|
local running = true
|
||||||
@@ -355,12 +351,12 @@ local function cacheAhead()
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 小睡避免过于频繁轮询
|
-- 小睡
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- -- 音频播放协程(保持原样)
|
-- -- 音频播放协程
|
||||||
-- local function playAudio()
|
-- local function playAudio()
|
||||||
-- os.queueEvent("audio_start") -- 触发同步
|
-- os.queueEvent("audio_start") -- 触发同步
|
||||||
-- shell.run("bg speakerlib play " ..
|
-- shell.run("bg speakerlib play " ..
|
||||||
@@ -369,7 +365,7 @@ end
|
|||||||
-- server_url .. videoInfo.audio_dfpwm_right_url)
|
-- server_url .. videoInfo.audio_dfpwm_right_url)
|
||||||
-- end
|
-- end
|
||||||
|
|
||||||
-- 视频渲染协程(稍作优化:及时清理旧帧)
|
-- 视频渲染协程
|
||||||
local function renderVideo()
|
local function renderVideo()
|
||||||
local frameIndex2 = 0
|
local frameIndex2 = 0
|
||||||
local play_fps = videoInfo.fps
|
local play_fps = videoInfo.fps
|
||||||
@@ -454,7 +450,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- 启动三个协程
|
-- 启动协程
|
||||||
parallel.waitForAll(renderVideo, cacheAhead)
|
parallel.waitForAll(renderVideo, cacheAhead)
|
||||||
local endtime = os.clock()
|
local endtime = os.clock()
|
||||||
local time = endtime-starttime1
|
local time = endtime-starttime1
|
||||||
|
|||||||
Reference in New Issue
Block a user