From 5e1d23b4bbf45bbbcf3b5fc4674c3cf392448db6 Mon Sep 17 00:00:00 2001 From: hkc Date: Sun, 15 Sep 2024 03:31:42 +0300 Subject: [PATCH] Added info.json support and switched to event --- video.lua | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/video.lua b/video.lua index 531635d..d2c0774 100644 --- a/video.lua +++ b/video.lua @@ -12,6 +12,7 @@ local delay = 0 local loading_concurrency = 8 local buffer_size = 8192 local wait_until_input = false +local n_frames, video_url, audio_url_l, audio_url_r while args[1] ~= nil and string.sub(args[1], 1, 1) == "-" do local k = table.remove(args, 1):sub(2) @@ -29,6 +30,15 @@ while args[1] ~= nil and string.sub(args[1], 1, 1) == "-" do buffer_size = tonumber(table.remove(args, 1)) elseif k == "w" or k == "wait" then wait_until_input = true + elseif k == "J" or k == "info-json" then + local req = assert(http.get(table.remove(args, 1))) + local info = textutils.unserializeJSON(req.readAll()) + delay = info.frametime + n_frames = info.frame_count + video_url = info.video + audio_url_l = info.audio.l + audio_url_l = info.audio.r + req.close() end end @@ -42,9 +52,14 @@ if not speakers.l then return end -if #args < 3 then - printError("Usage: video [-m MONITOR] [-l SPK_L] [-r SPK_R] [-d FRAME_T] [RIGHT_CHANNEL]") +if #args < 3 and not n_frames and not video_url and not audio_url_l then + printError("Usage: video [-w] [-b BUFSZ] [-t THREADS] [-J URL] [-m MONITOR] [-l SPK_L] [-r SPK_R] [-d FRAME_T] [RIGHT_CHANNEL]") return +else + n_frames = tonumber(table.remove(args, 1)) + video_url = table.remove(args, 1) + audio_url_l = table.remove(args, 1) + audio_url_r = #args > 0 and table.remove(args, 1) or nil end print(string.format("Using monitor %s", peripheral.getName(monitor))) @@ -54,11 +69,6 @@ else print("Mono sound: " .. peripheral.getName(speakers.l)) end -local n_frames = tonumber(table.remove(args, 1)) -local video_url = table.remove(args, 1) -local audio_url_l = table.remove(args, 1) -local audio_url_r = #args > 0 and table.remove(args, 1) or nil - if not speakers.r and audio_url_r then printError("No right speaker found but right audio channel was specified") printError("Right channel will not be played") @@ -157,7 +167,6 @@ table.insert(subthreads, function() print() end) -local playback_locked = true local playback_done = false table.insert(subthreads, function() @@ -170,22 +179,19 @@ table.insert(subthreads, function() term.setCursorPos(1, ty - 1) term.setBackgroundColor(colors.gray) term.clearLine() - term.write(string.format("Waiting for frames... (V:%d, A:%d) [ENTER to skip]", #frames, #audio_frames.l)) + term.write(string.format("Waiting for frames... (V:%d, A:%d)", #frames, #audio_frames.l)) if #frames > 60 and #audio_frames.l >= n_audio_samples and not wait_until_input then break end end - playback_locked = false -end) - -table.insert(subthreads, function() - while playback_locked do os.sleep(0) end -- spin + os.queueEvent("playback_ready") end) table.insert(subthreads, function() local is_dfpwm = ({ audio_url_l:find("%.dfpwm") })[2] == #audio_url_l local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8 - while playback_locked do os.sleep(0) end -- spin + + os.pullEvent("playback_ready") for i = 1, n_audio_samples do local buffer = decode(audio_frames.l[i]) @@ -202,7 +208,7 @@ table.insert(subthreads, function() local is_dfpwm = ({ audio_url_r:find("%.dfpwm") })[2] == #audio_url_r local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8 - while playback_locked do os.sleep(0) end -- spin + os.pullEvent("playback_ready") for i = 1, n_audio_samples do local buffer = decode(audio_frames.r[i]) @@ -214,7 +220,7 @@ table.insert(subthreads, function() end) table.insert(subthreads, function() - while playback_locked do os.sleep(0) end -- spin + os.pullEvent("playback_ready") local start_t = os.clock() while not playback_done do