修改demo1
This commit is contained in:
parent
2c2f42ee56
commit
564c08c944
@ -1,49 +1,62 @@
|
|||||||
-- 定义 API 地址
|
-- API 地址 / API Endpoint
|
||||||
local API_URL = "https://ffmpeg.liulikeji.cn/api/ffmpeg"
|
local API_URL = "http://ffmpeg.liulikeji.cn/api/ffmpeg"
|
||||||
|
|
||||||
-- 内置 MP3 URL(可以替换成你的目标音频)
|
-- 输入音频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"
|
||||||
|
|
||||||
-- 发送 POST 请求到 FFmpeg API
|
-- 主函数 / Main function
|
||||||
local function convertAndPlay()
|
local function convertAndPlay()
|
||||||
-- 构造请求数据(使用 DFPWM 转换参数)
|
-- ===== 1. 准备请求数据 / Prepare request data =====
|
||||||
local requestData = {
|
local requestData = {
|
||||||
input_url = INPUT_URL,
|
input_url = INPUT_URL,
|
||||||
args = { "-vn", "-ar", "48000", "-ac", "1" },
|
args = { "-vn", "-ar", "48000", "-ac", "1" }, -- DFPWM转换参数 / DFPWM conversion args
|
||||||
output_format = "dfpwm"
|
output_format = "dfpwm"
|
||||||
}
|
}
|
||||||
|
|
||||||
-- 发送 HTTP POST 请求
|
-- ===== 2. 发送HTTP请求 / Send HTTP request =====
|
||||||
local response = 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 =====
|
||||||
|
-- 网络错误 / Network error
|
||||||
if not response then
|
if not response then
|
||||||
printError("HTTP 请求失败,请检查网络或 API 地址")
|
printError("HTTP request failed: " .. (err or "unknown error"))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 解析 JSON 响应
|
-- 读取响应 / Read response
|
||||||
local responseData = textutils.unserializeJSON(response.readAll())
|
local responseBody = response.readAll()
|
||||||
response.close()
|
response.close()
|
||||||
|
|
||||||
-- 检查 API 返回状态
|
-- HTTP状态码检查 / HTTP status check
|
||||||
if responseData.status ~= "success" then
|
local statusCode = response.getResponseCode()
|
||||||
printError("音频转换失败: " .. (responseData.error or "未知错误"))
|
if statusCode ~= 200 then
|
||||||
|
printError("API error (status "..statusCode..")")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 获取下载 URL
|
-- ===== 4. 解析JSON / Parse JSON =====
|
||||||
local downloadUrl = responseData.download_url
|
local responseData = textutils.unserializeJSON(responseBody)
|
||||||
print("转换成功!下载 URL: " .. downloadUrl)
|
if not responseData then
|
||||||
|
printError("Invalid API response (bad JSON)")
|
||||||
-- 使用 speaker 播放音频
|
return
|
||||||
print("正在播放...")
|
|
||||||
shell.execute("speaker play " .. downloadUrl)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 调用函数
|
-- API业务错误 / API logic error
|
||||||
|
if responseData.status ~= "success" then
|
||||||
|
printError("Conversion failed: "..(responseData.error or "no error info"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ===== 5. 播放音频 / Play audio =====
|
||||||
|
print("Conversion successful! Download URL:")
|
||||||
|
print(responseData.download_url)
|
||||||
|
print("Playing...")
|
||||||
|
shell.run("speaker play "..responseData.download_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 执行 / Execute
|
||||||
convertAndPlay()
|
convertAndPlay()
|
Loading…
x
Reference in New Issue
Block a user