简化代码

This commit is contained in:
HKXluo 2025-04-13 16:51:30 +08:00
parent 01a57db73e
commit 91e97d122b
No known key found for this signature in database
GPG Key ID: 498DD8DAE5261F38

View File

@ -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
end
-- ===== 2. 读取数据 / Send HTTP request =====
if not response then error("HTTP Request Failure: "..(err or "Unknown error"))
else
-- 读取响应 / 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
local responseData = textutils.unserializeJSON(res.readAll())
response.close()
-- ===== 5. 播放音频 / Play audio =====
print("\nConversion successful! Download URL:")
print(responseData.download_url)
print("\nPlaying...")
shell.run("speaker play "..responseData.download_url)
--返回下载链接 / Return download URL
if responseData.status ~= "success" then error("Conversion failed:"..(responseData.error or "Unknown error"))
else
return responseData.download_url
end
end
end
-- 执行 / Execute
convertAndPlay()
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)