修改demo1

This commit is contained in:
HKXluo 2025-04-13 15:00:16 +08:00
parent 2c2f42ee56
commit 564c08c944
No known key found for this signature in database
GPG Key ID: 498DD8DAE5261F38

View File

@ -1,49 +1,62 @@
-- 定义 API 地址
local API_URL = "https://ffmpeg.liulikeji.cn/api/ffmpeg"
-- API 地址 / API Endpoint
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"
-- 发送 POST 请求到 FFmpeg API
-- 主函数 / Main function
local function convertAndPlay()
-- 构造请求数据(使用 DFPWM 转换参数)
-- ===== 1. 准备请求数据 / Prepare request data =====
local requestData = {
input_url = INPUT_URL,
args = { "-vn", "-ar", "48000", "-ac", "1" },
args = { "-vn", "-ar", "48000", "-ac", "1" }, -- DFPWM转换参数 / DFPWM conversion args
output_format = "dfpwm"
}
-- 发送 HTTP POST 请求
local response = http.post(
-- ===== 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("HTTP 请求失败,请检查网络或 API 地址")
printError("HTTP request failed: " .. (err or "unknown error"))
return
end
-- 解析 JSON 响应
local responseData = textutils.unserializeJSON(response.readAll())
-- 读取响应 / Read response
local responseBody = response.readAll()
response.close()
-- 检查 API 返回状态
if responseData.status ~= "success" then
printError("音频转换失败: " .. (responseData.error or "未知错误"))
-- HTTP状态码检查 / HTTP status check
local statusCode = response.getResponseCode()
if statusCode ~= 200 then
printError("API error (status "..statusCode..")")
return
end
-- 获取下载 URL
local downloadUrl = responseData.download_url
print("转换成功!下载 URL: " .. downloadUrl)
-- ===== 4. 解析JSON / Parse JSON =====
local responseData = textutils.unserializeJSON(responseBody)
if not responseData then
printError("Invalid API response (bad JSON)")
return
end
-- 使用 speaker 播放音频
print("正在播放...")
shell.execute("speaker play " .. downloadUrl)
-- 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()