63 lines
2.0 KiB
Lua
63 lines
2.0 KiB
Lua
-- API 地址 / API Endpoint
|
|
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 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
|
|
|
|
-- 读取响应 / 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() |