diff --git a/Demo/FFmpegApi-Demo/demo1.lua b/Demo/FFmpegApi-Demo/demo1.lua index 50a2656..4a475f9 100644 --- a/Demo/FFmpegApi-Demo/demo1.lua +++ b/Demo/FFmpegApi-Demo/demo1.lua @@ -4,60 +4,40 @@ local API_URL = "http://ffmpeg.liulikeji.cn/api/ffmpeg" -- 输入音频URL (可替换) / Input audio URL (replaceable) local INPUT_URL = "https://git.liulikeji.cn/xingluo/CCTweaked-Demo/raw/branch/main/Demo/FFmpegApi-Demo/demo.mp3" --- 主函数 / Main function -local function convertAndPlay() - -- ===== 1. 准备请求数据 / Prepare request data ===== + +local function Get_dfpwm_url(INPUT_URL,API_URL) + -- ===== 1. 发送HTTP请求 / Send HTTP request ===== local requestData = { input_url = INPUT_URL, args = { "-vn", "-ar", "48000", "-ac", "1" }, -- DFPWM转换参数 / DFPWM conversion args output_format = "dfpwm" } - -- ===== 2. 发送HTTP请求 / Send HTTP request ===== local response, err = http.post( API_URL, textutils.serializeJSON(requestData), { ["Content-Type"] = "application/json" } ) - -- ===== 3. 错误处理 / Error handling ===== - -- 网络错误 / Network error - if not response then - printError("\nHTTP request failed: " .. (err or "unknown error")) - return + -- ===== 2. 读取数据 / Send HTTP request ===== + if not response then error("HTTP Request Failure: "..(err or "Unknown error")) + else + -- 读取响应 / Read response + local responseData = textutils.unserializeJSON(res.readAll()) + response.close() + + --返回下载链接 / Return download URL + if responseData.status ~= "success" then error("Conversion failed:"..(responseData.error or "Unknown error")) + else + return responseData.download_url + end end - - -- 读取响应 / Read response - local responseBody = response.readAll() - - - -- HTTP状态码检查 / HTTP status check - local statusCode = response.getResponseCode() - if statusCode ~= 200 then - printError("\nAPI error (status "..statusCode..")") - return - end - - -- ===== 4. 解析JSON / Parse JSON ===== - local responseData = textutils.unserializeJSON(responseBody) - if not responseData then - printError("\nInvalid API response (bad JSON)") - return - end - - -- API业务错误 / API logic error - if responseData.status ~= "success" then - printError("\nConversion failed: "..(responseData.error or "no error info")) - return - end - response.close() - - -- ===== 5. 播放音频 / Play audio ===== - print("\nConversion successful! Download URL:") - print(responseData.download_url) - print("\nPlaying...") - shell.run("speaker play "..responseData.download_url) end --- 执行 / Execute -convertAndPlay() \ No newline at end of file + +local download_url = Get_dfpwm_url(INPUT_URL,API_URL) +-- ===== 播放音频 / Play audio ===== +print("\nConversion successful! Download URL:") +print(download_url) +print("\nPlaying...") +shell.run("speaker play "..download_url)