简化代码
This commit is contained in:
parent
01a57db73e
commit
91e97d122b
@ -4,60 +4,40 @@ local API_URL = "http://ffmpeg.liulikeji.cn/api/ffmpeg"
|
|||||||
-- 输入音频URL (可替换) / Input audio URL (replaceable)
|
-- 输入音频URL (可替换) / Input audio URL (replaceable)
|
||||||
local INPUT_URL = "https://git.liulikeji.cn/xingluo/CCTweaked-Demo/raw/branch/main/Demo/FFmpegApi-Demo/demo.mp3"
|
local INPUT_URL = "https://git.liulikeji.cn/xingluo/CCTweaked-Demo/raw/branch/main/Demo/FFmpegApi-Demo/demo.mp3"
|
||||||
|
|
||||||
-- 主函数 / Main function
|
|
||||||
local function convertAndPlay()
|
local function Get_dfpwm_url(INPUT_URL,API_URL)
|
||||||
-- ===== 1. 准备请求数据 / Prepare request data =====
|
-- ===== 1. 发送HTTP请求 / Send HTTP request =====
|
||||||
local requestData = {
|
local requestData = {
|
||||||
input_url = INPUT_URL,
|
input_url = INPUT_URL,
|
||||||
args = { "-vn", "-ar", "48000", "-ac", "1" }, -- DFPWM转换参数 / DFPWM conversion args
|
args = { "-vn", "-ar", "48000", "-ac", "1" }, -- DFPWM转换参数 / DFPWM conversion args
|
||||||
output_format = "dfpwm"
|
output_format = "dfpwm"
|
||||||
}
|
}
|
||||||
|
|
||||||
-- ===== 2. 发送HTTP请求 / Send HTTP request =====
|
|
||||||
local response, err = http.post(
|
local response, err = http.post(
|
||||||
API_URL,
|
API_URL,
|
||||||
textutils.serializeJSON(requestData),
|
textutils.serializeJSON(requestData),
|
||||||
{ ["Content-Type"] = "application/json" }
|
{ ["Content-Type"] = "application/json" }
|
||||||
)
|
)
|
||||||
|
|
||||||
-- ===== 3. 错误处理 / Error handling =====
|
-- ===== 2. 读取数据 / Send HTTP request =====
|
||||||
-- 网络错误 / Network error
|
if not response then error("HTTP Request Failure: "..(err or "Unknown error"))
|
||||||
if not response then
|
else
|
||||||
printError("\nHTTP request failed: " .. (err or "unknown error"))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 读取响应 / Read response
|
-- 读取响应 / Read response
|
||||||
local responseBody = response.readAll()
|
local responseData = textutils.unserializeJSON(res.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()
|
response.close()
|
||||||
|
|
||||||
-- ===== 5. 播放音频 / Play audio =====
|
--返回下载链接 / Return download URL
|
||||||
print("\nConversion successful! Download URL:")
|
if responseData.status ~= "success" then error("Conversion failed:"..(responseData.error or "Unknown error"))
|
||||||
print(responseData.download_url)
|
else
|
||||||
print("\nPlaying...")
|
return responseData.download_url
|
||||||
shell.run("speaker play "..responseData.download_url)
|
end
|
||||||
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user