From 04a38a7da47b34084a66d4ad35a7b33da7f9cbb8 Mon Sep 17 00:00:00 2001 From: HKXluo Date: Sun, 13 Apr 2025 12:28:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4demo1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo1.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 demo1.lua diff --git a/demo1.lua b/demo1.lua new file mode 100644 index 0000000..626db32 --- /dev/null +++ b/demo1.lua @@ -0,0 +1,49 @@ +-- 定义 API 地址 +local API_URL = "http://ffmpeg.liulikeji.cn/api/ffmpeg" + +-- 内置 MP3 URL(可以替换成你的目标音频) +local INPUT_URL = "http://example.com/audio.mp3" + +-- 发送 POST 请求到 FFmpeg API +local function convertAndPlay() + -- 构造请求数据(使用 DFPWM 转换参数) + local requestData = { + input_url = INPUT_URL, + args = { "-vn", "-ar", "48000", "-ac", "1" }, + output_format = "dfpwm" + } + + -- 发送 HTTP POST 请求 + local response = http.post( + API_URL, + textutils.serializeJSON(requestData), + { ["Content-Type"] = "application/json" } + ) + + -- 检查请求是否成功 + if not response then + printError("HTTP 请求失败,请检查网络或 API 地址") + return + end + + -- 解析 JSON 响应 + local responseData = textutils.unserializeJSON(response.readAll()) + response.close() + + -- 检查 API 返回状态 + if responseData.status ~= "success" then + printError("音频转换失败: " .. (responseData.error or "未知错误")) + return + end + + -- 获取下载 URL + local downloadUrl = responseData.download_url + print("转换成功!下载 URL: " .. downloadUrl) + + -- 使用 speaker 播放音频 + print("正在播放...") + shell.execute("speaker play " .. downloadUrl) +end + +-- 调用函数 +convertAndPlay() \ No newline at end of file